Skip to content

API ​

Every runtime export and public type has a runnable example in API Examples.

Package exports ​

EntryPublic values
vue-syntax-highlighterSyntaxHighlighter, SyntaxHighlighterProvider, VueSyntaxHighlighter (default and named), useSyntaxHighlighter, Light, LightAsync, PrismLight, PrismAsync, defineTheme, DEFAULT_THEME, DEFAULT_THEME_NAME, BUILTIN_THEME_REGISTRY, isColorScheme, ResultCache, buildCacheKey, stableStringify, createHighlighterContext, HIGHLIGHTER_CONTEXT, HighlighterContext, EngineManager, isHighlightEngine, BUILTIN_ENGINE_IDS
vue-syntax-highlighter/coreThe same runtime API without a built-in engine loader. Pass an explicit engine object; no optional engine peer is referenced at build time.
vue-syntax-highlighter/fullThe same API as the default entry, including all built-in engine loaders. It is equivalent to the default entry in the current release.
vue-syntax-highlighter/engines/shikicreateShikiEngine, ShikiEngine, ShikiEngineOptions, ShikiLanguageRegistration, ShikiLanguageLoader, languageLoaders, languageAliases, supportedLanguages
vue-syntax-highlighter/engines/highlightjscreateHighlightjsEngine, HighlightjsEngine, HighlightjsEngineOptions, HighlightjsLanguageRegistration, HighlightjsLanguageLoader, supportedLanguages
vue-syntax-highlighter/engines/lowlightcreateLowlightEngine, LowlightEngine, LowlightEngineOptions, LowlightLanguageRegistration, LowlightLanguageLoader, supportedLanguages, plus createHighlightjsEngine
vue-syntax-highlighter/engines/prismcreatePrismEngine, PrismEngine, PrismEngineOptions, PrismLanguageRegistration, PrismLanguageLoader, supportedLanguages
vue-syntax-highlighter/themesvshLight, vshDark, vshPaper, vshMidnight
vue-syntax-highlighter/themes/defaultdefault, vshLight
vue-syntax-highlighter/themes/vsh-lightdefault, vshLight
vue-syntax-highlighter/themes/vsh-darkdefault, vshDark
vue-syntax-highlighter/themes/vsh-paperdefault, vshPaper
vue-syntax-highlighter/themes/vsh-midnightdefault, vshMidnight
vue-syntax-highlighter/styles.cssBase layout and token stylesheet
vue-syntax-highlighter/styles/fallback.cssOptional pre-light-dark() scheme fallback
vue-syntax-highlighter/styles/complete.cssBase plus fallback rules in one stylesheet; opt-in and larger than styles.css
vue-syntax-highlighter/package.jsonPackage metadata

Public types: ColorScheme, EngineOptions, HighlightCacheLike, HighlightEngine, HighlightInput, HighlightLine, HighlightRequest, HighlightResult, HighlightToken, HighlighterOptions, HighlighterScopeMode, LineHeightMode, LineRenderInfo, LineSlotProps, SyntaxHighlighterEmits, SyntaxHighlighterExposed, SyntaxHighlighterProps, ThemeColorsInput, ThemeDefinition, ThemeInput, ThemePair, ThemeScope, TokenSlotProps, TokenStyle, TokenType, CacheKeyInput, ResultCacheOptions, HighlightedEventContext, HighlighterContext, EngineChoice, EngineLoader, EngineRegistry, BuiltinEngineId, VueSyntaxHighlighterOptions, VueSyntaxHighlighterPlugin, UseSyntaxHighlighterOptions, UseSyntaxHighlighterReturn.

Deep imports into dist/ are blocked by the exports map.

SyntaxHighlighter ​

Props ​

