Skip to content
Proyek

metonym/svelte-highlight

Lihat di GitHub

Tentang

Penyorotan sintaks untuk Svelte menggunakan highlight.js.

Pengembangan web dan aplikasi

Cara angka ini diukur

Pertumbuhan bintang terukur

dalam 24 jam
0
dalam 7 hari
-1
dalam 15 hari
-2
dalam 1 bulan
Mengumpulkan riwayat

Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.

README.md

Asli

svelte-highlight

NPM npm

Syntax highlighting for Svelte using highlight.js.

Try it in the Svelte REPL or StackBlitz.

Documentation

The Changelog is available on GitHub.

Installation

# npm
npm i svelte-highlight

# pnpm
pnpm i svelte-highlight

# Bun
bun i svelte-highlight

# Yarn
yarn add svelte-highlight

Basic Usage

The Highlight component requires two props:

  • code: text to highlight
  • language: language grammar used to highlight the text

Import languages from svelte-highlight/languages.

See SUPPORTED_LANGUAGES.md for a list of supported languages.

code accepts any value and is coerced with String(code ?? ""): an object like {} renders as "[object Object]", while undefined/null render as nothing.

<script>
  import Highlight from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<Highlight language={typescript} {code} />

Wrapping Long Lines

By default, overflowing horizontal content is contained by a scrollbar. Set wrap to true to wrap long lines instead.

<Highlight wrap language={typescript} {code} />

Don't combine wrap with HighlightStream's virtualize mode or HighlightVirtual -- both assume one fixed-height line per row, which wrapping breaks.

Accessibility

A long line makes the code block scroll. Keyboard users can only scroll what they can focus, so each code block is a focusable region (tabindex="0", role="region"). Use label to give the region a name. It defaults to "Code". A focused region shows the browser's focus ring, drawn just inside its edge so a scrolling parent can't clip it.

<Highlight language={typescript} {code} label="Add function" />

label works the same way on HighlightAuto, HighlightAutoLazy, HighlightSvelte, LineNumbers, AnsiOutput, HighlightStream, HighlightVirtual, and Typewriter. Inside a CodeWindow, the block takes the window's title (or its label) as its name.

Experimental: CSS Custom Highlight engine

Set engine="css-highlights" to paint tokens with the CSS Custom Highlight API (CSS.highlights, ::highlight()) instead of a <span> per token. The <code> then holds one plain text node. For a 1,768-line JavaScript file, <code> holds 2 DOM nodes instead of 9,478 (3,164 of them <span>s).

Pass theme (theme CSS or a ThemePalette) to generate the ::highlight() rules. Highlight resolves each token's color and background the way the browser would for the "dom" markup. Compound rules (.hljs-title.function_), descendant rules (.hljs-meta .hljs-keyword), and the fallback from title.function_ to .hljs-title all apply, so colors match the "dom" engine. You still need HighlightStyle for the base .hljs rules. Without theme, the text stays plain.

Bold and italic are dropped. ::highlight() can't change font-weight or font-style, so a theme's bold keywords and italic comments render at normal weight and style. Underlines are dropped too.

<script>
  import Highlight, { HighlightStyle } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";
</script>

<HighlightStyle theme={github}>
  <Highlight language={typescript} {code} engine="css-highlights" theme={github} />
</HighlightStyle>
  • The default stays engine="dom".
  • SSR always renders the "dom" markup. The swap to plain text happens after mount, so hydration is unchanged.
  • Where CSS.highlights is unavailable, Highlight keeps the "dom" markup. Check which engine is on screen with resolvedEngine() on a bind:this reference. It returns "dom" until the first CSS paint, and after a failed resolver load.
  • Each instance registers its own highlight names, so many instances can share a page.
  • The color resolver loads on first paint, as a separate chunk. Pages that only use "dom" don't download it. The "dom" markup stays until the resolver is ready, so the code never shows uncolored.
  • A custom default slot renders its own markup, so the engine has no effect there.

Limits. The colors come from theme alone, resolved in JavaScript:

  • Your app's own .hljs-* rules and --shl-* overrides don't reach the highlights.
  • @media blocks in the theme CSS (such as prefers-color-scheme) are skipped. To switch themes, change the theme prop.
  • Theme rules with selectors the resolver can't match (:hover, attributes, ids, sibling combinators) are skipped.
  • There are no per-token elements to query or attach handlers to.

Theming

Every highlight.js theme is also compiled into a ThemePalette: a typed object of --shl-* CSS custom properties plus a shared structural stylesheet (svelte-highlight/themes/base.css) that maps .hljs* classes to those variables. This is the recommended way to theme new projects — themes are data, scoping is an inline style attribute (SSR-safe by construction), and any token is overridable with one CSS line or a Svelte style prop. See SUPPORTED_THEMES.md for a list of compiled themes.

The legacy string-theme path (svelte-highlight/styles, below) keeps working unchanged and is unaffected by any of this.

Global theme, CSS only (no JS in the bundle for theming):

<script>
  import "svelte-highlight/themes/base.css";
  import "svelte-highlight/themes/atom-one-dark.css";
</script>

Scoped themes — multiple themes coexisting, zero runtime cost:

<script>
  import "svelte-highlight/themes/base.css";
  import "svelte-highlight/themes/github.css";
  import "svelte-highlight/themes/atom-one-dark.css";
</script>

<div data-shl-theme="github"><Highlight ... /></div>
<div data-shl-theme="atom-one-dark"><Highlight ... /></div>

base.css sets every variable to initial on a [data-shl-theme] element, so each scoped theme starts from nothing rather than inheriting what it leaves out from another theme.

Component-driven scoping via palette objects (replaces the scoped-<style> machinery described under Scoping styles below — no injected <style> tag, no runtime CSS parsing):

<script>
  import { Highlight, HighlightStyle } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import atomOneDark from "svelte-highlight/themes/atom-one-dark";
  import "svelte-highlight/themes/base.css";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<HighlightStyle theme={atomOneDark}>
  <Highlight language={typescript} {code} />
</HighlightStyle>

This renders SSR-identical output — no <svelte:head>, no <style> tag, just inline vars on the wrapper:

<div style="--shl-fg:#abb2bf;--shl-bg:#282c34;--shl-keyword:#c678dd;...">...</div>

Dual light/dark via light-dark():

<HighlightStyle light={cyanotypeLight} dark={cyanotypeDark} mode="auto">
  <Highlight language={typescript} {code} />
</HighlightStyle>

The wrapper's inline style carries the plain light value for each var (e.g. --shl-keyword:#a626a4) — a baseline every browser can render. A scoped, deduped <style> tag (gated behind @supports (color: light-dark(...))) overrides it with --shl-keyword: light-dark(#a626a4, #c678dd) (etc.) plus color-scheme: light dark on browsers that support light-dark(). light-dark() only takes colors, so the dark palette's font-weight, font-style and text-decoration values apply under the same condition instead: @media (prefers-color-scheme: dark) for mode="auto", or the mode selector. mode="light" / mode="dark" force color-scheme: light / dark. Any other mode string (the legacy "CSS selector" mode) omits color-scheme inline — set it on your own selector for app-controlled switching, e.g. [data-theme="dark"] { color-scheme: dark }.

See it live, with a mode toggle, on the dual-palette preview page.

One-line customization — any token, no theme forking:

[data-shl-theme="atom-one-dark"] {
  --shl-comment: #7f8c98;
}

Per-instance customization via Svelte style props (works automatically since every themed value routes through a var):

<Highlight --shl-keyword="hotpink" --shl-bg="#111" language={typescript} {code} />

Themes are data — derive a variant with a plain object spread:

const custom = {
  ...atomOneDark,
  vars: { ...atomOneDark.vars, "--shl-keyword": "#ff79c6" },
};

The "css-highlights" engine of Highlight and HighlightEditable accepts palettes too:

<HighlightEditable engine="css-highlights" theme={atomOneDark} language={typescript} bind:code />

The --shl-* variable contract

Variable names are derived mechanically from the selectors found in highlight.js themes. color is unsuffixed (it dominates — ~90% of declarations); every other supported property gets a suffix.

Theme selector Custom property
.hljs color --shl-fg
.hljs background / background-color --shl-bg
.hljs-keyword color --shl-keyword
.hljs-keyword background(-color) --shl-keyword-bg
.hljs-keyword font-style --shl-keyword-font-style
.hljs-keyword font-weight --shl-keyword-font-weight
.hljs-keyword text-decoration --shl-keyword-text-decoration
Compound .hljs-title.class_ --shl-title-class_ (scope names joined with -; hljs's trailing underscores are kept)
Descendant .hljs-meta .hljs-keyword --shl-meta-keyword

themes/base.css wraps a multi-scope (compound/descendant) variable in a fallback to its subject scope's variable — e.g. .hljs-meta .hljs-string { color: var(--shl-meta-string, var(--shl-string)); } — so a theme that never styles that specific combination still falls through to the plain scope color instead of rendering unstyled. Declarations that don't fit the var contract (gradients, borders, ::selection, etc.) ship as an opaque extras string on the palette, applied only via the .css artifact.

base.css assumes the default hljs- class prefix; projects using a custom classPrefix should stay on the legacy string path below.

HighlightStyle also inlines color-scheme from the palette(s) it's given — palette.colorScheme for a single theme, or a value derived from mode for a light/dark pair — so native form controls and scrollbars inside a themed block follow the theme too.

extras isn't a corner case for every theme — e.g. the 3024 theme's extras covers 7 declarations, including .hljs ::selection, .hljs::selection, .hljs-operator, and .ruby .hljs-property, all silently dropped when that theme is imported as a ThemePalette object (svelte-highlight/themes/3024) instead of its .css artifact. There's no per-theme fidelity indicator today; SUPPORTED_THEMES.md is where one could eventually live.

Creating themes

svelte-highlight/theme exports a typed, programmatic way to build or derive ThemePalette objects, instead of hand-writing --shl-* vars against the full scope vocabulary.

defineTheme() builds a complete palette from a small typed definition: a ~14-key semantic role layer (pick a dozen colors, get a full theme) plus optional per-scope precision overrides.

import { defineTheme } from "svelte-highlight/theme";

const midnight = defineTheme({
  name: "midnight",
  roles: {
    background: "#0b1021",
    foreground: "#d6deeb",
    comment: { color: "#637777", fontStyle: "italic" },
    keyword: "#c792ea",
    string: "#ecc48d",
    literal: "#f78c6c",
    function: "#82aaff",
    type: "#ffcb8b",
    variable: "#addb67",
    tag: "#7fdbca",
  },
  scopes: {
    "title.class_": "#ffd700", // per-scope precision on top of roles
    "meta keyword": "#5f7e97",
  },
});
<HighlightStyle theme={midnight}>
  <Highlight language={typescript} {code} />
</HighlightStyle>

roles are foreground, background, comment, keyword, string, literal, function, type, variable, property, tag, punctuation, meta, addition, deletion — each expands to a documented set of --shl-* scope vars. A bare string is shorthand for { color: string }; both roles and scopes also accept a { color, background, fontStyle, fontWeight, textDecoration } object. Precedence is extends palette vars → roles → scopes (later layers win per-var, not per-role). foreground/background are required unless extends is given. ROLE_SCOPES is exported for tooling that wants to enumerate exactly which scopes a role expands to.

extendTheme() derives a new palette from any shipped or user palette with role- or scope-level overrides:

import atomOneDark from "svelte-highlight/themes/atom-one-dark";
import { extendTheme } from "svelte-highlight/theme";

const branded = extendTheme(atomOneDark, {
  name: "atom-one-dark-branded",
  roles: { keyword: "#ff79c6" },
});

paletteToCss() emits any palette as a CSS string — for build-time/global CSS instead of the HighlightStyle component:

import { paletteToCss } from "svelte-highlight/theme";

writeFileSync("public/midnight.css", paletteToCss(midnight));

defineTheme output is a plain ThemePalette — spreadable, serializable, diffable, and valid everywhere shipped palettes work, per Themes are data above.

validatePalette() self-checks a ThemePalette — useful for a hand-built or hand-edited palette, e.g. one assembled via the Themes are data spread pattern above:

import { validatePalette } from "svelte-highlight/theme";

const custom = {
  ...atomOneDark,
  vars: { ...atomOneDark.vars, "--shl-keyword": "#ff79c6" },
};

validatePalette(custom); // -> [] when clean; otherwise a list of issues

It flags a missing/malformed vars object, a missing --shl-fg/--shl-bg, a vars key outside the --shl-* grammar, and a --shl-fg/--shl-bg value that doesn't look like a recognized color — never throws. defineTheme already runs it (plus an unknown-scopes-key check) automatically in dev mode.

Importing VS Code themes

svelte-highlight/theme/textmate imports VS Code / TextMate theme JSON (thousands of existing editor themes) into a ThemePalette:

import { fromTextMate } from "svelte-highlight/theme/textmate";
import nightOwl from "./night-owl-color-theme.json" with { type: "json" };

const palette = fromTextMate(nightOwl);

fromTextMate accepts a parsed object only — VS Code themes are often JSONC, so parsing is the caller's job. The scope mapping is best-effort: TextMate scope selectors are matched against a starter table by segment-prefix ("entity.name.function" matches "entity.name.function.method.ts"), with the most specific (longest) match winning ties broken by the later tokenColors entry — the same specificity semantics VS Code itself uses. The starter table covers common scopes, including Markdown code spans/fences and invalid/illegal markers; because matching generalizes by prefix, a row like "keyword" already resolves any more-specific scope sharing that prefix, so "keyword.control" and "keyword.operator" resolve differently without needing a dedicated row for each. Entries that don't map to anything are never silently dropped; pass onWarn to observe them:

const palette = fromTextMate(nightOwl, {
  onWarn: (message) => console.warn(message),
});

To count unmapped entries instead of observing each one as it happens, use fromTextMateWithWarnings, which aggregates every onWarn call:

import { fromTextMateWithWarnings } from "svelte-highlight/theme/textmate";

const { palette, warnings } = fromTextMateWithWarnings(nightOwl);
// warnings.length is the number of unmapped tokenColors entries

When present, theme.semanticTokenColors is read as a higher-priority overlay on top of tokenColors — matching VS Code's own precedence when semantic highlighting is enabled. It's resolved against a starter table of VS Code's default semantic token types (namespace, class, interface, enum, struct, typeParameter, type, parameter, variable, enumMember, property, event, decorator, label, function, method, macro, comment, string, keyword, number, regexp, operator); modifiers ("variable.readonly") and language scoping ("variable:typescript") are not resolved — only the base type before the first . or : is matched.

fromTextMate never populates ThemePalette.extras — editor.background is always read as a single solid color assigned straight to --shl-bg. This matches VS Code's own model (VS Code doesn't support gradients for editor.background either), so it's a deliberate, low-risk scope cut rather than a gap to fix.

Real VS Code theme files are usually JSONC (comments, trailing commas), which fromTextMate doesn't accept directly. scripts/import-textmate-theme.ts is a local dev recipe — not an installed CLI — that strips comments/trailing commas and writes the resulting ThemePalette as JSON; copy it into your own project and adapt as needed:

bun scripts/import-textmate-theme.ts ./night-owl-color-theme.json ./night-owl.palette.json

Styling

Import styles from svelte-highlight/styles. See SUPPORTED_STYLES.md for a list of supported styles. For new projects, prefer Theming above — this is the original, string-based theming path, kept fully supported for backward compatibility. Legacy styles are stable and will not be removed. One concrete reason to pick this path over ThemePalette: base.css assumes the default hljs- class prefix, so projects using a custom classPrefix should stay here. 45 of the styles (90 dark/light files) are original pairs authored by svelte-highlight rather than ported from highlight.js, marked as custom in the gallery and in SUPPORTED_STYLES.md.

There are two ways to apply highlight.js styles.

  1. Injected styles through svelte:head
  2. CSS StyleSheets

Injected Styles

This component exports highlight.js themes in JavaScript. Import the theme from svelte-highlight/styles and inject it using the svelte:head API.

<script>
  import Highlight from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<svelte:head>
  {@html github}
</svelte:head>

<Highlight language={typescript} {code} />

Under a strict Content Security Policy without 'unsafe-inline' for style-src, the browser silently drops this injected <style> tag with no visible error. Use CSS StyleSheet below instead, which is CSP-safe by construction since it's a normal stylesheet reference.

This snippet also has no protection against duplicate <style> tags: placing it inside a component that's mounted more than once emits one <style> tag per mount. For anything mounted more than once, use HighlightStyle instead, which dedupes automatically across instances of the same theme.

CSS StyleSheet

Depending on your set-up, importing a CSS StyleSheet in Svelte may require a CSS file loader. SvelteKit/Vite automatically supports this. For Webpack, refer to examples/webpack.

<script>
  import { Highlight } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import "svelte-highlight/styles/github.css";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<Highlight language={typescript} {code} />

Linking from a CDN

CSS StyleSheets can also be externally linked from a Content Delivery Network (CDN) like unpkg.com.

[!WARNING] Using a CDN is best suited for prototyping and not recommended for production use.

HTML

<head>
  <link
    rel="stylesheet"
    href="https://unpkg.com/svelte-highlight/styles/github.css"
  />
</head>

svelte:head

<svelte:head>
  <link
    rel="stylesheet"
    href="https://unpkg.com/svelte-highlight/styles/github.css"
  />
</svelte:head>

Scoping styles

Themes target global .hljs selectors. Add more than one and the last injected wins. That works when every block on the page shares one theme.

Reach for scoped styles when blocks on the same page need different themes: a style gallery, a side-by-side comparison, or a light snippet next to a dark one.

