Open History to see the last commit
1/// <reference types="mdast-util-to-hast" />
2import type { Root } from "mdast";
3import remarkMath from "remark-math";
4import remarkParse from "remark-parse";
5import remarkGfm from "remark-gfm";
6import { unified } from "unified";
7
8const mathParser = unified()
9 .use(remarkParse)
10 .use(remarkGfm)
11 .use(remarkMath, { singleDollarTextMath: true })
12 .freeze();
13
14export function parseMarkdownWithMath(source: string) {
15 return mathParser.parse(source);
16}
17
18/** Closed, parser-owned formulas with authored source for editors. */
19export function markdownMathRanges(source: string, tree?: Root) {
20 const ranges: Array<{
21 start: number;
22 end: number;
23 source: string;
24 tex: string;
25 display: boolean;
26 inTable: boolean;
27 }> = [];
28 if (!mayContainMath(source)) return ranges;
29 const sourceOffset = source.startsWith("\uFEFF") ? 1 : 0;
30 const visit = (node: Root | Root["children"][number], inTable = false) => {
31 const table = inTable || node.type === "tableCell";
32 if (
33 node.type === "math" ||
34 node.type === "inlineMath" ||
35 (node.type === "code" && node.lang === "math")
36 ) {
37 const parserStart = node.position?.start.offset;
38 const parserEnd = node.position?.end.offset;
39 if (parserStart === undefined || parserEnd === undefined) return;
40 const start = parserStart + sourceOffset;
41 const end = parserEnd + sourceOffset;
42 const authored = source.slice(start, end);
43 if (node.type === "code") {
44 const open = /^[ \t]*(`{3,}|~{3,})/.exec(authored)?.[1];
45 const close = /\n[ \t>]*(`{3,}|~{3,})[ \t\r]*$/.exec(authored)?.[1];
46 if (!open || !close || open[0] !== close[0] || close.length < open.length) return;
47 }
48 const display = node.type !== "inlineMath" || /^(?:\$\$|\\\[)/.test(authored);
49 ranges.push({ start, end, source: authored, tex: node.value, display, inTable: table });
50 } else if ("children" in node) node.children.forEach((child) => visit(child, table));
51 };
52 visit(tree ?? parseMarkdownWithMath(source));
53 return ranges;
54}
55
56/** Messages without these characters skip math parsing entirely. */
57function mayContainMath(text: string): boolean {
58 return (
59 mayContainDollarMath(text) ||
60 text.includes("\\(") ||
61 text.includes("\\[") ||
62 /(?:`{3,}|~{3,})[ \t]*math\b/.test(text)
63 );
64}
65
66/**
67 * A `$` formula needs a closing `$` (or the second `$` of `$$`) after another `$` on its line and
68 * not right after a space (micromark reads a tab as virtual spaces, which still close). So `$skill`
69 * chips and prices skip the parser.
70 */
71function mayContainDollarMath(text: string): boolean {
72 let opened = false;
73 for (let index = 0; index < text.length; index += 1) {
74 const char = text[index];
75 if (char === "\n" || char === "\r") {
76 opened = false;
77 } else if (char === "$") {
78 if (opened && text[index - 1] !== " ") return true;
79 opened = true;
80 }
81 }
82 return false;
83}
84
85export function mathKind(className: string | undefined): "inline" | "display" | null {
86 if (!className?.includes("language-math")) return null;
87 if (className.includes("math-display")) return "display";
88 return className.includes("math-inline") ? "inline" : null;
89}