PropTypeDefaultDescription
codestring''Source text. Never HTML. One terminal line break is not rendered as an extra row; copy() still returns it.
languagestring'text'Engine language; exposed as language-<id>.
enginestring | HighlightEngineinheritsEngine identifier or adapter object.
engineOptionsEngineOptions{}Serialized into the cache key.
themeThemeDefinition | stringinheritsSingle theme.
themesThemePairinheritsLight/dark pair.
colorScheme'auto' | 'light' | 'dark'inheritsScheme mode.
lineNumbersbooleanfalseDecorative gutter.
startLinenumber1First displayed/source line number. 0 is allowed.
highlightLines(number | [number, number])[][]Source line numbers/ranges.
wrapbooleanfalseWrap long lines; disables virtualization.
virtualizedboolean | 'auto'falseWindowed rendering.
virtualizeAfternumber2000Auto threshold in lines.
heightstring | number—Bounded pre height.
maxHeightstring | number—Bounded pre max height.
lineHeightnumber20Row line height in px; used by virtualization and lineHeightMode="fixed".
lineHeightMode'visual' | 'fixed''visual'Opt in to numeric inline row metrics. fixed emits min-height and line-height without forcing height on non-virtual rows.
themeScope'inherit' | 'self''inherit'Opt in to emitting theme variables and scheme attributes on the .vsh root even when a wrapper provider owns the scope.
overscannumber10Extra lines above/below.
deferbooleanfalseWait for intersection before highlighting.
rootMarginstring'200px 0px'Observer root margin.
preTagstring | Component'pre'Outer scroll element.
codeTagstring | Component'code'Code element.
lineProps(info: LineRenderInfo) => attrs—Per-line attributes.
tokenProps(info: TokenRenderInfo) => attrs—Per-token attributes.
tabindexnumber | string | boolean0Focusable scroll region. true becomes 0; false omits the attribute.
showLineNumbersboolean—Deprecated alias for lineNumbers.
wrapLinesboolean—Deprecated alias for wrap.
customStylestring | object—Deprecated; native style wins.
codeTagPropsobject—Deprecated code attributes.

Unknown attributes, including class, style, data-*, and aria-*, fall through to the root .vsh element. Caller classes merge with built-in classes.

At runtime, a non-string code value such as null from an API or Markdown parser is treated as an empty string for both rendering and copy helpers.

aria-label and aria-labelledby are applied to the scrollable pre, not duplicated on the root .vsh element. The pre becomes a named region only when one is present; without an accessible name it is not exposed as an unnamed landmark.

Slots ​

All content slots are optional and scoped.

SlotPayload
header{ language, theme }
footer{ language, theme }
loading{ busy, language, error }
error{ error, language }
line{ line, lineNumber, index, highlighted, tokens, renderTokens }
line-numbersame line payload; gutter content
token{ token, index, line, lineNumber, tokenIndex, renderToken }

Line slots replace line content; the gutter, line wrapper, and highlighted-line class remain. renderTokens() composes default token rendering, including the token slot.

Events ​

EventPayload
highlightedresult: HighlightResult, context: { language, theme, colorScheme }
errorError

Errors never throw out of render. The component keeps the last valid result or plain text and emits one development warning per distinct failure signature.

Exposed methods ​

MethodDescription
refresh()Re-run the current highlight. Returns the result or undefined.
highlight({ code, language?, preserveTrailingNewline? })Highlight explicit input and return its result. Set preserveTrailingNewline to true to keep terminal line breaks; the default is false.
copy()Copy the complete source without gutter text; resolves false on failure.
getCopyPayload()Return the complete normalized source.

SyntaxHighlighterProvider ​

Props: engine, engines, theme, themes, colorScheme, themeRegistry, languages, cache, cacheCapacity, cacheMaxChars, cacheEngineIdentity, renderless, defer, rootMargin, virtualized, virtualizeAfter, scopeTag.

The provider renders one display: contents theme scope, provides configuration to descendants, preloads languages on mount, and disposes loader-owned engines and caches on unmount. Preloading resolves the provider or ancestor engine; in the core entry there is no implicit default, so a provider that preloads languages must configure an engine itself. The default entry falls back to the built-in Shiki loader. Nested providers inherit unspecified values. scopeTag renders a div by default; set it to section, li, or another tag when the provider must sit inside a specific content model.

