feat: markdown 增量解析
This commit is contained in:
41
packages/client/ui-primitives/src/markdown/parse.ts
Normal file
41
packages/client/ui-primitives/src/markdown/parse.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* The markdown renderer's two mdast grammars, one per rendering arm. Both are
|
||||
* built from the same micromark extensions, so block boundaries and inline
|
||||
* semantics are identical wherever a document (or a document tail) is parsed:
|
||||
* the incremental streaming path, the settled path, and the plain-text
|
||||
* projection all agree on where blocks start and end.
|
||||
*/
|
||||
|
||||
import type { Root } from 'mdast'
|
||||
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 { mathCompatibility } from './mathCompatibility.ts'
|
||||
|
||||
/**
|
||||
* 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 fromMarkdown(text, {
|
||||
extensions: [gfm()],
|
||||
mdastExtensions: [gfmFromMarkdown()],
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 fromMarkdown(text, {
|
||||
extensions: [gfm(), mathCompatibility(), math()],
|
||||
mdastExtensions: [gfmFromMarkdown(), mathFromMarkdown()],
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user