Files
deepseek-harness/packages/client/ui-primitives/src/markdown/parse.ts
T
yudshj 1d9c0ab757 revert(markdown): remove PR #4484 display-dollar parser
Revert PR #4484 (merge b9bdd062c8f), retaining later Windows-path and GFM changes. Its display-dollar rules are replaced by upstream math parsing with bounded invalid-block recovery in the next commit.
2026-10-30 14:16:26 +08:00

50 lines
2.0 KiB
TypeScript

/**
* The markdown renderer's two mdast grammars, one per rendering arm. Each
* arm is internally consistent — the incremental tail parses, the one-shot
* parses, and the plain-text projection of a given grammar always agree on
* where blocks start and end — and the settled grammar is the streaming one
* plus the math extensions, so the arms differ only where TeX delimiters
* begin a math construct (a `$$` block is a paragraph while streaming and a
* math block once settled, by design).
*/
import type { Root } from 'mdast'
import { recoverLocalImages } from './local-image-syntax.ts'
import { fromMarkdown } from 'mdast-util-from-markdown'
import { gfmFromMarkdown } from 'mdast-util-gfm'
import { mathFromMarkdown } from 'mdast-util-math'
import { gfm } from 'micromark-extension-gfm'
import { math } from 'micromark-extension-math'
import { cjkFriendlyStrong } from './cjkFriendlyStrong.ts'
import { mathCompatibility } from './mathCompatibility.ts'
import { windowsPathDestinations } from './windows-path-destinations.ts'
/** GFM strikethrough requires `~~`; a single `~` stays literal, as in ranges such as `10~20`. */
const gfmOptions = { singleTilde: false }
/**
* Parse GFM markdown (the streaming arm's grammar: no math, so incomplete
* TeX never flashes KaTeX errors mid-stream).
* @param text - Markdown source.
* @returns The mdast root.
*/
export function parseGfm(text: string): Root {
return recoverLocalImages(fromMarkdown(text, {
extensions: [gfm(gfmOptions), cjkFriendlyStrong()],
mdastExtensions: [gfmFromMarkdown(), windowsPathDestinations()],
}), text)
}
/**
* Parse GFM markdown plus TeX math with the compatibility delimiters
* (the settled arm's grammar).
* @param text - Markdown source.
* @returns The mdast root.
*/
export function parseGfmWithMath(text: string): Root {
return recoverLocalImages(fromMarkdown(text, {
extensions: [gfm(gfmOptions), cjkFriendlyStrong(), mathCompatibility(), math()],
mdastExtensions: [gfmFromMarkdown(), mathFromMarkdown(), windowsPathDestinations()],
}), text)
}