Set renderless to remove the wrapper while keeping the provider configuration. A renderless provider renders its slot children directly, so descendant blocks own the theme variables; scopeTag has no visible effect and emits one development warning whenever it is set, including div. The default remains a div.vsh-scope wrapper.

cacheEngineIdentity is a creation-time option, default false. When enabled, cache keys include a stable token for each engine object, so two engines with the same id no longer share cached results. The default cache key is byte-identical to 1.1.x.

engines, themeRegistry, cache, cacheCapacity, cacheMaxChars, and cacheEngineIdentity are read when the provider is created. Changing them after creation is ignored and emits one development warning; recreate the provider or plugin scope to apply a new value. defer, rootMargin, renderless, scopeTag, themes, scheme, engine, and languages remain reactive.

useSyntaxHighlighter ​

ts
const {
  result,
  lines,
  language,
  loading,
  busy,
  error,
  refresh,
  highlight,
  copy,
  getCopyPayload,
  controller,
} = useSyntaxHighlighter({
  code,
  language,
  engine,
  engineOptions,
  preserveTrailingNewline,
})

code and the other inputs accept refs or getters. The composable respects the nearest provider/plugin, exposes reactive state, and disposes its controller on scope unmount. copy() and getCopyPayload() use the same full-source clipboard behavior as the component.

preserveTrailingNewline defaults to false. Set it on the composable to change the default for every highlight() call, or pass preserveTrailingNewline per call to override it.

defineTheme ​

ts
const theme = defineTheme({ name, colorScheme, colors, tokens })

Required: name, colorScheme (light/dark), colors.background, colors.foreground. Optional: gutter, lineHighlight, selection, fontFamily, and style overrides for any TokenType. Invalid input throws an actionable TypeError; the result is deeply frozen.

ThemeDefinition:

ts
interface ThemeDefinition {
  name: string
  colorScheme: 'light' | 'dark'
  colors: {
    background: string
    foreground: string
    gutter: string
    lineHighlight: string
    selection: string
    fontFamily?: string
  }
  tokens: Partial<Record<TokenType, {
    color?: string
    fontStyle?: string
    fontWeight?: string
    textDecoration?: string
  }>>
}

Engine contract ​

ts
interface HighlightEngine {
  readonly id: string
  readonly capabilities?: { sync?: boolean }
  hasLanguage?(language: string): boolean
  loadLanguage?(language: string): Promise<void>
  highlight(
    request: { code: string; language: string; signal?: AbortSignal },
    options?: Record<string, unknown>,
  ): HighlightResult | Promise<HighlightResult>
  cacheKey?(request: HighlightRequest, options?: EngineOptions): string
  dispose?(): void
}

HighlightResult contains lines, language, and optional meta. Each line has tokens { text, type }; concatenating line text with \n reproduces the normalized source. Engines must not return HTML, colors, or access the DOM.

Token types ​

plain, keyword, string, regexp, comment, number, constant, function, class, type, variable, property, operator, punctuation, tag, attribute, decorator, meta, invalid.

Cache helpers ​

  • buildCacheKey({ engineId, language, code, options, engineIdentity? }) — stable, theme-independent key. engineIdentity is added only when engine-identity caching is enabled.
  • stableStringify(value) — deterministic serialization; functions receive stable identities.
  • ResultCache — LRU + character budget + in-flight deduplication; implements HighlightCacheLike.

Engine helpers ​

  • EngineManager — loads/caches adapters per owner; disposes only loader-created engines.
  • BUILTIN_ENGINE_IDS — shiki, highlightjs, lowlight, prism.
  • isHighlightEngine(value) — public contract guard.
  • createHighlighterContext(options, parent?, providesScope?, scopeModeOverride?) — low-level context factory. scopeModeOverride selects wrapper, renderless, or none; omitted values derive from providesScope.
  • HIGHLIGHTER_CONTEXT — injection key for advanced integrations.

Released under the MIT License.