API reference

Functions

ExportDescription
createHighlighter(options?)Creates a Highlighter. Loads the language definition asynchronously. Returns Promise<Highlighter>.
highlightElement(element, source, options?)One-shot browser highlighting. Returns Promise<HighlightHandle> with refresh() and dispose().
highlightAll({ mode, root?, selector?, defaultLanguage?, prefix? })Highlights every matching code block. mode ("highlight" or "html") is required. Returns Promise<HighlightAllResult> with highlighted, skipped, and dispose().
highlightCode(code, { language?, prefix? })Code string → escaped span HTML. Unknown or missing language → escaped plain text with language: null. Returns Promise<HighlightCodeResult>.
supportsHighlightAPI()Whether the CSS Custom Highlight API is available.
languageOf(element)The language-*/lang-* class on an element or its nearest ancestor, or null.
renderHTML(source, tokens, options?)Escaped HTML string with <span class="sh-{type}"> wrappers.
renderANSI(source, tokens, options?)ANSI truecolor string for terminal output.
renderJSON(source, tokens)JSON string of the token stream.
validateTokens(source, tokens)Throws TypeError/RangeError/Error on invalid tokens; returns void when valid.
hexToAnsi(hex)Converts "#rrggbb" to an ANSI truecolor SGR sequence.
escapeControlChars(text)Rewrites terminal control characters in caret notation (ESC → ^[), keeping tab, LF, and CR. renderANSI applies it by default.
registerLanguage(def)Registers a LanguageDefinition.
getRegisteredLanguages()Array of registered language names.
loadLanguage(name)Loads a definition by name. Throws UnknownLanguageError for unknown names.
createToken(type, start, end)Creates a Token object.
isSignificant(token)true unless the token is whitespace.

Classes

  • Highlighter — highlight(source) tokenizes; exposes language and tokenizer. Throws TypeError for non-string input.

  • CSSHighlightRenderer — render(source, tokens), clear(), dispose(). Merges into the shared CSS.highlights registry under sh-* names.

  • UnifiedTokenizer — the core classifier; construct with a LanguageDefinition and call tokenize(source).

  • UnifiedLexer — markup-aware lexer used internally.

  • Lexer — base character-level lexer.

Constants

ExportDescription
TokenTypeFrozen map of token type names to values (TokenType.KEYWORD === "keyword").
WHITESPACEThe "whitespace" token type.
ANSI_THEMESAll built-in ANSI themes keyed by name (github and solarized are aliases).
ANSI_PALETTES{ dark, light } pair for auto switching.
ANSI_RESET"\x1b[0m".
defaultTheme / defaultLightDefault dark and light ANSI themes.
semanticsThe generic semantic registry for text.*, code.*, markup.*, and syntax.* metadata.
DEFAULT_SELECTORPrism's default code-block selector, used by highlightAll.

Types

ExportDescription
Token{ type; start; end } with optional semantic metadata and UTF-16 offsets.
TokenTypeUnion of token type strings ("keyword", "string", …).
LanguageDefinitionname, aliases, word lists, lex, markup, features, and declaration keyword data.
TokenizerFeaturesparameterBindings, contextStack, declarations, retroactiveRewrite, typeAnnotationAware, propertyKeys, classDetection, lineBreakResetsExpectation, styleSheet, yamlStructure, shellStructure.
AnsiTheme / AnsiThemeNameHex-color map and its key union.
AnsiRenderOptions{ theme?, color?, wrapWhitespace?, allowControlChars? }.
HtmlRenderOptions{ prefix?, wrapWhitespace? }.
HighlightOptions{ language?, debounceMs? }.
HighlightHandle{ highlighter, renderer, refresh(), dispose() }.
HighlightMode"highlight" (CSS Custom Highlight API) or "html" (span markup).
HighlightAllOptions / HighlightAllResult{ mode, root?, selector?, defaultLanguage?, prefix? } / { highlighted, skipped, dispose() }.
SkippedBlock{ element, language, reason: "no-language" | "unknown-language" }.
HighlightCodeOptions / HighlightCodeResult{ language?, prefix? } / { html, language } (language is null when not highlighted).

Theme subpaths

  • @opentf/syntax-highlighter/themes/* — CSS themes (default, dark-plus, light-plus, dracula, …).

  • @opentf/syntax-highlighter/ansi/themes/* — one ANSI theme per module, plus ANSI_THEMES from ansi/themes.

Last updated on
Edit this page