API reference
Functions
| Export | Description |
|---|---|
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; exposeslanguageandtokenizer. ThrowsTypeErrorfor non-string input.CSSHighlightRenderer—render(source, tokens),clear(),dispose(). Merges into the sharedCSS.highlightsregistry undersh-*names.UnifiedTokenizer— the core classifier; construct with aLanguageDefinitionand calltokenize(source).UnifiedLexer— markup-aware lexer used internally.Lexer— base character-level lexer.
Constants
| Export | Description |
|---|---|
TokenType | Frozen map of token type names to values (TokenType.KEYWORD === "keyword"). |
WHITESPACE | The "whitespace" token type. |
ANSI_THEMES | All 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 / defaultLight | Default dark and light ANSI themes. |
semantics | The generic semantic registry for text.*, code.*, markup.*, and syntax.* metadata. |
DEFAULT_SELECTOR | Prism's default code-block selector, used by highlightAll. |
Types
| Export | Description |
|---|---|
Token | { type; start; end } with optional semantic metadata and UTF-16 offsets. |
TokenType | Union of token type strings ("keyword", "string", …). |
LanguageDefinition | name, aliases, word lists, lex, markup, features, and declaration keyword data. |
TokenizerFeatures | parameterBindings, contextStack, declarations, retroactiveRewrite, typeAnnotationAware, propertyKeys, classDetection, lineBreakResetsExpectation, styleSheet, yamlStructure, shellStructure. |
AnsiTheme / AnsiThemeName | Hex-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, plusANSI_THEMESfromansi/themes.