Wrap each Highlight in HighlightStyle to keep a theme on that block:

<script>
  import { Highlight, HighlightStyle } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import a11yDark from "svelte-highlight/styles/a11y-dark";
  import github from "svelte-highlight/styles/github";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<HighlightStyle theme={a11yDark}>
  <Highlight language={typescript} {code} />
</HighlightStyle>

<HighlightStyle theme={github}>
  <Highlight language={typescript} {code} />
</HighlightStyle>

The component prefixes each selector with a scope class on the wrapper, so the theme only hits markup inside it.

theme is required. scopeClass defaults to a hash of the theme string. Pass your own, or bind it (bind:scopeClass) / read it from the slot (let:scopeClass). An empty string is treated the same as omitting scopeClass — it falls back to the auto-hash. scopeClass must be a single valid CSS class name; a value like "a b" will warn in the console and won't scope as expected.

For a page-wide theme, use {@html theme} as shown above.

Under a style-src CSP that requires a nonce, pass nonce and it's attached to the <style> tag HighlightStyle injects into <svelte:head>:

<HighlightStyle theme={a11yDark} nonce={cspNonce}>
  <Highlight language={typescript} {code} />
</HighlightStyle>

A single ThemePalette (theme as an object, not a light/dark pair) never injects a <style> tag at all — only inline vars on the wrapper — so it's unaffected by a style-src nonce requirement in the first place; that's a reason to prefer it under a strict CSP.

Known limitation: on the CSS-string path, server-side rendering emits one <style> tag per HighlightStyle instance, even when several instances on the page share the exact same theme — the client-side dedupe only kicks in after hydration. This is harmless (CSS is idempotent), but adds extra bytes on pages with many repeated identical-theme instances. The ThemePalette object path has no such concern for a single theme, since it never injects a <style> tag to begin with.

Dark mode

HighlightStyle can emit a light and a dark theme together and switch between them. Pass light and dark instead of theme:

<script>
  import { Highlight, HighlightStyle } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";
  import githubDark from "svelte-highlight/styles/github-dark";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<HighlightStyle light={github} dark={githubDark}>
  <Highlight language={typescript} {code} />
</HighlightStyle>

The mode prop controls how the two themes are switched (default "auto"):

  • "auto" — wrap each theme in a @media (prefers-color-scheme: …) query so the OS/browser preference decides.
  • "light" / "dark" — emit only that single theme.
  • any other string — treated as a CSS selector that gates the dark block while light stays the default. Use this to drive theming from a class or attribute on an ancestor, e.g. a manual theme toggle:
<HighlightStyle light={github} dark={githubDark} mode={'[data-theme="dark"]'}>
  <Highlight language={typescript} {code} />
</HighlightStyle>

Both themes are scoped to the wrapper, so different blocks can use different theme pairs on the same page. When light and dark are both set they take precedence over theme; passing only theme keeps the existing single-theme behavior unchanged.

The ThemePalette object pair path (light/dark as ThemePalettes rather than CSS strings) works the same way, but resolves the pair via light-dark() instead of prefers-color-scheme/selector gating. On a browser that predates light-dark() support, it now falls back to the light theme instead of rendering broken (unset) colors: a scoped, deduped <style> tag applies the light-dark() values behind @supports (color: light-dark(...)), and the wrapper's own inline style carries the plain light value as the pre-@supports baseline. Single-theme (theme alone, no pair) SSR-identical guarantees are unaffected either way.

Styling with CSS variables

Every Highlight variant forwards $$restProps to its top-level element, so you can style the component using CSS variables without resorting to :global overrides or !important.

<Highlight
  language={typescript}
  {code}
  --border-radius="8px"
  --max-width="40rem"
/>

Container variables

These apply to the outer container of every component (the pre element for Highlight, HighlightAuto, HighlightSvelte, and LangTag; the div element for LineNumbers).

Variable Description Default value
--border-radius Border radius of the container 0
--width Width of the container auto
--max-width Maximum width of the container none
--overflow-x Horizontal overflow behavior auto
--overflow-y Vertical overflow behavior auto

[!NOTE] Because the container clips its content (overflow is not visible by default), setting --border-radius rounds the visible corners without any extra CSS.

LineNumbers variables

Variable Description Default value
--line-number-color Text color of the line numbers currentColor
--line-number-digit-width Width of a single gutter digit 0.6em
--border-color Border color of the column of line numbers currentColor
--highlighted-background Background color of highlighted lines rgba(254, 241, 96, 0.2)
--line-added-background Background color of lineStates "added" lines rgba(46, 204, 113, 0.15)
--line-removed-background Background color of lineStates "removed" lines rgba(231, 76, 60, 0.15)
--unhighlighted-opacity Opacity of un-highlighted lines (focus mode) 1
--unhighlighted-filter CSS filter for un-highlighted lines (focus mode) none
--padding Fallback padding for --padding-left / --padding-right 1em
--padding-left Left padding for td elements var(--padding, 1em)
--padding-right Right padding for td elements var(--padding, 1em)

CopyButton variables

Variable Description Default value
--copy-top Top offset of the button 0.5em
--copy-right Right offset of the button 0.5em
--copy-size Width and height of the button 2em
--copy-padding Inner padding of the button 0.5em
--copy-background Background color of the button inherit
--copy-hover-background Background color on hover inside CodeToolbar text color at 12%
--copy-color Foreground color of the button inherit
--copy-border-radius Border radius of the button 4px
--copy-border Border of the button none
--copy-z-index Stacking order of the button 2

Language tag variables

These apply when langtag is set to true.

Variable Description Default value
--langtag-top Top position of the langtag 0
--langtag-right Right position of the langtag 0
--langtag-background Background color of the langtag inherit
--langtag-color Text color of the langtag inherit
--langtag-border-radius Border radius of the langtag 0
--langtag-padding Padding of the langtag 1em
--langtag-font-size Font size of the langtag inherit

CodeWindow variables

Variable Description Default value
--window-background Background color of the window body #1e1e1e
--window-border Border of the window 1px solid rgba(255, 255, 255, 0.1)
--window-radius Corner radius of the window 0
--titlebar-gap Gap between items in the title bar 0.5em
--titlebar-padding Padding of the title bar 0.65em 1em
--titlebar-background Background color of the title bar #2d2d2d
--titlebar-border Bottom border of the title bar 1px solid rgba(255, 255, 255, 0.1)
--titlebar-color Text color of the title bar rgba(255, 255, 255, 0.6)
--titlebar-font-family Font family of the title bar system-ui, -apple-system, sans-serif
--titlebar-font-size Font size of the title bar 0.8125em
--titlebar-line-height Line height of the title bar normal
--dot-gap Gap between the macOS traffic-light dots 0.5em
--dot-size Diameter of the macOS traffic-light dots 0.75em
--dot-close Color of the "close" traffic-light dot #ff5f56
--dot-minimize Color of the "minimize" traffic-light dot #ffbd2e
--dot-maximize Color of the "maximize" traffic-light dot #27c93f
--prompt-font-family Font family of the terminal prompt ui-monospace, monospace
--prompt-font-weight Font weight of the terminal prompt 700
--prompt-color Color of the terminal prompt inherit

FileTabs variables

Variable Description Default value
--file-tabs-gap Gap between tabs 0
--file-tabs-background Tab strip background inherit
--tab-overflow-fade Width of the scroll shadow shown on the overflowing edge 1.5rem
--tab-padding Padding of each tab 0.5em 1em
--tab-color Text color of inactive tabs inherit
--tab-background Background color of inactive tabs transparent
--tab-inactive-opacity Opacity of inactive tabs (active and hovered tabs stay opaque) 0.55
--tab-active-color Text color of the active tab the highlighted code's text color
--tab-active-background Background color of the active tab the highlighted code's background
--tab-focus-outline Keyboard focus outline of a tab 2px solid currentColor

CodeToolbar variables

Variable Description Default value
--toolbar-background Tint painted over the theme background color-mix(in srgb, currentColor 5%, transparent)
--toolbar-color Text color of the toolbar content the theme's text color
--toolbar-border Bottom border of the toolbar 1px solid text color at 14%
--toolbar-padding Padding of the toolbar 0.25em 0.375em 0.25em 1em
--toolbar-gap Gap between the title, badge and controls 0.75em
--toolbar-height Minimum height of the toolbar 2.75em
--toolbar-font-family Font family of the toolbar system-ui, -apple-system, "Segoe UI", Roboto, sans-serif
--toolbar-font-size Font size of the toolbar 0.8125em
--toolbar-z-index Stacking order of a sticky toolbar 3
--langtag-background Background color of the language badge text color at 10%
--langtag-color Text color of the language badge text color at 72%
--langtag-padding Padding of the language badge 0.375em 0.625em
--langtag-border-radius Corner radius of the language badge 999px
--langtag-font-size Font size of the language badge 0.875em

ToolbarButton variables

WrapToggle, and CopyButton inside CodeToolbar, use these too.

Variable Description Default value
--toolbar-button-size Width and height of the button 2em
--toolbar-button-gap Gap between the button's content 0.4em
--toolbar-button-padding Padding of the button 0.3em
--toolbar-button-background Background color of the button transparent
--toolbar-button-color Icon and text color of the button text color at 72%
--toolbar-button-hover-background Background color on hover text color at 12%
--toolbar-button-pressed-background Background color when pressed or active text color at 18%
--toolbar-button-border-radius Corner radius of the button 6px
--toolbar-button-border Border of the button none
--toolbar-button-font-size Font size of the button inherit

Svelte Syntax Highlighting

Use the HighlightSvelte component for Svelte syntax highlighting. Its grammar understands Svelte 5 runes ($state, $derived, $effect and its suffixed forms, $props.id, $inspect.trace), store auto-subscription ($store, $store(), $store.prop), lang="ts"/context="module" script-block resolution, and directive shorthand (on:, bind:, use:, and friends) — genuinely ahead of generic HTML-plus-embedded-JS Svelte highlighting, which has no notion that runes exist.

<script>
  import { HighlightSvelte } from "svelte-highlight";
  import github from "svelte-highlight/styles/github";

  const code = `<script>
  let count = $state(0);
  let doubled = $derived(count * 2);
<\/script>

<button on:click={() => count++}>{doubled} (store: {$externalCount})</button>`;
</script>

<svelte:head>
  {@html github}
</svelte:head>

<HighlightSvelte {code} />

