2026-08-06 13:56:43 +08:00
|
|
|
/**
|
2026-08-06 14:58:33 +08:00
|
|
|
* 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).
|
2026-08-06 13:56:43 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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'
|
2026-08-06 14:37:56 +08:00
|
|
|
import { cjkFriendlyStrong } from './cjkFriendlyStrong.ts'
|
2026-08-06 13:56:43 +08:00
|
|
|
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, {
|
2026-08-06 14:37:56 +08:00
|
|
|
extensions: [gfm(), cjkFriendlyStrong()],
|
2026-08-06 13:56:43 +08:00
|
|
|
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, {
|
2026-08-06 14:37:56 +08:00
|
|
|
extensions: [gfm(), cjkFriendlyStrong(), mathCompatibility(), math()],
|
2026-08-06 13:56:43 +08:00
|
|
|
mdastExtensions: [gfmFromMarkdown(), mathFromMarkdown()],
|
|
|
|
|
})
|
|
|
|
|
}
|