@streamdown/code package provides syntax highlighting powered by Shiki. It implements the CodeHighlighterPlugin interface and integrates with the plugins.code field on <Streamdown>.
Installation
Quick start
createCodePlugin()
Creates aCodeHighlighterPlugin instance with optional configuration.
Optional configuration for the plugin.
CodeHighlighterPlugin interface
Discriminant for the plugin union type. Always
"code-highlighter".Plugin identifier. Always
"shiki".highlight
(options: HighlightOptions, callback?: (result: HighlightResult) => void) => HighlightResult | null
required
Highlights code and returns a token result. Returns
null synchronously when the Shiki highlighter is still loading; the optional callback is called with the result once highlighting completes. Subsequent calls with the same input return the cached result immediately.Returns the full list of language identifiers supported by the bundled Shiki distribution.
Returns the
[lightTheme, darkTheme] tuple that was provided at construction time (or the defaults). Streamdown reads this value to synchronise the theme tuple across the component tree.Returns
true if language (after alias normalisation) is in the set of bundled Shiki languages. Language aliases such as "js" for "javascript" are resolved automatically.HighlightOptions
Passed as the first argument tohighlight().
The source code string to highlight.
The language identifier. Aliases (e.g.
"js", "py") are resolved automatically.The
[lightTheme, darkTheme] tuple to use for this highlight call. Typically sourced from getThemes() or the shikiTheme prop on <Streamdown>.HighlightResult
The value returned by a successfulhighlight() call or delivered to the callback. Compatible with Shiki’s TokensResult.
A two-dimensional array of tokens. The outer array represents lines; the inner array represents individual tokens on each line.
Background colour for the code block (CSS colour string), derived from the active theme.
Default foreground colour for the code block (CSS colour string), derived from the active theme.
Inline styles to apply to the root element of the highlighted output, as a CSS string.
false when no root styles are needed.HighlightToken
A single token within a highlighted line.The text content of this token.
Foreground colour for this token as a CSS colour string.
Background colour for this token as a CSS colour string.
Additional HTML attributes to apply to the token’s
<span> element.Additional inline styles to apply to the token’s
<span> element.Character offset of this token from the start of the line.
ThemeInput type
BundledTheme is a string union of all themes shipped with Shiki (e.g. "github-light", "nord", "dracula"). ThemeRegistrationAny accepts a custom theme object loaded via the Shiki theme registration API.
Caching behaviour
The plugin maintains a module-level token cache keyed onlanguage + themes + code. The first call for a given combination triggers async highlighter initialisation. Subsequent calls with the same key return the cached result synchronously without re-highlighting.
Highlighter instances are also cached per
(language, lightTheme, darkTheme) combination. This means the first call for a new language may take a moment to load the grammar, but all subsequent calls are instant.