Nested template declaration tags are also supported — including a second, shadowing {const ...} declared inside a nested element within the same {#each} block.

See the kitchen-sink live demo for a fuller tour of Svelte highlighting across every theme.

Auto-highlighting

The HighlightAuto component uses the highlightAuto API and attempts to guess what grammar to use based on the provided code. Its on:highlight event's secondBest field surfaces the runner-up candidate and its relevance, for showing detection confidence.

[!WARNING] Mounting HighlightAuto statically imports all 279 shipped grammars, regardless of languageNames, so bundle size is unconditionally larger than a Highlight with a single named language. Specify a language if possible. If you can't, and plain text before hydration is fine, use HighlightAutoLazy instead. It loads only a few likely grammars.

<script>
  import { HighlightAuto } from "svelte-highlight";
  import github from "svelte-highlight/styles/github";

  const code = `body {\n  padding: 0;\n  color: red;\n}`;
</script>

<svelte:head>
  {@html github}
</svelte:head>

<HighlightAuto {code} />

Limiting Language Detection

You can restrict language auto-detection to a subset using the languageNames prop. This can improve performance and accuracy — it narrows the candidates scored during detection, but it does not reduce bundle size, since all shipped grammars are imported unconditionally (see the warning above). A grammar registered with disableAutodetect: true stays excluded from detection even when explicitly named in languageNames.

<script>
  import { HighlightAuto } from "svelte-highlight";
  import github from "svelte-highlight/styles/github";

  const code = "const x = 42;";
</script>

<svelte:head>
  {@html github}
</svelte:head>

<HighlightAuto {code} languageNames={["javascript", "typescript"]} />

Lazy auto-highlighting

HighlightAutoLazy detects the language without importing every grammar. It works in two steps:

  1. It scores code against a small generated fingerprint index (about 33 KB gzip) and picks the 5 likeliest grammars. This step loads no grammar.
  2. It imports only those grammars, plus any grammars they embed. Then the same relevance engine as HighlightAuto picks the winner from them and from every grammar already registered.

Each grammar is its own chunk, so a page fetches only the grammars its code needs.

<script>
  import { HighlightAutoLazy } from "svelte-highlight";
  import github from "svelte-highlight/styles/github";

  const code = `@media (max-width: 600px) {\n  body { padding: 0; }\n}`;
</script>

<svelte:head>
  {@html github}
</svelte:head>

<HighlightAutoLazy {code} />

It has the same props, slot props, and on:highlight detail as HighlightAuto, with these differences:

  • Plain text first. On the server and on the first client render, it shows code as escaped plain text with no language. It highlights after mount, once the grammars load. If they are already registered, that happens at once.
  • on:highlight fires for results only. It does not fire for the plain-text placeholder.
  • Latest code wins. If code changes while grammars still load, the older result is dropped.
  • languageNames skips the fingerprint step. It loads each named grammar on demand and detects among those only.
  • Different picks. It picks from fewer grammars, so its answer can differ from HighlightAuto's. On this repo's detection corpus it is right more often, but the fingerprint step misses some grammars with few keywords. To make sure a grammar competes, register it first (for example with loadLanguage and registerAll), or pass languageNames.

The two steps are also available without the component, from svelte-highlight/auto-detect:

import {
  detectCandidates,
  highlightAutoLazy,
} from "svelte-highlight/auto-detect";

detectCandidates(code); // ["stylus", "less", "css", "scss", "ada"]

const result = await highlightAutoLazy(code);
result.language; // "css"
result.value; // highlighted HTML

Both accept an options object: k (number of candidates, default 5), languageNames, and registry (defaults to the shared registry). highlightAutoIfLoaded(code, options) does the same work synchronously and returns undefined if a grammar it needs is not registered yet.

Line Numbers

Use the LineNumbers component to render the highlighted code with line numbers.

<script>
  import Highlight, { LineNumbers } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import atomOneDark from "svelte-highlight/styles/atom-one-dark";

  const code = "const add = (a: number, b: number) => a + b";
</script>

<svelte:head>
  {@html atomOneDark}
</svelte:head>

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers {highlighted} />
</Highlight>

Hidden Border

Set hideBorder to true to hide the border of the line numbers column.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers {highlighted} hideBorder />
</Highlight>

Wrapped Lines

By default, overflowing horizontal content is contained by a scrollbar.

Set wrapLines to true to hide the border of the line numbers column.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers {highlighted} wrapLines />
</Highlight>

Custom Starting Line Number

The line number starts at 1. Customize this via the startingLineNumber prop.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers {highlighted} startingLineNumber={42} />
</Highlight>

Windowed Lines

LineNumbers re-splits the entire highlighted string into lines on every update, which is wasteful once you're already rendering a window out of a much larger document. Pass a pre-split lines array instead -- the same per-line HTML shape svelte-highlight/tokenized-document's lineRange (see Large documents) and HighlightStream's incremental rendering already produce -- and set lineCount to the total document line count so the gutter is sized for the whole document instead of jittering as the window scrolls. startingLineNumber offsets the row numbers to match the window's position; highlightedLines/lineStates index relative to the lines array actually rendered, not the absolute document line.

<script>
  import { LineNumbers } from "svelte-highlight";
  import { createTokenizedDocument } from "svelte-highlight/tokenized-document";
  import typescript from "svelte-highlight/languages/typescript";

  const doc = createTokenizedDocument({ language: typescript });
  doc.setCode(bigFile);

  const start = 50_000;
  const end = 50_040;
</script>

<LineNumbers
  lines={doc.lineRange(start, end)}
  startingLineNumber={start + 1}
  lineCount={doc.lineCount()}
/>

Highlighted Lines

Specify the lines to highlight using the highlightedLines prop. Indices start at zero.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers {highlighted} highlightedLines={[0, 2, 3, 14]} />
</Highlight>

Use --unhighlighted-opacity or --unhighlighted-filter to de-emphasize the remaining lines and focus attention on the highlighted ones. Both only take effect when highlightedLines is non-empty.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers
    {highlighted}
    highlightedLines={[0, 2, 3, 14]}
    --unhighlighted-opacity="0.4"
  />
</Highlight>

Use --highlighted-background to customize the background color of highlighted lines.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers
    {highlighted}
    highlightedLines={[0, 2, 3, 14]}
    --highlighted-background="#000"
  />
</Highlight>

Line States

highlightedLines only supports one visual treatment. Use lineStates for a per-line state map -- "highlighted" (same treatment as highlightedLines), "focus" (exempt from dimming, no background color), "added", or "removed" -- so diff-style and meta-string-style decorations can share one primitive. Indices start at zero and are merged with highlightedLines, which is equivalent to setting "highlighted" here.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers
    {highlighted}
    lineStates={{ 1: "added", 2: "removed", 4: "focus" }}
  />
</Highlight>

Use --line-added-background and --line-removed-background to customize the background colors.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers
    {highlighted}
    lineStates={{ 1: "added", 2: "removed" }}
    --line-added-background="rgba(0, 255, 0, 0.15)"
    --line-removed-background="rgba(255, 0, 0, 0.15)"
  />
</Highlight>

Custom Styles

Use --style-props to customize styles.

Style prop Description Default value
--line-number-color Text color of the line numbers currentColor
--line-number-digit-width Width of a single gutter digit 0.6em
--border-color Border color of the column of line numbers currentColor
--padding Fallback padding for --padding-left / --padding-right 1em
--padding-left Left padding for td elements var(--padding, 1em)
--padding-right Right padding for td elements var(--padding, 1em)
--highlighted-background Background color of highlighted lines rgba(254, 241, 96, 0.2)
--line-added-background Background color of lineStates "added" lines rgba(46, 204, 113, 0.15)
--line-removed-background Background color of lineStates "removed" lines rgba(231, 76, 60, 0.15)
--unhighlighted-opacity Opacity of un-highlighted lines (focus mode) 1
--unhighlighted-filter CSS filter for un-highlighted lines (focus mode) none

See Styling with CSS variables for the full list, including container-level variables like --border-radius, --width, and --overflow-x.

<Highlight language={typescript} {code} let:highlighted>
  <LineNumbers
    {highlighted}
    --line-number-color="pink"
    --line-number-digit-width="0.75em"
    --border-color="rgba(255, 255, 255, 0.2)"
    --padding-left={0}
    --padding-right="3em"
    --highlighted-background="#000"
    --border-radius="8px"
    --max-width="40rem"
  />
</Highlight>

The gutter's position and border use logical CSS properties (inset-inline-start/inset-inline-end), so it follows dir="rtl" without any extra configuration.

Language Tag

When using a custom slot, forward langtag and languageName from Highlight to LineNumbers.

<Highlight
  language={typescript}
  {code}
  langtag
  let:highlighted
  let:langtag
  let:languageName
>
  <LineNumbers {highlighted} {langtag} {languageName} />
</Highlight>

With HighlightAuto:

<HighlightAuto {code} langtag let:highlighted let:langtag let:languageName>
  <LineNumbers {highlighted} {langtag} {languageName} />
</HighlightAuto>

Copy Button

Compose the CopyButton component alongside Highlight to add a copy-to-clipboard button. Wrap both in a relatively-positioned container so the button can be positioned over the code block.

By default, it copies the code using the native Clipboard API and shows a transient "copied" state. The button renders a copy icon that swaps to a checkmark on success; the text / copiedText props set the aria-label for each state. Clicks are ignored while in the "copied" state, so no duplicate copy fires.

<script>
  import { Highlight, CopyButton } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import atomOneDark from "svelte-highlight/styles/atom-one-dark";

  const code = "const add = (a: number, b: number) => a + b";
</script>

<svelte:head>
  {@html atomOneDark}
</svelte:head>

<div style="position: relative">
  <Highlight language={typescript} {code} />
  <CopyButton {code} />
</div>

Custom Copy Behavior

Pass a copy function to override the default copy behavior. It receives the code to copy and may be async (return a promise); the "copied" state is shown only once it resolves. Clicks are ignored while a copy is in flight, so a slow async copy can't fire duplicates.

<script>
  function copy(code) {
    navigator.clipboard.writeText(code);
    console.log("Copied:", code);
  }
</script>

<CopyButton {code} {copy} />

The component dispatches a copy event on success and an error event if the copy behavior throws.

<CopyButton
  {code}
  on:copy={(e) => console.log(e.detail.code)}
  on:error={(e) => console.error(e.detail.error)}
/>

Transforming copied text

Displayed code often carries decoration that shouldn't survive a copy -- shell prompts, REPL markers, diff signs. Pass a transform function to strip it before the copy function runs; on:copy reports the transformed string, not the original code.

stripPrompts and stripDiffMarkers cover the common cases and are exported for reuse (compose them for your own transform when a block needs both):

<script>
  import { CopyButton, stripPrompts } from "svelte-highlight";

  const code = "$ npm install\nadded 1 package";
</script>

<CopyButton {code} transform={stripPrompts} />

Custom Button Content

Provide custom button content using the default slot to replace the default icons. The slot exposes a copied boolean and a copying boolean (true while an async copy is in flight), so you can render a pending state for slow copy functions.

<CopyButton {code} let:copied let:copying>
  {#if copying}Copying…{:else if copied}Copied!{:else}Copy{/if}
</CopyButton>

Custom Styles

Use --copy-* style props to customize the button.

<CopyButton
  {code}
  --copy-background="rgba(255, 255, 255, 0.1)"
  --copy-color="#fff"
  --copy-border-radius="8px"
/>

Set absolute={false} to lay the button out in normal document flow instead of overlaying its container. Inside CodeToolbar, this is the default.

<CopyButton {code} absolute={false} />

With Line Numbers

Compose CopyButton with LineNumbers by wrapping both in a relatively-positioned container.

<div style="position: relative">
  <Highlight language={typescript} {code} let:highlighted>
    <LineNumbers {highlighted} />
  </Highlight>
  <CopyButton {code} />
</div>

With Language Tag

When using a language tag alongside CopyButton, offset --langtag-top and --langtag-right so the tag sits to the left of the button (the copy button defaults to --copy-top: 0.5em, --copy-right: 0.5em, and --copy-size: 2em).

<div style="position: relative">
  <Highlight
    language={typescript}
    {code}
    langtag
    --langtag-top="0"
    --langtag-right="3em"
    --langtag-padding="0.25em 0.5em"
    --langtag-font-size="0.75em"
  />
  <CopyButton {code} />
</div>

Editable

HighlightEditable is a contenteditable code block. It re-highlights on every edit and keeps the caret where you left it.

Use bind:code to keep your state in sync.

<script>
  import { HighlightEditable } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import atomOneDark from "svelte-highlight/styles/atom-one-dark";

  let code = "const add = (a: number, b: number) => a + b";
</script>

<svelte:head>
  {@html atomOneDark}
</svelte:head>

<HighlightEditable language={typescript} bind:code />

Keyboard Behavior

  • Enter inserts a newline.
  • Tab / Shift+Tab indent or dedent the selected lines (or insert one indent at the caret). Set the indent size with the tabSize prop (default 2).
  • Escape then Tab/Shift+Tab moves keyboard focus out of the editor instead of indenting, so the block never traps focus. Any other key clears this and restores normal Tab behavior.
  • Cmd/Ctrl+Z undo and Shift+Cmd/Ctrl+Z (or Ctrl+Y) redo. Typing bursts collapse into a single undo step; cap the retained history with the historyLimit prop (default 200).

Pasting and dragging content into the editor both insert plain text only, regardless of the source payload (rich text, HTML, etc.).

Dispatched Events

HighlightEditable dispatches change (on every edit), blur, and history (whenever the undo/redo stack changes).

<HighlightEditable
  language={typescript}
  bind:code
  on:change={(e) => console.log(e.detail.code)}
  on:blur={(e) => console.log(e.detail.code)}
  on:history={(e) => console.log(e.detail.canUndo, e.detail.canRedo)}
/>

Imperative API

Use bind:this to call methods on the instance.

<script>
  let editor;
</script>

<HighlightEditable bind:this={editor} language={typescript} bind:code />

<button on:click={() => editor.undo()}>Undo</button>
<button on:click={() => editor.redo()}>Redo</button>
<button on:click={() => editor.indent()}>Indent</button>
<button on:click={() => editor.clear()}>Clear</button>

Available methods: undo, redo, focus, selectAll, insert, indent, outdent, setCode, clear, getCode, canUndo, canRedo, and resolvedEngine.

Custom Styles

Customize the focus outline with the --outline-color, --outline-width, and --outline-offset style props.

<HighlightEditable
  language={typescript}
  bind:code
  --outline-color="#42be65"
  --outline-width="2px"
/>

Read-only

Set readonly to keep highlighting, caret placement, and selection active while blocking edits (typing, paste, drag-and-drop, and Tab/Shift+Tab indent). The imperative API still works, so a consumer can toggle a document read-only without swapping components.

<script>
  let readonly = false;
</script>

<HighlightEditable language={typescript} bind:code {readonly} />
<button on:click={() => (readonly = !readonly)}>
  {readonly ? "Unlock" : "Lock"}
</button>

Experimental: CSS Custom Highlight engine

engine="css-highlights" paints tokens with the CSS Custom Highlight API (CSS.highlights, ::highlight()) instead of wrapping them in <span>s. The editable <code> stays plain text (one <span> per line, reused from the default engine's line structure, but with no per-token spans inside), so a repaint never replaces the DOM the caret is sitting in.

Requires Chrome 105+, Safari 17.2+, or Firefox 140+. Where CSS.highlights is unavailable, HighlightEditable falls back to engine="dom" silently; check what is on screen with editor.resolvedEngine(). It returns "dom" until the first CSS paint, and while the resolver load has failed.

Pass theme (theme CSS or a ThemePalette) to color the tokens. HighlightEditable resolves colors the same way Highlight does, so compound rules (.hljs-title.function_) and descendant rules (.hljs-meta .hljs-keyword) apply. Only colors and backgrounds carry over, and the same limits apply. Each edit re-resolves only the lines it changed. As with Highlight, the resolver loads on the first paint, so "dom"-only pages don't bundle it. Until it loads, the editor shows "dom" spans.

theme only covers token colors — you still need HighlightStyle (or an injected stylesheet) for the base .hljs layout rules (padding, overflow, background), exactly as with the default engine.

<script>
  import { HighlightEditable, HighlightStyle } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  let code = "const add = (a: number, b: number) => a + b";
</script>

<HighlightStyle theme={github}>
  <HighlightEditable language={typescript} bind:code engine="css-highlights" theme={github} />
</HighlightStyle>

Without Svelte

createEditor from svelte-highlight/editable runs the same editor on any element, with no framework. Use a <code contenteditable> inside a <pre>. It takes the same settings as the component and has the same methods, plus update, syncCode and destroy.

import { createEditor } from "svelte-highlight/editable";
import typescript from "svelte-highlight/languages/typescript";

const editor = createEditor(document.querySelector("pre code"), {
  language: typescript,
  code: "const add = (a, b) => a + b;",
  onChange: (code) => console.log(code),
});

editor.update({ readonly: true });
editor.destroy();

For the "css-highlights" engine, pass theme to createEditor or update. The editor adds its ::highlight() rules to the page and removes them on destroy.

To render the editor on a server, put prerenderEditor(language, code).html inside the <code>: the markup the "dom" engine paints, so the code has its colors before any script runs. In the browser, pass prerenderEditor's result for the same code as prerendered. The first paint then reuses its parse and keeps the markup instead of rebuilding it. HighlightEditable does this on its own, so a server-rendered page shows its colors before hydration.

import { createEditor, prerenderEditor } from "svelte-highlight/editable";

// Server: `<pre><code contenteditable="true">${prerenderEditor(typescript, code).html}</code></pre>`
// Browser:
createEditor(element, { language: typescript, code, prerendered: prerenderEditor(typescript, code) });

instanceHighlightStyle(theme, editor.instanceId) still works for an editor made without theme. Add its <style> tag to the page's <head>. It only converts single-class .hljs-<scope> rules, so compound scopes such as title.function_ get no color.

Adding a gutter

HighlightEditable has no slot and doesn't expose highlighted/languageName, so it can't be wrapped directly in LineNumbers the way a static Highlight block can. Mirror the same code in a second, read-only Highlight instance wrapped in LineNumbers, and position it alongside the editable block:

<script>
  import { Highlight, HighlightEditable, LineNumbers } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";

  let code = "const add = (a: number, b: number) => a + b";
</script>

<HighlightEditable language={typescript} bind:code />

<Highlight language={typescript} {code} let:highlighted let:languageName>
  <LineNumbers {highlighted} {languageName} />
</Highlight>

Language Targeting

All Highlight components apply a data-language attribute on the codeblock containing the language name.

See SUPPORTED_LANGUAGES.md for a list of supported languages.

[data-language="css"] {
  /* custom style rules */
}

Language Tags

The language tag is one badge (LangTag.svelte) reached three ways: automatically, via Highlight/HighlightAuto/HighlightSvelte's built-in fallback render when langtag is set; directly, via the standalone LangTag component; or via LineNumbers' own langtag/languageName props, which are not wired to a wrapping Highlight automatically and must be forwarded manually from slot props (see Language Tag under Line Numbers).

Set langtag to true to display the language name in the top right corner of the code block.

Customize the language tag background, color, and border-radius using style props.

Setting langtag opts a <Highlight> usage out of the svelte-highlight/static build-time transform, since static output can't pull in LangTag's styles.

If the highlighted output is empty (e.g. empty code, or code that highlights to an empty string), the badge and code block still render, but the code falls back to plain, unhighlighted text — the hljs class is still applied, there's just no syntax-highlighting markup inside it.

See the Languages page for a list of supported languages.

Style prop Description Default value
--langtag-top Top position of the langtag 0
--langtag-right Right position of the langtag 0
--langtag-background Background color of the langtag inherit
--langtag-color Text color of the langtag inherit
--langtag-border-radius Border radius of the langtag 0
--langtag-padding Padding of the langtag 1em
--langtag-font-size Font size of the langtag inherit
<script>
  import { HighlightAuto } from "svelte-highlight";

  $: code = `.body { padding: 0; margin: 0; }`;
</script>

<HighlightAuto {code} langtag />
<HighlightAuto
  {code}
  langtag
  --langtag-background="linear-gradient(135deg, #2996cf, 80%, white)"
  --langtag-color="#fff"
  --langtag-border-radius="6px"
  --langtag-padding="0.5rem"
/>

Custom Language

A language is { name, register }, where register is a plain-JSON grammar — the same format every svelte-highlight/languages/* module ships, produced by converting an hljs-format grammar at build time.

The easiest way to author one is with svelte-highlight/compat's fromHighlightJs: write your grammar exactly as the hljs guide describes (a register(hljs) function returning a mode object), and convert it at runtime — no build step, no bundled hljs code.

<script>
  import { Highlight } from "svelte-highlight";
  import { fromHighlightJs } from "svelte-highlight/compat";

  function defineDotenv(hljs) {
    return {
      name: "dotenv",
      contains: [
        hljs.HASH_COMMENT_MODE,
        { className: "keyword", begin: /^[ \t]*export(?=[ \t])/ },
        { className: "attr", begin: /[A-Za-z_][A-Za-z0-9_]*(?=[ \t]*=)/ },
        {
          className: "string",
          variants: [
            { begin: /"/, end: /"/, contains: [hljs.BACKSLASH_ESCAPE] },
            { begin: /'/, end: /'/ },
          ],
        },
      ],
    };
  }

  const languagePromise = fromHighlightJs("dotenv", defineDotenv);
</script>

{#await languagePromise then language}
  <Highlight {language} code={'PORT=3000\n# comment'} />
{/await}

fromHighlightJs needs highlight.js itself, which svelte-highlight does not bundle — install it as your own dependency (npm install highlight.js). It's only imported the first time you call fromHighlightJs, so pages that never use it don't pay for it.

fromHighlightJs also accepts an optional third source argument (raw source text, e.g. via a bundler's ?raw import) to recover array-membership on:begin guards the compiled callback alone can't expose, and the returned object carries a warnings: string[] field (empty when clean) so you can detect a degraded conversion without reading the console.

const language = await fromHighlightJs("dotenv", defineDotenv, grammarSourceText);
if (language.warnings.length > 0) {
  console.error("dotenv degraded:", language.warnings);
}

If you're using TypeScript, use the LanguageType interface to type the language.

import type { LanguageType } from "svelte-highlight";
import { fromHighlightJs } from "svelte-highlight/compat";

const language: LanguageType<"dotenv"> = await fromHighlightJs(
  "dotenv",
  (hljs) => ({
    name: "dotenv",
    contains: [
      hljs.HASH_COMMENT_MODE,
      { className: "attr", begin: /[A-Za-z_][A-Za-z0-9_]*(?=[ \t]*=)/ },
    ],
  }),
);

Building a package for others to install? Ship the converted grammar directly instead of calling fromHighlightJs at runtime — see scripts/convert-grammars.ts for the converter this package's own bundled languages go through.

Conversion fidelity

fromHighlightJs and scripts/convert-grammars.ts (the pipeline behind every one of the ~244 bundled svelte-highlight/languages/* grammars) share the exact same converter, so a custom grammar goes through identical logic to every shipped one, not a lesser path.

Most hljs grammars are plain-JSON contains/begin/end/className rules, which convert as-is. A grammar can also use on:begin, on:end, or __beforeBegin callbacks for cases regexes alone can't express; the converter recognizes six specific shapes and turns them into declarative flags:

  • An on:begin callback that copies the matched text so the closing delimiter must equal the opening one (heredocs).
  • An on:begin callback that only accepts a match at the very start of input (a shebang guard).
  • An on:begin callback that disambiguates an opening tag from a generic comparison (JSX).
  • An on:begin callback that checks the character immediately before the match (a letter-boundary guard).
  • An on:begin callback checking SOME_SET.has(match[0]) against a module-level word list — recovered only when you also pass the grammar's own source text as fromHighlightJs's third argument, since the compiled callback alone doesn't expose the backing array.
  • A __beforeBegin callback that rejects a match immediately following a . (hljs's beginKeywords dot-guard).

Any other on:begin/on:end/__beforeBegin body converts too, but with a console.warn and the rule's relevance forced to zero — it still matches, unconditionally, without skewing auto-detection. Check language.warnings (see above) to catch this without reading the console.

Upgrading a custom language written before 7.16? Before 7.16, a custom language was { name, register: (hljs) => modeObject } passed synchronously straight to <Highlight>'s language prop. Since 7.16 replaced the highlight.js runtime with this package's own engine, register must be plain-JSON grammar IR instead, and fromHighlightJs (now async, requiring {#await}) is the replacement shown above.

Custom Plugin

Third-party hljs language plugins work the same way: pass their register(hljs) export to fromHighlightJs.

This example uses the cURL language plugin.

<script>
  import { Highlight } from "svelte-highlight";
  import { fromHighlightJs } from "svelte-highlight/compat";
  import curl from "highlightjs-curl";
  import github from "svelte-highlight/styles/github";

  const languagePromise = fromHighlightJs("curl", curl);

  const code = `curl -X POST "https://api.example.com/data" \\
     -H "Content-Type: application/json" \\
     -d '{"key": "value"}'`;
</script>

<svelte:head>
  {@html github}
</svelte:head>

{#await languagePromise then language}
  <Highlight {language} {code} />
{/await}

Code-splitting

You can use the await import syntax for code-splitting.

In the example below, the HighlightAuto component and injected styles are dynamically loaded.

<script>
  import { onMount } from "svelte";

  let component;
  let styles;

  onMount(async () => {
    component = (await import("svelte-highlight")).HighlightAuto;
    styles = (await import("svelte-highlight/styles/github")).default;
  });
</script>

<svelte:head>
  {#if styles}
    {@html styles}
  {/if}
</svelte:head>

<svelte:component
  this={component}
  langtag
  code={`body {\n  padding: 0;\n  color: red;\n}`}
/>

Registering a grammar checks its canonical name, not an alias lookup, before skipping — so a grammar that aliases another (highlight.js's "ini" aliases "toml") can't make the real grammar look already-registered before it has actually loaded.

Loading a language by name

The example above imports a specific language as a static string, which lets the bundler split out only the grammars you reference. When the language is known only at runtime — a Markdown fence (```ts), an API field, or a user-selected value — use the loadLanguage helper to import a grammar by name:

<script>
  import { Highlight, loadLanguage } from "svelte-highlight";
  import github from "svelte-highlight/styles/github";

  // The language name is not known until runtime.
  export let language = "typescript";
  export let code = "const add = (a, b) => a + b;";
</script>

<svelte:head>
  {@html github}
</svelte:head>

{#await loadLanguage(language) then grammar}
  <Highlight {code} language={grammar} />
{:catch}
  <pre>{code}</pre>
{/await}

loadLanguage accepts a supported language name, dynamically imports its grammar, and resolves with the language object. It rejects with a LanguageLoadError for an unrecognized name, so handle the {:catch} block (or .catch) when the name comes from untrusted input. LanguageLoadError is importable from svelte-highlight for an instanceof check:

import { LanguageLoadError } from "svelte-highlight";

try {
  const grammar = await loadLanguage(name);
} catch (error) {
  if (error instanceof LanguageLoadError) {
    // handle a missing/misspelled language name
  }
}

Grammars with embedded sublanguages (astro, svelte, and others with a dependencies list) register their dependencies automatically — Highlight, HighlightAuto, and HighlightSvelte all call ensureRegistered on the language they're given, which registers the grammar and recurses through its dependencies.

loadLanguage does a fresh import() on every call and keeps no explicit cache, but it's safe to call repeatedly for the same name: module loaders dedupe dynamic imports by resolved specifier rather than re-fetching, so repeated calls resolve to the same cached module.

Note: Because the imported name is dynamic, the bundler cannot prune unused grammars and will emit a chunk for every language — all 279 shipped grammars. Prefer a static import when the language is known ahead of time, and reach for loadLanguage only when it is not.

A practical case is rendering Markdown, where each fenced block declares its own language — see "Rendering Markdown/MDX fences" below for a ready-made highlightFence helper built on top of loadLanguage.

Static mode

svelte-highlight/static is a Svelte preprocessor. At build time it replaces <Highlight> usages that have known code and language with pre-rendered highlight.js HTML, so the client never downloads highlight.js or that grammar module.

The biggest win is on server-rendered or statically-generated pages, since a client-only SPA still ships the pre-rendered markup as a string inside the JS bundle, just without highlight.js and the grammar module.

// vite.config.js
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { highlightStatic } from "svelte-highlight/static";

export default {
  plugins: [svelte({ preprocess: [highlightStatic()] })],
};
<script>
  import Highlight from "svelte-highlight";
  import javascript from "svelte-highlight/languages/javascript";
</script>

<Highlight language={javascript} code="const x = 1;" />

At build time this becomes plain HTML:

<pre class="hljs" data-language="javascript"><code class="hljs" tabindex="0" role="region" aria-label="Code"
  ><span class="hljs-keyword">const</span> x = <span class="hljs-number">1</span>;</code
></pre>

The static transform runs only when all of these are true:

  • language is a plain identifier from a static import of svelte-highlight/languages/*.
  • code is a string literal or a template literal with no ${...} interpolation.
  • The element has no slot content, spread props, or directives (bind:, on:, etc.).
  • langtag, if set, is a bare flag or a static boolean literal (langtag={true}/langtag={false}) - not a variable or expression. Static output can't pull in LangTag's styles (that only happens when a runtime LangTag is in the bundle), so a truthy langtag renders the language badge as a plain inline-styled <span> instead, themed with the same --langtag-* custom properties LangTag reads - customization is limited to those variables, not LangTag's slot content.
  • label, if set, is a string literal. Static output can't read a parent CodeWindow, so it uses label or "Code".

Dynamic code or language, loadLanguage, HighlightAuto, HighlightSvelte, and HighlightEditable keep the runtime component. No warning. That's intentional.

If a usage looks static but still fails (language module missing, highlight.js throws), it falls back to the runtime component and calls onWarn. Usually that's a typo or a resolution quirk, not a dynamic usage you meant to keep:

highlightStatic({
  onWarn(message, { filename, line, cause }) {
    // defaults to `console.warn(`[svelte-highlight/static] ${filename}:${line} - ${message}`)`
  },
});

To measure how many <Highlight> usages a build actually converted to static markup, pass onSummary. It fires once per file with at least one shape-matched usage (files with none don't trigger it), so summing across calls gives a build-wide total:

let matched = 0;
let succeeded = 0;

highlightStatic({
  onSummary(summary) {
    matched += summary.matched;
    succeeded += summary.succeeded;
  },
});

Scope is small on purpose. Extra props on <Highlight> don't carry over to the emitted <pre>. Unused Highlight and language imports are left in place; bundlers drop them (sideEffects in package.json is narrow enough).

Rendering Markdown/MDX fences

highlightStatic only transforms literal <Highlight> usages in .svelte files. Markdown/MDX/mdsvex code fences never go through the Svelte compiler at all, so svelte-highlight/fence provides a separate, framework-agnostic building block: parseMeta(meta) and highlightFence({ code, lang, meta }), a plain async function with zero Svelte dependency.

import { highlightFence } from "svelte-highlight/fence";

const html = await highlightFence({
  code: 'const add = (a, b) => a + b;\nconst sub = (a, b) => a - b;\nexport { add, sub };',
  lang: "typescript",
  meta: '{1,3-5} title="app.ts"',
});

lang accepts either the grammar's canonical file name or a known alias (e.g. "ts", "sh") -- highlightFence resolves it internally via resolveLanguageName, and rejects with LanguageLoadError when neither resolves. meta is the Expressive Code/Shiki-style vocabulary standardized on by tools like Astro, Starlight, and rehype-pretty-code: a bare {1,3-5} or mark={1,3-5} marks lines, ins={...}/del={...} mark insertions/deletions, title="..." sets a title, and showLineNumbers is a bare flag. The output wraps each line in <span class="line" data-line-state="mark|ins|del"> (only when a state applies) inside a <pre class="hljs" data-language="...">, so a mark/ins/del-aware stylesheet can target [data-line-state] the same way it would target Expressive Code or Shiki output.

Call resolveLanguageName directly when you need the canonical name ahead of highlightFence -- e.g. to pick a display label, or to call loadLanguage/<Highlight> yourself instead:

import { resolveLanguageName } from "svelte-highlight/fence";

resolveLanguageName("ts"); // "typescript"
resolveLanguageName("nope"); // undefined

It trims, lowercases, and reads only the first word, so passing a whole info string (resolveLanguageName("ts title=\"app.ts\"")) works too. The alias table it reads from is also importable directly, as svelte-highlight/languages/aliases (LANGUAGE_ALIASES: Readonly<Record<string, string>>), for consumers that want the raw alias -> canonical mapping without going through resolveLanguageName.

mdsvex. Wire highlightFence into highlight.highlighter, whose signature is (code, lang, meta) => string | Promise<string>:

// mdsvex.config.js
import { highlightFence } from "svelte-highlight/fence";

/** @type {import("mdsvex").MdsvexOptions} */
const config = {
  highlight: {
    highlighter: (code, lang, meta) => highlightFence({ code, lang, meta }),
  },
};

export default config;

markdown-it. Wire it into the highlight constructor option, whose signature is (str, lang, attrs) => string — markdown-it inserts the returned string verbatim (no further escaping), and highlight isn't allowed to be async, so pre-render fences you know about or fall back to a sync path for the rest:

import MarkdownIt from "markdown-it";
import { highlightFence } from "svelte-highlight/fence";

const md = new MarkdownIt({
  highlight(str, lang, attrs) {
    // highlightFence is async; markdown-it's highlight isn't, so either
    // pre-highlight fences in a separate async pass before render() and
    // look the result up here, or fall back to escaped plain text.
    return md.utils.escapeHtml(str);
  },
});

rehype. rehype/unist-util-visit are the consumer's dependencies, not this package's. A rehype plugin walks the hast tree after remark-rehype has already converted fenced code to <pre><code class="language-*"> — the fence's meta string isn't part of that hast by default, only lang (via the language-* class), so pass meta through only if an earlier remark step attached it (e.g. node.data.hProperties["data-meta"] = mdastNode.meta):

import { visit } from "unist-util-visit";
import { highlightFence } from "svelte-highlight/fence";

function rehypeHighlightFence() {
  return async (tree) => {
    const nodes = [];
    visit(tree, "element", (node, index, parent) => {
      if (node.tagName !== "pre" || parent === undefined || index === undefined) return;
      const code = node.children.find((child) => child.tagName === "code");
      if (code) nodes.push({ node, index, parent, code });
    });

    for (const { node, index, parent, code } of nodes) {
      const lang = code.properties?.className
        ?.map(String)
        .find((c) => c.startsWith("language-"))
        ?.slice("language-".length);
      if (!lang) continue;

      const html = await highlightFence({
        code: code.children.map((c) => (c.type === "text" ? c.value : "")).join(""),
        lang,
        meta: code.properties?.["data-meta"],
      });
      parent.children[index] = { type: "raw", value: html };
    }
  };
}

Action

Use the highlight action to highlight existing <pre><code> markup in place. This is useful for progressively enhancing server-rendered content (e.g. markdown) without swapping in a component.

Place use:highlight on the <code> element inside a <pre>, matching the theme CSS's .hljs code selector -- elsewhere (directly on <pre>, or a non-<pre><code> element) it still highlights but may not be visually targeted by the active theme.

The action accepts the same language prop as the components. When code is omitted, the element's existing textContent is highlighted. Updating code re-highlights the element.

<script>
  import { highlight } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<svelte:head>
  {@html github}
</svelte:head>

<pre><code use:highlight={{ language: typescript, code }}></code></pre>

Omit code to highlight the element's existing contents:

<pre><code use:highlight={{ language: typescript }}
  >const add = (a, b) => a + b;</code
></pre>

language is also optional: when omitted, the action reads a language-xxx class off its own node (the Prism/highlight.js Markdown convention) and resolves it via loadLanguage, so server-rendered Markdown output needs no per-block language prop. With no matching class, it warns in dev and dispatches error.

<pre><code class="language-typescript" use:highlight>const add = (a, b) => a + b;</code></pre>

Dispatched Events

The action dispatches highlighted (detail: { html, language }) on success and error (detail: { error }) on failure, on the node it's placed on.

<pre><code
  use:highlight={{ language: typescript, code }}
  on:highlighted={(e) => console.log(e.detail.html, e.detail.language)}
  on:error={(e) => console.error(e.detail.error)}
></code></pre>

highlighted is also a composition hook for a sibling LineNumbers: on:highlighted={(e) => (highlighted = e.detail.html)} feeding <LineNumbers {highlighted} languageName={typescript.name} />.

Without Svelte

svelte-highlight/dom runs the action on plain pages. highlightAll highlights every pre code[class*="language-"] in place and loads each block's grammar from its class. ready settles once every block is done, and destroy puts the original text back.

<pre><code class="language-typescript">const add = (a, b) => a + b;</code></pre>
import "svelte-highlight/styles/github.css";
import { highlightAll } from "svelte-highlight/dom";

const { ready } = highlightAll();
await ready; // { highlighted: 1, failed: 0 }

Pass a root element to limit the search, and { selector, language } to pick other elements or skip the class lookup. highlight(element, { language, code }) from the same entry highlights a single element and returns { update, destroy }.

paintHighlights from the same entry is the "css-highlights" engine without a component. The text stays a single text node and the browser paints each token through the CSS Custom Highlight API. It adds the ::highlight() rules in a <style> and needs the theme for them, since those rules can't come from the page's theme CSS. Without a theme, or where CSS.highlights is missing, it renders <span>s like the action. engine says which one painted.

import { paintHighlights } from "svelte-highlight/dom";
import typescript from "svelte-highlight/languages/typescript";
import github from "svelte-highlight/themes/github";

const painted = paintHighlights(document.querySelector("pre code"), {
  language: typescript,
  theme: github,
});
painted.engine; // "css-highlights"
painted.update({ code: "let total = 1;" });
painted.destroy(); // removes the highlights and restores the text

Without a bundler, an import map points the package at a CDN, and the same imports work in a <script type="module">. Browsers don't read the package's exports, so add the .js extension (svelte-highlight/dom.js). See examples/cdn.

Web component

defineHighlightElement from svelte-highlight/element defines <shl-code>, which highlights its text in the language its language attribute names. It renders <pre><code class="hljs"> inside itself, so theme CSS applies. It works on plain pages and in any framework.

<shl-code language="typescript">const add = (a, b) => a + b;</shl-code>
import { defineHighlightElement } from "svelte-highlight/element";

defineHighlightElement();

Changing language, or setting the code property, highlights again. Set code for code with < or & in it, since the element's text is parsed as HTML. The element dispatches highlighted ({ html, language }) or error ({ error }). Pass a name, as in defineHighlightElement("my-code"), to use another tag.

Code Window

Wrap a code block in CodeWindow to frame it with window chrome. It's purely cosmetic; the default slot renders your content unchanged.

Use the variant prop to choose the chrome style: "macos" (default) renders traffic-light dots, "terminal" renders a prompt, and "plain" renders just the title bar. The optional title is shown in the title bar, and is also exposed as a native tooltip via the HTML title attribute so a truncated title is readable on hover or focus.

<script>
  import Highlight, { CodeWindow } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<svelte:head>
  {@html github}
</svelte:head>

<CodeWindow variant="macos" title="example.ts">
  <Highlight language={typescript} {code} />
</CodeWindow>

Terminal output

Pair variant="terminal" with a shell language to frame command-line output:

<script>
  import Highlight, { CodeWindow } from "svelte-highlight";
  import bash from "svelte-highlight/languages/bash";
  import nord from "svelte-highlight/styles/nord";

  const code = `$ npm install svelte-highlight

added 1 package in 1.2s`;
</script>

<svelte:head>
  {@html nord}
</svelte:head>

<CodeWindow variant="terminal" title="bash">
  <Highlight language={bash} {code} />
</CodeWindow>

Theming the chrome

The title bar is a three-column flex row (leading chrome, title, a mirrored trailing spacer), so the title stays centered in the space actually left over without overlapping the dots or prompt even at narrow widths -- this doesn't apply to plain, which has no leading chrome to mirror.

Every part of the chrome is driven by CSS variables (see CodeWindow variables). Pass them as style props to restyle the window without :global overrides. For example, give the default square window rounded corners with --window-radius:

<CodeWindow
  variant="macos"
  title="example.ts"
  --window-radius="12px"
  --window-background="#0d1117"
  --window-border="1px solid #30363d"
  --titlebar-background="#161b22"
  --titlebar-color="#8b949e"
>
  <Highlight language={typescript} {code} />
</CodeWindow>

The macOS traffic-light dots are themable too. Recolor them, or mute them to a single neutral color:

<CodeWindow
  variant="macos"
  title="example.ts"
  --dot-close="#888"
  --dot-minimize="#888"
  --dot-maximize="#888"
>
  <Highlight language={typescript} {code} />
</CodeWindow>

The plain variant drops the dots and prompt for a minimal titled bar:

<CodeWindow variant="plain" title="example.ts">
  <Highlight language={typescript} {code} />
</CodeWindow>

Composing with other components

CodeWindow only frames its slot, so it composes with the rest of the library. Add LineNumbers inside it:

<script>
  import Highlight, { CodeWindow, LineNumbers } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  const code = `const add = (a, b) => a + b;
const sub = (a, b) => a - b;`;
</script>

<svelte:head>
  {@html github}
</svelte:head>

<CodeWindow variant="macos" title="math.ts">
  <Highlight language={typescript} {code} let:highlighted>
    <LineNumbers {highlighted} hideBorder />
  </Highlight>
</CodeWindow>

Or layer a CopyButton over it by wrapping both in a relatively-positioned container:

<div style="position: relative">
  <CodeWindow variant="macos" title="example.ts">
    <Highlight language={typescript} {code} />
  </CodeWindow>
  <!-- Offset the button so it clears the title bar. -->
  <CopyButton {code} --copy-top="3em" />
</div>

Put a FileTabs strip in the titlebar slot to switch between files without leaving the title bar:

<script>
  import Highlight, { CodeWindow, FileTabs } from "svelte-highlight";
  import javascript from "svelte-highlight/languages/javascript";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  const sources = {
    "App.svelte": { language: typescript, code: "const answer = 42;" },
    "index.js": { language: javascript, code: "export default answer;" },
  };

  const files = Object.keys(sources);
  let active = files[0];
</script>

<svelte:head>
  {@html github}
</svelte:head>

<CodeWindow variant="macos">
  <FileTabs {files} bind:active slot="titlebar" />
  <Highlight language={sources[active].language} code={sources[active].code} />
</CodeWindow>

Code Toolbar

CodeToolbar adds a bar above the code with a file name, a language badge and buttons. Put it first in Highlight's default slot.

<script>
  import Highlight, {
    CodeToolbar,
    CopyButton,
    LineNumbers,
    WrapToggle,
  } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  export let code;

  let wrap = false;
</script>

<svelte:head>
  {@html github}
</svelte:head>

<div class="code-block">
  <Highlight {code} language={typescript} {wrap} let:highlighted let:languageName>
    <CodeToolbar {languageName} title="app.ts" label="app.ts actions">
      <WrapToggle bind:wrap />
      <CopyButton {code} />
    </CodeToolbar>
    <LineNumbers {highlighted} wrapLines={wrap} style="max-height: 24rem" />
  </Highlight>
</div>

<style>
  .code-block {
    border-radius: 8px;
    overflow: clip;
  }
</style>
  • Theme colors: the toolbar has the hljs class, so it takes its background and text color from your theme. A faint tint and a divider set it apart from the code. With no theme, it uses the system Canvas colors.
  • Scoped themes: if you scope a theme to a class, put that class on an element that wraps the toolbar, such as .code-block above. A global theme works as is.
  • Scrolling: put the max-height on LineNumbers, not on a wrapper. Then only the code scrolls, and the scrollbar starts below the toolbar. overflow: clip on the wrapper rounds the corners without making it scroll.
  • Sticky: if a wrapper or the page scrolls instead, the toolbar sticks to its top while the block is on screen. Set sticky={false} to turn it off.
  • Buttons: inside CodeToolbar, CopyButton sits in the bar, and WrapToggle and CopyButton share one icon-button style.
  • Accessibility: the root is a role="toolbar" element. Set label to give it a name. The buttons are ordinary tab stops, as in the lightweight WAI-ARIA toolbar pattern.

Custom actions

Use ToolbarButton for your own actions. It matches the built-in buttons. Give it a label for the tooltip and screen readers, and an icon in its slot. Set href for a link, or pressed for a toggle.

<script>
  import { ToolbarButton } from "svelte-highlight";

  function download() {
    const url = URL.createObjectURL(new Blob([code], { type: "text/plain" }));
    Object.assign(document.createElement("a"), { href: url, download: "app.ts" }).click();
    URL.revokeObjectURL(url);
  }
</script>

<CodeToolbar {languageName} title="app.ts">
  <ToolbarButton label="Download" on:click={download}>
    <svg aria-hidden="true"><!-- icon --></svg>
  </ToolbarButton>
  <ToolbarButton label="Open in playground" href={playgroundUrl} target="_blank" rel="noopener">
    <svg aria-hidden="true"><!-- icon --></svg>
  </ToolbarButton>
  <WrapToggle bind:wrap />
  <CopyButton {code} />
</CodeToolbar>

Use the start slot for content after the badge, such as a status or a branch name.

Composing with CodeWindow

In a CodeWindow, the window title already names the file. Leave out the toolbar's title, so the name doesn't show twice:

<CodeWindow variant="macos" title="app.ts">
  <Highlight {code} language={typescript} {wrap} let:highlighted let:languageName>
    <CodeToolbar {languageName}>
      <WrapToggle bind:wrap />
      <CopyButton {code} />
    </CodeToolbar>
    <LineNumbers {highlighted} wrapLines={wrap} />
  </Highlight>
</CodeWindow>

Animation

Use Typewriter inside Highlight's default slot with the highlighted prop. It prints the code one character at a time, syntax highlighting included. A blinking caret marks the end of the typed text and hides when typing stops.

Unrevealed text stays in the layout but invisible, so the block is full height from the start. Content below won't jump as characters appear. You don't need a min-height hack.

<script>
  import Highlight, { Typewriter } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  const code = "const add = (a: number, b: number) => a + b;";
</script>

<svelte:head>
  {@html github}
</svelte:head>

<Highlight language={typescript} {code} let:highlighted>
  <Typewriter {highlighted} />
</Highlight>

Set speed (milliseconds per character) and pause with play. Turn play back on to pick up where you left off. A new highlighted value starts over from the first character. Fire on:done when the last character is visible.

<Highlight language={typescript} {code} let:highlighted>
  <Typewriter
    {highlighted}
    speed={50}
    {play}
    on:done={() => console.log("revealed")}
  />
</Highlight>

Control the pacing curve with easing: a function mapping elapsed-time fraction (0-1) to revealed-fraction (0-1). It defaults to linear; import a named curve or pass your own. Total typing duration is always speed × character count, no matter the curve -- only the pacing within that duration changes.

<script>
  import Highlight, { Typewriter, easeOutCubic } from "svelte-highlight";
</script>

<Highlight language={typescript} {code} let:highlighted>
  <Typewriter {highlighted} easing={easeOutCubic} />
</Highlight>

Named curves: linear (default), easeInQuad/easeOutQuad/easeInOutQuad, easeInCubic/easeOutCubic/easeInOutCubic, easeInSine/easeOutSine/easeInOutSine.

Set speed={0} to reveal all content on the very first frame -- a documented "skip typing" escape hatch, useful for a "skip" button or for finishing an off-screen instance instantly.

By default granularity="char" reveals one character at a time. Set granularity="word" to reveal a full word per step instead -- a ChatGPT-style token-by-token reveal. Total typing duration and easing are unaffected either way; only which character counts revealed may land on changes.

<Highlight language={typescript} {code} let:highlighted>
  <Typewriter {highlighted} granularity="word" />
</Highlight>

Fire on:progress for a progress bar or "X% typed" indicator -- it dispatches { revealed, total } (visible-character counts) whenever revealed advances. revealed and total are also exposed for bind:revealed/bind:total, though both are read-only in practice: the component overwrites them every frame.

<Highlight language={typescript} {code} let:highlighted>
  <Typewriter
    {highlighted}
    bind:revealed
    bind:total
    on:progress={(e) => console.log(`${e.detail.revealed}/${e.detail.total}`)}
  />
</Highlight>

Typewriter doesn't gate itself on prefers-reduced-motion -- if you want to honor it, check matchMedia("(prefers-reduced-motion: reduce)") yourself and skip rendering Typewriter (or set a very low speed). The caret stops blinking under prefers-reduced-motion: reduce. Customize the caret with --caret-width, --caret-height, --caret-gap, --caret-color, and --caret-blink.

Screen readers get the full text at once from a visually-hidden copy. The animated layer is aria-hidden, so they don't read a half-typed block.

Each tick reveals one already-rendered unit rather than re-rendering the block, so animating stays smooth up to tens of thousands of characters; past that it falls back to the coarser whole-block reveal.

Streaming

Rendering LLM chat output is the dominant new syntax-highlighting use case: code arrives in arbitrary-sized chunks, mid-token and mid-line, and you don't know the full content up front. HighlightStream is built for that -- unlike Typewriter, which animates a complete, already-highlighted string and restarts whenever it changes, HighlightStream accepts a growing code buffer and re-highlights it as chunks arrive.

Append to code as chunks come in; set done once the stream ends.

<script>
  import { HighlightStream } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  let code = "";
  let done = false;

  // Wire this up to your streaming source (fetch, WebSocket, SSE, ...).
  async function stream() {
    for (const chunk of chunks) {
      code += chunk;
      await new Promise((r) => setTimeout(r, 30));
    }
    done = true;
  }
</script>

<svelte:head>
  {@html github}
</svelte:head>

<HighlightStream language={typescript} {code} {done} />

Multiple chunks appended within the same animation frame coalesce into a single highlight pass. Already-rendered lines are diffed and left untouched in the DOM; only lines whose content actually changed are repainted, so a fast-scrolling response only touches its changed suffix (typically the last line). A multi-line construct left open mid-stream -- an unterminated template literal or block comment -- re-tokenizes the lines it spans once the closing delimiter arrives, with no special-casing needed: it falls out of re-highlighting the full buffer on every update.

code doesn't have to only grow by appending -- an edit to already-streamed text (an LLM regenerating an earlier paragraph, say) is handled incrementally too, via StreamSession#replace() under the hood, instead of discarding the session and re-tokenizing the whole buffer from scratch.

A blinking caret marks the end of output while !done; setting done hides it and performs one final full highlight, so the finished output matches what Highlight would render for the same code. on:done fires right after that final highlight. Customize the caret with the same --caret-width, --caret-height, --caret-gap, --caret-color, and --caret-blink variables as Typewriter.

A blinking caret is a purely visual cue, so the root element also carries aria-busy while !done, and a visually-hidden, polite live region announces doneText (default "Code finished streaming") once done flips to true -- screen reader users get the same "still streaming" / "finished" signal sighted users get from the caret. Set doneText to "" to disable the announcement.

Set autoScroll to keep the container pinned to the bottom as output grows -- it stops auto-scrolling as soon as you scroll up, and resumes once you scroll back to the bottom.

<HighlightStream language={typescript} {code} {done} autoScroll style="max-height: 20em; overflow-y: auto;" />

Per-chunk work is O(tail), not O(stream length so far): finished output is sealed into immutable chunks the DOM never re-diffs, so a response that's ten times longer doesn't cost ten times more per repaint. DOM updates stay proportional to the changed lines -- fine for chat-sized output up to very long responses, but every line ever streamed still stays in the DOM; for a stream that runs long enough to scroll through, set virtualize to bound that too.

<HighlightStream language={typescript} {code} {done} virtualize style="height: 20em;" />

virtualize renders only the lines within the scrolled viewport (plus overscan), the same windowing HighlightVirtual does for static documents -- a stream that runs to tens of thousands of lines still costs a couple dozen DOM nodes. It swaps the sealed-chunk session for TokenizedDocument (see Large documents below), so output always reflects the streaming (non-canonicalized) parse, even once done -- unlike the default mode, which upgrades to a canonical final render. on:highlight isn't dispatched in this mode, since materializing the full HTML on every repaint would defeat the point of windowing; on:done, the caret, and autoScroll all keep working. on:windowchange fires with { start, end, lineCount } whenever the rendered window moves, so you can show something like "lines N-M of T" without counting DOM nodes yourself. Row math assumes one line per fixed-height row, so virtualize can't wrap lines -- it always renders with white-space: pre, overriding any white-space you pass through $$restProps. bind:this, then call scrollToLine(line) to scroll a given line into view -- works in both virtualize and default modes.

Streaming without Svelte

createStreamView from svelte-highlight/stream runs the same rendering on any element, with no framework. Use a <code> inside a <pre>, and import svelte-highlight/stream.css for the caret. It takes the same settings as the component, minus virtualize, and renders the first frame at once.

import "svelte-highlight/stream.css";
import { createStreamView } from "svelte-highlight/stream";
import typescript from "svelte-highlight/languages/typescript";

const view = createStreamView(document.querySelector("pre code"), {
  language: typescript,
  autoScroll: true,
  onDone: () => console.log(view.highlighted),
});

let code = "";
for await (const chunk of response) {
  code += chunk;
  view.update({ code });
}
view.update({ done: true });
view.destroy();

aria-busy and the "finished" announcement are up to you here. Set aria-busy on the <pre> while streaming, and clear it in onDone.

Streaming Markdown with multiple fences

HighlightStream highlights a single growing code buffer, but LLM chat output is Markdown: prose interleaved with several fenced code blocks, each in its own language. svelte-highlight/fence's createFenceSplitter (see "Rendering Markdown/MDX fences" above) turns that growing Markdown string into prose and fence segments, so you can drive one HighlightStream per fence as it streams in:

<script>
  import { HighlightStream, loadLanguage } from "svelte-highlight";
  import { createFenceSplitter } from "svelte-highlight/fence";
  import github from "svelte-highlight/styles/github";

  const splitter = createFenceSplitter();
  let segments = splitter.segments();
  let done = false;

  // Wire this up to your streaming source (fetch, WebSocket, SSE, ...).
  async function stream() {
    for (const chunk of chunks) {
      splitter.append(chunk);
      segments = splitter.segments();
      await new Promise((r) => setTimeout(r, 30));
    }
    done = true;
  }
</script>

<svelte:head>
  {@html github}
</svelte:head>

{#each segments as segment (segment.id)}
  {#if segment.kind === "text"}
    <p style="white-space: pre-wrap">{segment.text}</p>
  {:else}
    {#await loadLanguage(segment.lang ?? "plaintext") then grammar}
      <HighlightStream language={grammar} code={segment.code} done={!segment.open} />
    {/await}
  {/if}
{/each}

Every segment's id is stable across both append (growing the buffer) and a full set (a regenerated buffer, keyed by common prefix) for everything before the first change, so the keyed {#each} never re-mounts a fence's HighlightStream just because more text arrived elsewhere in the document -- only the segment that actually changed gets a new instance.

MarkdownStream

MarkdownStream wraps the pattern above: pass it a growing Markdown text buffer and it drives one HighlightStream per fence for you, keyed by the splitter's stable ids. Prose renders as plain text by default -- it's not a Markdown renderer, just a router for fenced code.

<script>
  import { MarkdownStream } from "svelte-highlight";
  import github from "svelte-highlight/styles/github";

  let text = "";
  let done = false;

  // Wire this up to your streaming source (fetch, WebSocket, SSE, ...).
  async function stream() {
    for (const chunk of chunks) {
      text += chunk;
      await new Promise((r) => setTimeout(r, 30));
    }
    done = true;
  }
</script>

<svelte:head>
  {@html github}
</svelte:head>

<MarkdownStream {text} {done} />

done is forwarded to every fence's HighlightStream as done || !segment.open, so a trailing fence still open when the overall stream ends is treated as closed. The caret only ever renders on the last open fence, and doneText's completion announcement fires once, from the last fence, rather than once per fence.

Override the text slot to render real Markdown prose (headings, lists, inline code) with a library of your choice, leaving fences to MarkdownStream:

<script>
  import { MarkdownStream } from "svelte-highlight";
  import { renderInlineMarkdown } from "./your-markdown-renderer.js";
</script>

<MarkdownStream {text} {done}>
  <div slot="text" let:segment>{@html renderInlineMarkdown(segment.text)}</div>
</MarkdownStream>

By default, resolveLanguage dynamically imports one grammar per language seen via loadLanguage, falling back to plaintext while loading and for an unrecognized fence language. Pass your own resolveLanguage to restrict resolution to a fixed set of languages -- for example, everything else falls back to plaintext instead of fetching an arbitrary grammar chunk:

<script>
  import { MarkdownStream, loadLanguage } from "svelte-highlight";

  const ALLOWED = new Set(["typescript", "javascript", "bash", "json"]);

  function resolveLanguage(lang) {
    return lang && ALLOWED.has(lang) ? loadLanguage(lang) : undefined;
  }
</script>

<MarkdownStream {text} {done} {resolveLanguage} />

That still dynamically imports each allowed language's grammar on first use. For an app that wants no dynamic chunks at all -- everything bundled up front, same tradeoff as Loading a language by name -- map the allowlist to statically imported grammars instead:

<script>
  import { MarkdownStream } from "svelte-highlight";
  import bash from "svelte-highlight/languages/bash";
  import javascript from "svelte-highlight/languages/javascript";
  import json from "svelte-highlight/languages/json";
  import typescript from "svelte-highlight/languages/typescript";

  const GRAMMARS = { typescript, javascript, bash, json };

  function resolveLanguage(lang) {
    return lang ? GRAMMARS[lang] : undefined;
  }
</script>

<MarkdownStream {text} {done} {resolveLanguage} />

See Code-splitting for the tradeoffs between a static import (bundled up front, no request-time latency) and loadLanguage (smaller initial bundle, one chunk fetched per language actually seen).

A resolver that returns the grammar directly, as above, highlights each fence on the first render, so a server-rendered page sends the fences highlighted. A promise, such as loadLanguage's, can't be awaited by the server: those fences render as plain text until the grammar loads in the browser.

Large documents

HighlightVirtual renders only the visible lines of a huge document (plus a small overscan margin) inside its own scroll container -- a 100k-line file costs a couple dozen DOM line nodes, not one per line. It exploits the same engine checkpoint/resume primitive HighlightStream uses for streaming, but for random-access windows into a static document instead of a growing tail.

<script>
  import { HighlightVirtual } from "svelte-highlight";
  import json from "svelte-highlight/languages/json";
  import atomOneDark from "svelte-highlight/styles/atom-one-dark";
</script>

<svelte:head>
  {@html atomOneDark}
</svelte:head>

<HighlightVirtual language={json} code={hugeLogDump} style="height: 480px" />

It's tested up to 500,000 lines (about 24 MB of TypeScript). The first paint takes milliseconds at any size. Tokenizing is lazy, so a first jump far ahead tokenizes everything before it: about 0.9 s to the end of 100k lines of TypeScript and 3.8 s for 500k, in Chrome on an M4 Max. After that, jumps anywhere take a few milliseconds. Add tokenizeAhead (<HighlightVirtual tokenizeAhead ... />) to tokenize the rest of the document in idle time after the first paint, a few milliseconds per slice, so far jumps are fast from the start. It's off by default because it retains memory for the whole document up front, about 117 MB per 100k lines of TypeScript today. on:tokenize reports its progress as { through, lineCount }. The large files page lets you try it.

The rendered <pre> is the scroll container itself -- size it with style/class/$$restProps, same as Highlight, but white-space can't be overridden this way: it's pinned to pre because the windowing math depends on a uniform line height. Content doesn't wrap (uniform line height is a v1 constraint, measured once from a rendered probe line). overscan (default 12) controls how many extra lines render above/below the viewport; checkpointInterval (default 100) controls how often the engine snapshots its parse state, trading a little memory for cheaper random access. Concretely, memory scales with document lines / checkpointInterval retained checkpoints, each holding a full engine snapshot plus open-scope/pending-HTML state -- and checkpoints are never evicted: they're retained for the entire lifetime of the TokenizedDocument instance, across any number of append() calls, until a setCode() call with unrelated content triggers a full reset. This is fine for the target "huge static document" use case, since checkpoints are bounded by that one document's size, but worth knowing for a long-lived instance that's repeatedly setCode()-reset with genuinely new documents rather than recreated. A consumer worried about retained memory can call doc.checkpointCount() directly rather than estimating it from lineCount()/checkpointInterval. Server-rendered output is the whole document: the first 100 lines highlighted and the rest as plain escaped text, so the server's cost doesn't grow with the document. The first client render is the same, so hydration keeps it, and the windowed view takes over on mount. A page that opens at the top shows highlighted code from the first paint; one that opens scrolled past line 100 shows plain text there until hydration. The container already needs an explicit size (style/class/$$restProps, above); reserving that same min-height/height up front also prevents layout shift when the windowed view takes over.

on:windowchange fires with { start, end, lineCount } whenever the rendered window moves, once the new rows are in the DOM, so you can show something like "lines N-M of T" or paint into the rows (as highlightMatches does) without counting DOM nodes yourself. bind:this, then call scrollToLine(line, { align }) to scroll a given line into view -- useful for a "jump to line" control -- without guessing at the line height yourself.

For custom virtualization, servers, or tests, svelte-highlight/tokenized-document exposes the same windowing primitive headlessly:

import { createTokenizedDocument } from "svelte-highlight/tokenized-document";
import typescript from "svelte-highlight/languages/typescript";

const doc = createTokenizedDocument({ language: typescript });
doc.setCode(bigFile);
doc.lineCount(); // cheap -- never triggers tokenization
doc.lineRange(50_000, 50_040); // 40 highlighted lines in O(window + interval)
doc.append(moreCode); // streaming growth, no re-tokenization

Typewriter's tokenizer is available the same way, for a custom reveal UI or non-DOM use: svelte-highlight/typewriter-units exposes tokenizeTypewriter, buildUnitMarkup, createTypewriterSplitter, and computeWordBoundaries headlessly.

import {
  computeWordBoundaries,
  tokenizeTypewriter,
} from "svelte-highlight/typewriter-units";

const units = tokenizeTypewriter(highlighted);
const wordBoundaries = computeWordBoundaries(units); // for word-by-word reveal

lineRange tokenizes lazily and caches the most recently resolved window, so scrolling through even a huge document only ever pays for the lines actually requested. Its output matches HighlightStream's live (non-canonicalized) parse: constructs needing multi-line lookahead across a window's edge can render slightly differently than registry.highlight()'s canonical output, the same tradeoff streaming already makes. TokenizedDocument doesn't support mid-document edits -- append and full setCode resets only. For a bounded, editable document (not a windowed/virtualized one), HighlightEditable solves mid-document editing, built on its own checkpoint-resume re-tokenizer (src/incremental-tokenize.js, not itself a public export). The two aren't unified today -- there's no single primitive yet for a document that's both editable and virtualized.

Without virtualization

For documents in the low thousands to tens of thousands of lines (roughly 2,000-20,000), HighlightVirtual's hard constraints -- no wrapping, plain text past the first 100 lines until hydration, a fixed-height v1 container -- may not be worth taking on. The CSS content-visibility: auto property gets you a native-scrolling, cheap-to-render alternative built entirely from already-public APIs, at the cost of the DOM-node count HighlightVirtual avoids:

<script>
  import { createRegistry, registerAll, tokenLines } from "svelte-highlight/engine";
  import typescript from "svelte-highlight/languages/typescript";

  const registry = createRegistry();
  registerAll(registry, typescript);

  export let code;
  const { events } = registry.highlight(code, { language: "typescript" });
  const lines = tokenLines(events); // [[{ text, scopes }, …], …]

  function scopeClass(scope) {
    return scope
      .split(".")
      .map((piece) => `hljs-${piece}`)
      .join(" ");
  }
</script>

<div class="hljs" style="content-visibility: auto; contain-intrinsic-size: auto 1000px;">
  {#each lines as line}
    <div style="white-space: pre; content-visibility: auto; contain-intrinsic-size: auto 20px;">
      {#each line as token}<span class={token.scopes.map(scopeClass).join(" ")}>{token.text}</span>{/each}
    </div>
  {/each}
</div>

Each line is its own block-level <div>, not a row in a <table>/<tr> (the way LineNumbers renders) -- content-visibility doesn't apply to table-internal display types, so off-screen table rows never get the skip. Off-screen <div> lines do: the browser skips layout/paint/style for them, using contain-intrinsic-size as a placeholder size until they scroll near the viewport. Unlike HighlightVirtual, this is a progressive enhancement, not a hard requirement -- browsers without content-visibility support just render every line normally, and you get native text selection, find-in-page, and line wrapping for free (drop the white-space: pre on each line's <div> to allow it).

Find in document

Browser Cmd+F can't see rows a virtualized view (HighlightVirtual, HighlightStream's virtualize mode) hasn't mounted, and neither of them ships a search box -- svelte-highlight/search is a headless find-in-document engine plus a DOM-painting helper, so any consumer can build one. No <input> component is exported; that's application UI, not a library primitive.

createSearch(source) accepts a plain string (split on "\n"), a string[] of lines, or a TokenizedDocument (duck-typed -- only lineCount/lineRange/textRange are used, never checked via instanceof; plain text comes from textRange when present, so searching never tokenizes the document), such as the one backing a HighlightStream in virtualize mode. Against a TokenizedDocument, repeated query() calls with unchanged text/options only rescan lines appended since the last call, instead of rescanning the whole document every time:

<script>
  import { HighlightVirtual } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import { createSearch, highlightMatches } from "svelte-highlight/search";
  import "svelte-highlight/search.css";

  export let code;
  const search = createSearch(code);

  /** @type {HighlightVirtual} */
  let ref;
  /** @type {HTMLElement} */
  let root;
  let dispose = () => {};

  function repaint() {
    dispose();
    dispose = highlightMatches(root, search.matches(), {
      current: search.current()?.index,
    }).dispose;
  }

  const unsubscribe = search.onChange(repaint);

  function onInput(event) {
    search.query(event.currentTarget.value, { wholeWord: true });
  }

  function next() {
    const match = search.next();
    if (match) ref.scrollToLine(match.line, { align: "center" });
  }
</script>

<input on:input={onInput} />
<button on:click={next}>Next ({search.count()})</button>

<div bind:this={root}>
  <HighlightVirtual bind:this={ref} language={typescript} {code} on:windowchange={repaint} />
</div>

query(text, options) runs a search; options.caseSensitive and options.wholeWord (wraps the pattern in \b...\b) both default to false. options.regex treats text as a regular expression source instead of literal text -- capped at 256 characters and compiled in a try/catch, so an over-long or invalid pattern yields zero matches and sets error() rather than throwing. Empty text clears matches without an error. matches() returns every { line, start, end } match in document order (never spanning a line); count() is its length. current()/next()/prev() return the selected match plus its index into matches(), wrapping around the ends; all return undefined before any query or with zero matches. setSource(source) swaps the document and re-runs an already-active query as a full rescan. onChange(callback) fires after every mutating call and returns an unsubscribe function.

highlightMatches(root, matches, { current, name }) paints matches into root, restricted to whatever rows are currently rendered there -- resolved per match's line via [data-line] (the row shape HighlightVirtual/HighlightStream render), then the line-th .line element (the shape highlightFence/fenced code renders), then the whole <code> split on "\n"; matches whose line resolves to nothing are skipped. It uses the CSS Custom Highlight API when available (two Highlights: name, defaulting to "shl-search", and ${name}-current for the match at index current), or falls back to wrapping text in <mark data-shl-search> (plus data-shl-search-current) when it isn't. It paints once per call and returns { dispose() } -- there's no diffing against a previous call, so a consumer disposes and re-runs it after the rendered window changes (on:windowchange) or the query changes (onChange), as in the example above. It's a no-op on the server. import "svelte-highlight/search.css" is optional and styles both highlight names via the --search-match-background/--search-current-background CSS variables; passing a custom name bypasses it, since the stylesheet only targets the default names.

Diffs

HighlightDiff shows a syntax-highlighted diff of two texts. It highlights each side as a whole file, not hunk by hunk. So a string or comment that opens far above a change still highlights correctly inside it.

<script>
  import { HighlightDiff } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";
  import atomOneDark from "svelte-highlight/styles/atom-one-dark";

  export let before;
  export let after;
</script>

<svelte:head>
  {@html atomOneDark}
</svelte:head>

<HighlightDiff {before} {after} language={typescript} style="height: 480px" />

Try every feature on the diff preview page.

Views, folds, and word diffs

  • view is "unified" (default) or "split". It supports bind:view; the v key toggles it.
  • context (default 3) sets how many unchanged lines stay around each change. Longer unchanged runs fold into a row you can click to expand.
  • wordDiff (default true) marks the changed words inside paired lines.
  • ignoreWhitespace treats lines that differ only in whitespace as unchanged.
  • detectMoves (default true) shows a block removed in one place and added in another as a move. Moved lines are purple, and their title says where they came from or went.
  • wrap wraps long lines. Without it, each side scrolls sideways on its own.
  • A minimap beside the scrollbar marks every change. Click it to jump. Turn it off with minimap={false}.
  • palette="colorblind" uses blue and orange instead of green and red.

The diff takes focus on click. Then n/p move to the next or previous change, e/c expand or collapse every fold, and v toggles the view.

Copying across rows gives one file's text, not both. In split view, it's the side where the selection started. In unified view, it's the new text, or the old text if you select only removed lines.

Virtualization

virtualize defaults to "auto". Diffs up to 500 rows render every row, so browser find (Ctrl/Cmd+F), printing, and server rendering see the whole diff. Larger diffs render only the rows in view. virtualize={true} or virtualize={false} forces either mode. Rows can have different heights (wrapped lines, notes), and are measured as they render.

Streaming

Set streaming while after is still growing, for example while a model writes a new version of a file:

<HighlightDiff {before} {after} streaming={!done} language={typescript} />

While streaming:

  • The part of before that after hasn't reached yet shows as "not reached yet", not as deleted.
  • Rows behind the last unchanged run of 3 or more lines freeze. Later chunks don't move them, and each chunk only re-diffs what comes after them.
  • follow (default true) keeps the newest row in view until the reader scrolls away.

When streaming turns off, the diff finishes from the frozen rows. It doesn't start over, so nothing the reader saw reshuffles.

LLM edit formats

parseEdits reads the edit formats models write, and applyEdits applies them to a file:

import { applyEdits, parseEdits } from "svelte-highlight/diff-edits";

const { format, edits } = parseEdits(modelOutput);
const { text, results } = applyEdits(source, edits);
// <HighlightDiff before={source} after={text} ... />

It reads:

  • SEARCH/REPLACE blocks (<<<<<<< SEARCH … ======= … >>>>>>> REPLACE), with an optional file path line before them.
  • Unified diffs. Line numbers are ignored, since models often get them wrong.
  • str_replace tool calls and edits arrays as JSON (old_str/new_str or old_string/new_string).
  • apply_patch envelopes (*** Begin Patch).
  • A single fenced code block, as a whole-file replacement.

applyEdits finds each edit's search text exactly first. Then it ignores trailing whitespace, then indentation (and re-indents the replacement to match the file), then it falls back to fuzzy line similarity. Each result reports status, strategy, score, and the matched lines. An edit that can't be found, or that overlaps an earlier edit, is skipped and reported as failed.

To show edits while the model is still writing them, use streamEditPrefix. It returns the part of the edited file that's settled so far, which only grows:

import { streamEditPrefix } from "svelte-highlight/diff-edits";

const preview = streamEditPrefix(source, partialOutput, { done });
// <HighlightDiff before={source} after={preview.after} streaming={!preview.done} ... />

Review

With review, each change gets Accept and Reject buttons. With the diff focused, a, r, and u accept, reject, or undo the current change. on:review fires on every decision with { decisions, text }. text is after with the rejected changes reverted.

<HighlightDiff
  bind:this={diff}
  {before}
  {after}
  language={typescript}
  review
  on:review={(e) => (result = e.detail.text)}
/>

bind:this gives you decideAll("accepted" | "rejected"), getResult(), and getPatch({ oldPath, newPath }). The patch is a unified diff that git apply accepts.

Git patches

To show a patch without its source files, parse it and pass one file at a time:

import { parsePatch } from "svelte-highlight/diff-edits";

const files = parsePatch(gitDiffOutput);
// <HighlightDiff patch={files[0]} language={...} />

parsePatch reads multi-file git output, including added, deleted, renamed, and binary files. Lines between hunks are unknown, so they show as folds that can't expand, with the hunk header. Line numbers stay real. Since only the hunks are known, highlighting can be less accurate than with full files.

Annotations

annotations puts notes below lines: [{ side: "new", line: 12, body: "…", author, tone }]. side is "old" or "new", line is 1-based, and tone is "info", "warning", "error", or "suggestion". A note's row grows to fit it.

Building your own diff UI

HighlightDiff combines smaller parts that you can use on their own. They share one controller:

<script>
  import {
    createDiffController,
    DiffMinimap,
    DiffStats,
    DiffView,
  } from "svelte-highlight";
  import typescript from "svelte-highlight/languages/typescript";

  const diff = createDiffController({ language: typescript, view: "split" });
  $: diff.update(before, after);
</script>

<DiffStats {diff} />
<button on:click={() => diff.nextChange()}>Next change</button>

<div style="display: flex; height: 480px">
  <DiffView {diff} style="flex: 1">
    <div slot="note" let:note>{note.author}: {note.body}</div>
    <button slot="actions" let:change on:click={() => comment(change)}>Comment</button>
  </DiffView>
  <DiffMinimap {diff} />
</div>
  • createDiffController(options) holds the diff state: the diff, highlighting, folds, review decisions, and navigation. Use it as a Svelte store ($diff), or call it from anywhere. update(before, after, { streaming }) and setPatch(file) set the input. setOptions({ view, context, … }) changes options. on("navigate" | "review" | …, listener) subscribes to events.
  • DiffView renders the rows. Its slots replace fold rows (fold), note cards (note), and the controls on each change (actions).
  • DiffMinimap shows the change marks and the area a DiffView on the same controller shows. Click it to scroll there.
  • DiffStats shows the counts and, after you navigate, "change N of M".

Every part on a controller stays in sync. For example, a toolbar button that calls diff.nextChange() scrolls each DiffView.

Many files

DiffFileList shows a page of per-file diffs, like a large pull request. It mounts files near the viewport and unmounts far ones, and keeps each file's measured height so the page doesn't jump. A page of 827 files stays near 10,000 DOM nodes. Before it mounts, it renders the first files down to about a screen and a half, so a server-rendered page shows them; a file's language, or a languageFor that returns one directly, highlights them there too.

<script>
  import { DiffFileList, parsePatch } from "svelte-highlight";

  $: files = parsePatch(gitDiffOutput).map((file) => ({
    path: file.newPath,
    oldPath: file.oldPath,
    status: file.status,
    patch: file,
  }));
</script>

<DiffFileList {files} />
  • Each file is a parsed patch or a before/after pair.
  • It loads each file's language by extension the first time the file mounts. Pass languageFor(path) to choose another way, or set language on a file.
  • windowing is "windowed" (default), "lazy" (mount near the view, never unmount), or "none" (mount everything, for printing or browser find).
  • maxHeight (default 640) caps each file's height before it scrolls on its own. null shows every file in full.
  • Click a header to collapse its file. Replace the header with the header slot.
  • bind:this, then scrollToFile(index) jumps to a file. The files around it mount first, so they have real heights before you scroll to them.
  • Set root if the list scrolls inside an element instead of the page.

When files above the view change height, the list keeps the first file in view still. It does this itself, since browsers' scroll anchoring doesn't catch these changes everywhere.

Headless diffs

svelte-highlight/diff has the diff itself, with no DOM:

import { buildRows, diffTexts, toUnifiedPatch, wordDiff } from "svelte-highlight/diff";

const state = diffTexts(before, after, { ignoreWhitespace: false });
state.blocks; // [{ type: "equal" | "change", a, aEnd, b, bEnd, id }]
const rows = buildRows(state, { view: "split", context: 3 });
const patch = toUnifiedPatch(state, { oldPath: "a/file.ts", newPath: "b/file.ts" });
const { old, new: added } = wordDiff("const a = 1;", "const a = 2;");
  • diffTexts and diffLines find shared prefixes and suffixes, then anchor on lines unique to both sides, then run Myers' algorithm between anchors.
  • createDiffSession() keeps state between calls, for streaming: session.update(before, after, { streaming: true }).
  • applyReview(state, decisions), detectMoves(state), and diffStats(state) work on the same state.

svelte-highlight/diff-controller exports createDiffController, and svelte-highlight/diff-edits exports the edit and patch parsers.

Limits

  • With ignoreWhitespace, unchanged lines show their after text, and toUnifiedPatch writes before's text for them. The patch then doesn't reproduce after's whitespace.
  • A region with more than 2,000 edits shows as one replacement, to bound the work.
  • Moved blocks aren't detected while streaming.
  • DiffView only virtualizes inside its own scroll container. For a page of many diffs, use DiffFileList.

Terminal Output

Use AnsiOutput to render terminal output that still contains ANSI SGR escape codes. Colors, bold, dim, italic, and underline become styled HTML (including the colon sub-parameter forms modern tools emit, like 4:3 and 38:5:196), along with OSC 8 hyperlinks, carriage-return overwrites, and reverse/strikethrough. The parser is separate from highlight.js, so reach for it with build logs, CLI output, and test runners.

<script>
  import { AnsiOutput } from "svelte-highlight";

  // Raw program output, escape codes included.
  const text = "\x1b[32m✓\x1b[0m build succeeded \x1b[2m(1.2s)\x1b[0m";
</script>

<AnsiOutput {text} />

Malformed or unsupported escape sequences are dropped. Nothing throws. OSC 8 hyperlinks only render as links for http:, https:, and mailto: URIs; any other scheme renders as plain text. parseAnsi is one-shot and drops a trailing unterminated sequence, so for streamed output accumulate the full text and re-parse it rather than calling it once per chunk.

Theming the palette

Pass --ansi-* style props to recolor the output. The 16 base colors, default foreground and background, and bold/dim weight and opacity all come from CSS variables with hex fallbacks:

<AnsiOutput
  {text}
  --ansi-background="#0d1117"
  --ansi-red="#ff7b72"
  --ansi-green="#3fb950"
/>

256-color and 24-bit truecolor codes resolve to RGB.

Readable colors

With autoContrast on (the default), spans that set a background get a readable foreground. If the chosen text color would disappear on that background (white on white, for example), it flips to black or white, whichever reads better. Only spans with a background are adjusted. Set autoContrast={false} to keep the ANSI colors as-is.

<AnsiOutput {text} autoContrast={false} />

Wrapping long lines

AnsiOutput never wraps by default -- long lines scroll horizontally. Set wrap to wrap them instead:

<AnsiOutput {text} wrap />

Pairing with CodeWindow

Put AnsiOutput inside CodeWindow variant="terminal" for the prompt and title bar:

<script>
  import { AnsiOutput, CodeWindow } from "svelte-highlight";

  const text = "\x1b[1;32m$\x1b[0m npm run build";
</script>

<CodeWindow variant="terminal" title="bash">
  <AnsiOutput {text} />
</CodeWindow>

Call parseAnsi directly if you need the segments yourself:

import { parseAnsi } from "svelte-highlight";

parseAnsi("\x1b[31merror\x1b[0m");
// => [{ text: "error", fg: { name: "red" } }]

For streamed output, createAnsiSession from svelte-highlight/ansi parses chunk by chunk and keeps sequences that straddle a chunk boundary intact. Call delta() after each append() to get only what changed: truncate your copy of the segments to start, then append segments.

import { createAnsiSession } from "svelte-highlight/ansi";

const session = createAnsiSession();
const segments = [];

session.append("\x1b[31merr");
session.append("or\x1b[0m done");
const { start, segments: changed } = session.delta();
segments.length = start;
segments.push(...changed);
// => [{ text: "error", fg: { name: "red" } }, { text: " done" }]

File Tabs

FileTabs groups code snippets behind a tab strip, like files in an editor. Pass file names as files, then use let:active in the default slot to render the matching snippet.

Arrow keys move between tabs; Home and End jump to the first and last. The markup follows the WAI-ARIA tabs pattern (role="tablist", role="tab", role="tabpanel").

<script>
  import Highlight, { FileTabs } from "svelte-highlight";
  import javascript from "svelte-highlight/languages/javascript";
  import typescript from "svelte-highlight/languages/typescript";
  import github from "svelte-highlight/styles/github";

  const sources = {
    "App.svelte": { language: typescript, code: "const answer = 42;" },
    "index.js": { language: javascript, code: "export default answer;" },
  };

  const files = Object.keys(sources);
</script>

<svelte:head>
  {@html github}
</svelte:head>

<FileTabs {files} let:active>
  <Highlight language={sources[active].language} code={sources[active].code} />
</FileTabs>

bind:active sets the open tab from your code. on:change fires when the user picks a different one.

<FileTabs {files} bind:active on:change={(e) => console.log(e.detail.active)}>
  <Highlight language={sources[active].language} code={sources[active].code} />
</FileTabs>

active is reconciled whenever files changes: an unknown or stale value (including a bind:active set before files loads) falls back to files[0]; removing the active file selects its former neighbor (same index, clamped to the new length), like closing a tab in an editor; an empty files array sets active to undefined and the tab panel omits aria-labelledby. on:change fires once whenever this reconciliation actually changes active.

When the tab strip overflows, its overflowing edge fades to signal there's more to scroll to; changing active (by click, keyboard, or bind:active) scrolls that tab into view.

The active tab's --tab-active-color and --tab-active-background are resolved from the highlighted code's computed style after mount, so they render as inherit/transparent on first paint; for an SSR-critical, above-the-fold tab strip, set --tab-active-background and --tab-active-color yourself to avoid a flash before hydration.

Custom renderers

Svelte 5.59 added custom renderers, which render components to terminals, canvas, PDF or native views instead of the DOM. The components above render HTML, so they only work with the DOM. HighlightTokens works anywhere: it has no markup of its own, and renders styled tokens only through your snippets. Import it by path. It's Svelte 5 only, so it isn't in the package's main entry.

<svelte:options customRenderer="terminal-renderer" />

<script>
  import HighlightTokens from "svelte-highlight/HighlightTokens.svelte";
  import rust from "svelte-highlight/languages/rust";
  import github from "svelte-highlight/themes/github";

  let { code } = $props();
</script>

<box>
  <HighlightTokens {code} language={rust} theme={github}>
    {#snippet line(tokens)}
      <line>
        {#each tokens as token}
          <text fg={token.color} bold={token.bold}>{token.text}</text>
        {/each}
      </line>
    {/snippet}
  </HighlightTokens>
</box>

HighlightTokens has to be compiled with the same renderer as the component that passes it snippets. A default customRenderer in your config already covers it. If components pick their renderer with <svelte:options>, pick one for HighlightTokens in your config too:

// svelte.config.js
export default {
  compilerOptions: {
    experimental: {
      customRenderer: ({ filename }) =>
        filename.includes("/src/terminal/") ||
        filename.endsWith("svelte-highlight/HighlightTokens.svelte")
          ? "terminal-renderer"
          : null,
    },
  },
};

Each token has text, scopes, and the theme's color, background, bold, italic, underline and strikethrough (see Styled tokens, without CSS). For the whole block at once, including the theme's foreground and background, use the children snippet instead of line:

<HighlightTokens {code} language={rust} theme={github}>
  {#snippet children({ lines, foreground, background })}
    <box fg={foreground} bg={background}>
      {#each lines as tokens}
        <line>{#each tokens as token}<text fg={token.color}>{token.text}</text>{/each}</line>
      {/each}
    </box>
  {/snippet}
</HighlightTokens>

It works with the DOM too, with HTML elements in the snippets.

See examples/terminal for a complete terminal renderer that prints highlighted files, and examples/opentui for an interactive opentui view.

Component API

Highlight

Props

Name Type Default value
code any N/A (required)
language { name: string; register: object } N/A (required)
langtag boolean false
wrap boolean false
label string "Code"

$$restProps are forwarded to the top-level pre element. label names the focusable code region (see Accessibility).

Dispatched Events

  • on:highlight: fired after code is highlighted, once per change (not on unrelated re-renders), including when the result is empty (empty code)
<Highlight
  language={typescript}
  {code}
  on:highlight={(e) => {
    /**
     * The highlighted HTML as a string.
     * @example "<span>...</span>"
     */
    console.log(e.detail.highlighted);

    /**
     * The scope-event stream behind `highlighted`. See "Headless usage"
     * below for consuming it directly (tokenLines, toRanges, custom
     * renderers) instead of re-parsing the HTML.
     */
    console.log(e.detail.events);
  }}
/>

HighlightStyle

Props

Name Type Default value
theme string | ThemePalette N/A
light string | ThemePalette N/A
dark string | ThemePalette N/A
mode "auto" | "light" | "dark" | string "auto"
scopeClass string hash of theme
nonce string N/A

The default slot exposes { scopeClass }. $$restProps are forwarded to the top-level div element.

LineNumbers

Props

Name Type Default value
highlighted string N/A (required unless lines is set)
lines string[] undefined
lineCount number undefined
hideBorder boolean false
wrapLines boolean false
startingLineNumber number 1
highlightedLines number[] []
lineStates Record<number, "highlighted" | "focus" | "added" | "removed"> {}
langtag boolean false
languageName string "plaintext"
label string "Code with line numbers"

$$restProps are forwarded to the top-level div element. label names the focusable code region.

CopyButton

Props

Name Type Default value
code string N/A (required)
copy (code: string) => void | Promise<void> (code) => navigator.clipboard.writeText(code)
transform (code: string) => string (code) => code
timeout number 2000
text string "Copy"
copiedText string "Copied!"
errorText string "Copy failed"
absolute boolean true (false inside CodeToolbar)

$$restProps are forwarded to the top-level button element. A visually-hidden role="status" region after the button announces copiedText on success and errorText on failure.

<CopyButton {code} copiedText="Copied to clipboard" errorText="Could not copy" />

CSS Variables

Variable Description Default value
--copy-top Top offset of the button 0.5em
--copy-right Right offset of the button 0.5em
--copy-size Width and height of the button 2em
--copy-padding Inner padding of the button 0.5em
--copy-background Background color of the button inherit
--copy-hover-background Background color on hover inside CodeToolbar text color at 12%
--copy-color Foreground color of the button inherit
--copy-border-radius Border radius of the button 4px
--copy-border Border of the button none
--copy-z-index Stacking order of the button 2

Dispatched Events

  • on:copy: fired after a successful copy, with { code } -- the transform-applied string, not the original code prop
  • on:error: fired if the copy behavior throws, with { error }
  • on:mouseenter: forwarded MouseEvent, useful for prefetching an async copy on hover
  • on:mouseleave: forwarded MouseEvent
<CopyButton
  {code}
  on:copy={(e) => console.log(e.detail.code)}
  on:error={(e) => console.error(e.detail.error)}
/>

CodeWindow

Props

Name Type Default value
variant "macos" | "terminal" | "plain" "macos"
title string ""
label string undefined

$$restProps are forwarded to the top-level div element. The code block inside the window takes title, or label when there is no title, as its region name.

<CodeWindow variant="terminal" label="Install output">
  <AnsiOutput text={output} />
</CodeWindow>

Slots

  • titlebar: optional content rendered in the mirrored trailing region of the title bar (empty by default, which keeps the title centered)

CSS Variables

Variable Description Default value
--window-background Background color of the window body #1e1e1e
--window-border Border of the window 1px solid rgba(255, 255, 255, 0.1)
--window-radius Corner radius of the window 0
--titlebar-gap Gap between items in the title bar 0.5em
--titlebar-padding Padding of the title bar 0.65em 1em
--titlebar-background Background color of the title bar #2d2d2d
--titlebar-border Bottom border of the title bar 1px solid rgba(255, 255, 255, 0.1)
--titlebar-color Text color of the title bar rgba(255, 255, 255, 0.6)
--titlebar-font-family Font family of the title bar system-ui, -apple-system, sans-serif
--titlebar-font-size Font size of the title bar 0.8125em
--titlebar-line-height Line height of the title bar normal
--dot-gap Gap between the macOS traffic-light dots 0.5em
--dot-size Diameter of the macOS traffic-light dots 0.75em
--dot-close Color of the "close" traffic-light dot #ff5f56
--dot-minimize Color of the "minimize" traffic-light dot #ffbd2e
--dot-maximize Color of the "maximize" traffic-light dot #27c93f
--prompt-font-family Font family of the terminal prompt ui-monospace, monospace
--prompt-font-weight Font weight of the terminal prompt 700
--prompt-color Color of the terminal prompt inherit

CodeToolbar

Props

Name Type Default value
languageName string undefined
title string ""
sticky boolean true
label string undefined

$$restProps are forwarded to the top-level div element.

Slots

  • start: rendered next to the language badge/title
  • default: rendered in the trailing/"end" region

CSS Variables

Variable Description Default value
--toolbar-background Tint painted over the theme background color-mix(in srgb, currentColor 5%, transparent)
--toolbar-color Text color of the toolbar content the theme's text color
--toolbar-border Bottom border of the toolbar 1px solid text color at 14%
--toolbar-padding Padding of the toolbar 0.25em 0.375em 0.25em 1em
--toolbar-gap Gap between the title, badge and controls 0.75em
--toolbar-height Minimum height of the toolbar 2.75em
--toolbar-font-family Font family of the toolbar system-ui, -apple-system, "Segoe UI", Roboto, sans-serif
--toolbar-font-size Font size of the toolbar 0.8125em
--toolbar-z-index Stacking order of a sticky toolbar 3
--langtag-background Background color of the language badge text color at 10%
--langtag-color Text color of the language badge text color at 72%
--langtag-padding Padding of the language badge 0.375em 0.625em
--langtag-border-radius Corner radius of the language badge 999px
--langtag-font-size Font size of the language badge 0.875em

WrapToggle

Props

Name Type Default value
wrap boolean false
text string "Wrap"

wrap is bindable (bind:wrap). $$restProps are forwarded to the ToolbarButton.

Slots

  • default: exposes wrap, falls back to a wrap icon (text is the aria-label and title)

CSS Variables

WrapToggle is a ToolbarButton. Style it with the --toolbar-button-* variables.

ToolbarButton

Props

Name Type Default value
label string N/A (required)
pressed boolean | undefined undefined
href string | undefined undefined

label is the aria-label and the title tooltip. Set pressed to make a toggle button. Set href to render a link instead of a button. $$restProps are forwarded to the button or a element, and class is merged with the button's own class.

Slots

  • default: the icon or text

Events

  • on:click

CSS Variables

See ToolbarButton variables.

AnsiOutput

Props

Name Type Default value
text string N/A (required)
autoContrast boolean true
wrap boolean false
label string "Terminal output"

$$restProps are forwarded to the top-level pre element. label names the focusable output region.

HighlightSvelte

Props

Name Type Default value
code any N/A (required)
langtag boolean false
wrap boolean false
label string "Code"

$$restProps are forwarded to the top-level pre element.

Dispatched Events

  • on:highlight: fired after code is highlighted, once per change (not on unrelated re-renders), including when the result is empty (empty code)
<HighlightSvelte
  {code}
  on:highlight={(e) => {
    /**
     * The highlighted HTML as a string.
     * @example "<span>...</span>"
     */
    console.log(e.detail.highlighted);

    /** The scope-event stream behind `highlighted`. See "Headless usage" below. */
    console.log(e.detail.events);
  }}
/>

HighlightAuto

Props

Name Type Default value
code any N/A (required)
languageNames LanguageName[] undefined
langtag boolean false
wrap boolean false
label string "Code"

$$restProps are forwarded to the top-level pre element.

Note: LanguageName is a union type of all supported language names, providing autocomplete and type safety. You can import it from svelte-highlight:

import type { LanguageName } from "svelte-highlight";

Dispatched Events

  • on:highlight: fired after code is highlighted, once per change (not on unrelated re-renders), including when the result is empty (empty code, or no candidate language matches)
<HighlightAuto
  {code}
  on:highlight={(e) => {
    /**
     * The highlighted HTML as a string.
     * @example "<span>...</span>"
     */
    console.log(e.detail.highlighted);

    /**
     * The inferred language name
     * @example "css"
     */
    console.log(e.detail.language);

    /** The scope-event stream behind `highlighted`. See "Headless usage" below. */
    console.log(e.detail.events);

    /**
     * The runner-up candidate and its relevance, for surfacing detection
     * confidence. `undefined` when no other candidate scored above zero.
     * @example { language: "typescript", relevance: 6 }
     */
    console.log(e.detail.secondBest);
  }}
/>

HighlightAutoLazy

Same props, slot props, and on:highlight detail as HighlightAuto. See Lazy auto-highlighting for how it differs.

Name Type Default value
code any N/A (required)
languageNames LanguageName[] undefined
langtag boolean false
wrap boolean false
  • on:highlight: fired once per detection result, after the grammars load. Not fired for the plain-text placeholder.

HighlightEditable

Props

Name Type Default value
code string ""
language { name: string; register: object } N/A (required)
tabSize number 2
historyLimit number 200
readonly boolean false
label string "Code editor"

$$restProps are forwarded to the top-level pre element. The editor is a role="textbox" with aria-multiline, and label is its accessible name. readonly also sets aria-readonly.

<HighlightEditable language={typescript} bind:code label="Query editor" />

code supports two-way binding (bind:code).

Methods

Use bind:this, then call undo(), redo(), focus(), selectAll(), insert(text), indent(), outdent(), setCode(value), clear(), getCode(), canUndo(), or canRedo().

Dispatched Events

  • on:change: fired on every edit, with { code }
  • on:blur: fired when the editor loses focus, with { code }
  • on:history: fired when the undo/redo stack changes, with { entries, index, canUndo, canRedo }
<HighlightEditable
  language={typescript}
  bind:code
  on:change={(e) => console.log(e.detail.code)}
  on:history={(e) => console.log(e.detail.canUndo, e.detail.canRedo)}
/>

HighlightStream

Props

Name Type Default value
code string ""
language { name: string; register: object } N/A (required)
done boolean false
caret boolean true
autoScroll boolean false
virtualize boolean false
overscan number 12
checkpointInterval number 100
doneText string "Code finished streaming"
label string "Code"

$$restProps are forwarded to the top-level pre element. label names the focusable code region. overscan and checkpointInterval only apply when virtualize is set. doneText is announced by a visually-hidden live region once done becomes true; set it to "" to disable the announcement.

Methods

Use bind:this, then call scrollToLine(line) -- works in both virtualize and default modes.

Dispatched Events

  • on:highlight: fired after each highlight pass, with { highlighted } -- not dispatched when virtualize is set
  • on:done: fired after the final full highlight once done is set
  • on:windowchange: fired with { start, end, lineCount } whenever the rendered window moves, once the new rows are in the DOM -- only dispatched when virtualize is set
<HighlightStream
  language={typescript}
  {code}
  {done}
  on:highlight={(e) => console.log(e.detail.highlighted)}
  on:done={() => console.log("stream finished")}
/>

MarkdownStream

Props

Name Type Default value
text string ""
done boolean false
resolveLanguage (lang: string | undefined, segment: FenceSegment) => LanguageType | Promise<LanguageType> | undefined resolves via loadLanguage, falling back to plaintext
caret boolean true
autoScroll boolean true
doneText string "Code finished streaming"

$$restProps are forwarded to the top-level div element. resolveLanguage results are cached per canonical language name for the component's lifetime. doneText is announced once, by the last fence only, once done becomes true; set it to "" to disable the announcement. Each fence's code region is named for its language ("js code"), or "Code" when the fence has none.

Slots

Name Props Fallback
text segment <div class="shl-md-text">{segment.text}</div>
fence segment, language <HighlightStream code={segment.code} {language} done={done || !segment.open} caret={...} autoScroll={autoScroll} label={...} doneText={...} />

Dispatched Events

  • on:fence: fired once, the first time a fence segment appears, with { id, lang }
  • on:done: fired once when done becomes true

CSS Variables

Variable Description Default value
--md-gap Gap between prose and fence segments 1em
--md-text-white-space white-space of the default prose block pre-wrap
<MarkdownStream {text} {done} on:fence={(e) => console.log(e.detail.lang)} />

HighlightVirtual

Props

Name Type Default value
code any N/A (required)
language { name: string; register: object } N/A (required)
overscan number 12
checkpointInterval number 100
tokenizeAhead boolean false
label string "Code"

label names the focusable scroll region. $$restProps are forwarded to the top-level pre element (the scroll container -- size it with style/class, but white-space is pinned to pre and can't be overridden this way).

Methods

Use bind:this, then call scrollToLine(line, { align }). It jumps without animation. align is "start" (default, line at the top edge) or "center" (line in the middle of the viewport).

Dispatched Events

  • on:windowchange: fired whenever the rendered window changes, once the new rows are in the DOM, with { start, end, lineCount }
  • on:tokenize: fired after each idle slice of tokenizeAhead, with { through, lineCount }; through === lineCount once the whole document is tokenized
<HighlightVirtual language={json} code={hugeLogDump} style="height: 480px" />

See Large documents above.

HighlightDiff

Props

Name Type Default value
before string ""
after string ""
patch FilePatch | null (from parsePatch) null
language { name: string; register: object } N/A (required)
view "unified" | "split" "unified"
context number 3
wordDiff boolean true
ignoreWhitespace boolean false
detectMoves boolean true
streaming boolean false
follow boolean true
review boolean false
wrap boolean false
virtualize "auto" | boolean "auto"
annotations Annotation[] []
palette "default" | "colorblind" "default"
minimap boolean true
overscan number 10
tabSize number 4

patch takes precedence over before/after. view supports bind:view. class and style go on the outer element; size the diff with them (it's 400px tall by default). Other $$restProps are forwarded to the inner DiffView.

Methods

Use bind:this, then call nextChange(), prevChange(), expandAll(), collapseAll(), decideAll("accepted" | "rejected"), getResult(), or getPatch({ oldPath, newPath }). The diff property is the underlying controller.

Dispatched Events

  • on:stats: fired when the counts change, with { additions, deletions, changes }
  • on:navigate: fired on nextChange/prevChange, with { change, index, count }
  • on:review: fired on every accept or reject, with { decisions, text }

See Diffs above.

DiffView

Props

Name Type Default value
diff DiffController N/A (required)
follow boolean true
review boolean false
wrap boolean false
virtualize "auto" | boolean "auto"
palette "default" | "colorblind" "default"
overscan number 10
keyboard boolean true

$$restProps are forwarded to the top-level section element.

Slots

  • fold: replaces a fold row, with { fold, count, toggle }
  • note: replaces an annotation card, with { note }
  • actions: controls on the first row of each change, with { change, decision, decide }. Filling it shows it even without review.

DiffMinimap and DiffStats

Both take one prop, diff (a DiffController). $$restProps are forwarded to the top-level element. DiffMinimap fills its parent's height, so give it one.

DiffFileList

Props

Name Type Default value
files DiffFile[] []
view "unified" | "split" "unified"
context number 3
wordDiff boolean true
ignoreWhitespace boolean false
wrap boolean false
review boolean false
palette "default" | "colorblind" "default"
maxHeight number | null 640
languageFor (path) => language | Promise<language> | undefined by file extension
windowing "windowed" | "lazy" | "none" "windowed"
rootMargin string "1500px 0px"
root Element | null null (the page scrolls)
collapsible boolean true

A DiffFile is { key?, path, oldPath?, status?, patch?, before?, after?, language? }. $$restProps are forwarded to the top-level div element.

Slots

  • header: replaces a file's header, with { file, index, stats, collapsed, toggle }. stats comes from the patch, or from the file's diff once it has mounted.

Methods

Use bind:this, then call scrollToFile(index, options). options are scrollIntoView options (default { block: "start" }).

FileTabs

Props

Name Type Default value
files string[] N/A (required)
active string files[0]

$$restProps are forwarded to the top-level div element. active supports bind:active and is reconciled against files: an unknown value falls back to files[0], removing the active file selects its former neighbor, and an empty files array sets active to undefined.

Dispatched Events

  • on:change: fired when the user picks a tab, with { active }
<FileTabs {files} on:change={(e) => console.log(e.detail.active)} let:active>
  <Highlight language={sources[active].language} code={sources[active].code} />
</FileTabs>

HighlightTokens

Svelte 5 only. Import from svelte-highlight/HighlightTokens.svelte. See Custom renderers.

Props

Name Type Default value
code any —
language LanguageType —
theme string | ThemePalette undefined
color string undefined
line Snippet<[tokens, index]> undefined
children Snippet<[{ lines, events, foreground, background }]> undefined

Without theme, tokens carry only text and scopes. color is the text color for themes that leave text uncolored. children takes precedence over line.

Typewriter

Props

Name Type Default value
highlighted string ""
speed number 30
play boolean true
easing (t: number) => number linear
granularity "char" | "word" "char"
revealed number 0
total number 0
label string "Code"

$$restProps are forwarded to the top-level pre element. label names the focusable code region. revealed/total are read-only in practice (overwritten every frame) but exposed for bind:revealed/bind:total.

Dispatched Events

  • on:done: fires when typing finishes
  • on:progress: fires whenever revealed advances, with { revealed, total }
<Highlight language={typescript} {code} let:highlighted>
  <Typewriter {highlighted} on:done={() => console.log("revealed")} />
</Highlight>

Headless usage

The highlighting engine's output is a flat scope-event stream (ScopeEvent[]): a sequence of TEXT/OPEN/CLOSE events that renderHtml, toRanges, and tokenLines each render differently. Every <Highlight>-family component exposes this stream (as events, in the default slot and the highlight event detail — see Component API above), and svelte-highlight/engine + svelte-highlight/registry expose it directly, so highlighting can be consumed headlessly: server routes, build pipelines, tests, or any non-component context. svelte-highlight/engine has zero Svelte dependency and works in any JS runtime.

Zero-bundler usage from a CDN

The engine, grammars and DOM entries are plain ES modules with only relative imports, so a page can load them from a CDN with no bundler, build step, or Svelte in the loop. An import map points the package at the published files, so the imports read the same as with a bundler. Browsers don't read the package's exports, so add the .js extension:

<script type="importmap">
  { "imports": { "svelte-highlight/": "https://cdn.jsdelivr.net/npm/svelte-highlight/" } }
</script>

<script type="module">
  import { createRegistry, registerAll, renderHtml } from "svelte-highlight/engine.js";
  import typescript from "svelte-highlight/languages/typescript.js";

  const registry = createRegistry();
  registerAll(registry, typescript);

  const { events } = registry.tokenize("const greeting: string = 'hi';", "typescript");
  document.querySelector("pre").innerHTML = renderHtml(events);
</script>

Use a CDN that serves the files unchanged, such as jsDelivr or unpkg. loadLanguage imports grammars by relative path, which breaks on CDNs that rewrite modules. See it as a complete page in examples/cdn.

Other hosts

spec/ENGINE.md specifies what the engine computes, for running the same grammars outside JavaScript or in another JS runtime. spec/corpus.json has samples with their expected events, and a host conforms if it reproduces them. examples/rust passes it from Rust, by running the engine in QuickJS.

Stability tiers

  • Stable, semver-governed: ScopeEvent, TEXT/OPEN/CLOSE, TokenRange, HighlightResult, LineToken, Renderer, renderHtml, toRanges, extendLines, tokenLines, escapeHtml, createHtmlRenderer, createRangeRenderer, createLineRenderer, Registry and its methods, createRegistry, registerAll, StreamSession, TokenizedDocument, createTokenizedDocument, TextSegment, FenceSegment, MarkdownSegment, FenceSplitter, createFenceSplitter, SearchMatch, SearchOptions, Search, createSearch, highlightMatches, UnknownLanguageError, TokenizerLoopError. Snapshot is a serializable format that round-trips within one library version, but is not guaranteed stable across versions — a snapshot from an older release may be rejected on resume. The same caveat applies to TokenizedDocument: its method surface (setCode/append/lineCount/lineRange/textRange/tokenizedThrough/checkpointCount) is stable and semver-governed, but internally it resumes from Snapshots the same way StreamSession does, so anything that tried to serialize and later resume a TokenizedDocument's internal state directly would hit the same cross-version instability -- the public API doesn't expose that today.
  • Generated data, versioned with the library: GrammarIR/GrammarState. These come from the build pipeline and are consumed by registerAll; treat them as opaque payloads whose field-level structure may change in any minor release. Always load grammars from the same package version as the engine.
  • Experimental, may change in a minor release: everything in svelte-highlight/diff, svelte-highlight/diff-edits, svelte-highlight/diff-controller, and svelte-highlight/styled-tokens, and the HighlightDiff, DiffView, DiffMinimap, DiffStats, and DiffFileList components. They're new, and their API may change before they're stable. Also createWorkerHighlighter, serveHighlighter, PostMessageTarget, WorkerHighlighter, WorkerSession, ServeHighlighterOptions (svelte-highlight/worker). The wire protocol between the two halves is not part of the public contract — only the documented function behavior is.

Headless highlighting, isolated registry

Use createRegistry() when you want an instance isolated from the components' shared registry (SSR request isolation, tests):

import { createRegistry, registerAll, renderHtml, tokenLines } from "svelte-highlight/engine";
import typescript from "svelte-highlight/languages/typescript";

const registry = createRegistry();
registerAll(registry, typescript);

const { events, value } = registry.highlight(code, { language: "typescript" });
// value: same HTML the <Highlight> component renders
// events: the scope-event stream — one tokenization, any number of consumers
const lines = tokenLines(events); // [[{ text: "const", scopes: ["keyword"] }, …], …]

Joining the components' shared registry

Use svelte-highlight/registry when a language registered here should also be visible to <Highlight> (and vice versa):

import { registry, ensureRegistered } from "svelte-highlight/registry";
import typescript from "svelte-highlight/languages/typescript";

ensureRegistered(typescript);
const ranges = registry.tokenizeRanges(code, { language: "typescript" });

Streaming, headless

The same primitives HighlightStream uses:

import { registry } from "svelte-highlight/registry";

const session = registry.createSession("typescript");
session.append(chunk); // repeat as chunks arrive
session.replace(from, to, text); // patch an earlier range without restarting
const snapshot = session.snapshot(); // JSON-serializable checkpoint
const { value } = session.finish();

Highlighting in a Web Worker

svelte-highlight/worker is a headless pair — a worker-side handler and a main-thread client — for running the engine off the main thread. Both halves have zero Svelte dependency.

// highlight.worker.js
import { serveHighlighter } from "svelte-highlight/worker";
serveHighlighter();
import { createWorkerHighlighter } from "svelte-highlight/worker";

const highlighter = createWorkerHighlighter(
  typeof Worker !== "undefined"
    ? new Worker(new URL("./highlight.worker.js", import.meta.url), { type: "module" })
    : undefined,
);

An omitted (or falsy) worker runs the identical API in-process against svelte-highlight/registry's shared singleton instead — the SSR/tests fallback, and why the construction above is a single ternary: the same code works whether or not Worker exists in the current environment.

const { value } = await highlighter.highlight(code, "typescript");
const lines = await highlighter.tokenLines(code, "typescript");

Streaming works the same way as registry.createSession, just async:

const session = highlighter.createSession("typescript");
await session.append(chunk); // repeat as chunks arrive
await session.replace(from, to, text); // patch an earlier range without restarting
const snapshot = await session.snapshot(); // JSON-serializable checkpoint
const { value } = await session.finish();

Grammars load lazily inside the worker, per language, the first time each is seen. HighlightStream can't yet consume a remote, worker-backed session — its incremental rendering assumes a same-thread StreamSession — so streaming highlight results back into a component still requires posting the resolved HTML/events yourself. terminate() tears down the underlying Worker (a no-op in local mode).

Reaching the stream from a component — no HTML re-parsing

<Highlight language={typescript} {code} let:highlighted let:events>
  <pre><code>{@html highlighted}</code></pre>
  <TokenMinimap {events} />
</Highlight>

<Highlight language={typescript} {code} on:highlight={(e) => analyze(e.detail.events)} />

A third-party render target

Renderer<Out> names the contract that renderHtml, toRanges, and tokenLines (via createHtmlRenderer, createRangeRenderer, createLineRenderer) all conform to — implement it to plug a custom render target into the same pipeline:

import { TEXT, OPEN, CLOSE } from "svelte-highlight/engine";

/** @type {import("svelte-highlight/engine").Renderer<MyOutput>} */
const myRenderer = {
  render(events) {
    for (const ev of events) {
      if (ev.t === TEXT) {
        /* ev.v */
      } else if (ev.t === OPEN) {
        /* ev.s */
      } else {
        /* CLOSE */
      }
    }
    return out;
  },
};

Styled tokens, without CSS

Terminals, canvas, PDF, images and native views can't read theme CSS. styleTokens from svelte-highlight/styled-tokens resolves a theme per token instead: each token gets its text color, its own background when it paints one, and bold, italic, underline and strikethrough flags. The theme can be a ThemePalette (svelte-highlight/themes/*) or theme CSS (svelte-highlight/styles/*). The colors match what the theme's CSS paints for <Highlight>.

import { createRegistry, registerAll } from "svelte-highlight/engine";
import rust from "svelte-highlight/languages/rust";
import { styleTokens } from "svelte-highlight/styled-tokens";
import github from "svelte-highlight/themes/github";

const registry = createRegistry();
registerAll(registry, rust);

const { events } = registry.highlight("fn main() {}", { language: "rust" });
const { foreground, background, lines } = styleTokens(events, github);
// lines[0][0]: { text: "fn", scopes: ["keyword"], color: "#d73a49" }

Lines split the same way as tokenLines. Colors are CSS values as the theme writes them, usually hex. For many event streams with one theme, createTokenStyler(theme) resolves the theme once and returns a styler with lines(events). Pass { color } to either function for themes that leave the text uncolored.

For a terminal, toAnsi turns styled lines into a string with true-color escapes. Pass { background: true } to paint the theme's background too.

import { styleTokens, toAnsi } from "svelte-highlight/styled-tokens";

console.log(toAnsi(styleTokens(events, github)));

Errors

  • UnknownLanguageError ({ language }): thrown by tokenize, highlight, tokenizeRanges, and createSession when language isn't registered.
  • TokenizerLoopError ({ grammarName, iterations }): thrown once a parse exceeds 500,000 tokenizer iterations, guarding against a grammar bug that never advances position.

Both are exported from svelte-highlight/engine and are distinct from LanguageLoadError (exported from svelte-highlight and svelte-highlight/load-language), which fires from a grammar module's dynamic import failure, not a registry lookup.

illegal is enforced only during auto-detection

A grammar's illegal pattern only aborts tokenizeAuto/highlightAuto: a candidate whose text matches its own illegal pattern loses relevance scoring and is dropped from consideration. An explicit tokenize/highlight/createSession call never checks it, unlike hljs's own ignoreIllegals: false default, which throws on the same condition. This is deliberate: streamed or LLM-generated code is routinely "illegal" mid-stream — an unclosed string, a dangling # — and an explicit-language highlighter that could throw partway through a stream would be unusable for that case. For hljs-style strict validation, run highlightAuto(code, [language]) instead.

Supported Languages

Supported Styles

Examples

By default, example set-ups use Svelte 5. The exception is examples/vite@svelte-4, which uses Svelte 4.

Changelog

License

MIT

Grammars and themes are derived from highlight.js, used under its BSD-3-Clause license. The highlighting engine itself is original code under this package's MIT license — no highlight.js source is included in it.

Jelajahi proyek terkait

Memuat…