@tanstack/highlight/markdown converts Markdown code-fence inputs into rendered block data or HAST. It does not parse Markdown documents.
type TanStackMarkdownHighlighterOptions = {
highlightLines?: ReadonlyArray<number>
lineNumbers?: boolean
}A synchronous callback matching TanStack Markdown's code-highlighter contract without importing @tanstack/markdown.
function createTanStackMarkdownHighlighter(
highlighter: Highlighter,
): TanStackMarkdownHighlighterReturns escaped inner token markup for a renderer-owned <code> element. Highlighted lines receive th-line--highlighted; unknown languages use the highlighter's configured fallback.
function parseCodeDiffNotation(code: string): {
code: string
decorations: Array<HighlightDecoration>
}Removes trailing [!code ++] and [!code --] directives and returns th-line--inserted and th-line--deleted decorations for their one-based line numbers. JavaScript-style line and block comments, # line comments, and HTML comments are supported.
type CodeFenceMeta = {
decorations: Array<HighlightDecoration>
lineNumbers: boolean
title?: string
}function parseCodeFenceMeta(meta?: string | null): CodeFenceMetaRecognizes title, filename, file, name, lineNumbers, showLineNumbers, shorthand highlighted lines, and named line annotations. Invalid line fragments are ignored. See Annotations.
function getCodeFenceTitle(meta?: string | null): string | undefinedReturns the trimmed value of the first title, filename, file, or name assignment. Single-quoted, double-quoted, and unquoted values are accepted.
type CodeFenceInput = {
code: string
decorations?: ReadonlyArray<HighlightDecoration>
lang?: string | null
lineNumbers?: boolean
meta?: string | null
title?: string | null
}Inline diff notation is converted first, followed by metadata and explicit decorations. Explicit lineNumbers takes precedence over metadata. Explicit title takes precedence when it is non-empty.
RenderedCodeBlockData plus the resolved decorations and lineNumbers.
function renderCodeFence(
input: CodeFenceInput,
highlighter: Highlighter,
): HighlightedCodeFenceParses inline diff notation and metadata, then delegates to the supplied highlighter.
A HAST-compatible text node with type: 'text' and value.
A HAST-compatible element node with type, tagName, optional properties, and recursive children.
function codeFenceToHast(
input: CodeFenceInput,
highlighter: Highlighter,
): HastElementReturns a <pre class="th-code th-code--LANG"><code>...</code></pre> HAST tree. A title is exposed as dataTitle on the pre node.
function tokensToHast(
tokens: ReadonlyArray<HighlightToken>,
lang: string,
options?: {
decorations?: ReadonlyArray<HighlightDecoration>
lineNumbers?: boolean
title?: string
},
): HastElementRenders existing tokens to the same HAST structure without tokenizing code.