import assert from "node:assert/strict"; import test from "node:test"; import { markdown, markdownLanguage } from "@codemirror/lang-markdown"; import { noteFormats } from "../src/lib/editor/noteFormats.ts"; import { renderMarkdownHtml } from "../src/lib/workspace/noteHtml.ts"; const parser = markdown({ base: markdownLanguage, extensions: [noteFormats] }).language.parser; const render = (source: string) => renderMarkdownHtml(source, new Map()); function mathNodes(source: string) { const nodes: string[] = []; parser.parse(source).iterate({ enter(node) { if (node.name === "NoteMath") nodes.push(source.slice(node.from, node.to)); } }); return nodes; } test("dollar amounts stay literal in writing, reading, and table cells", () => { for (const source of [ "$10", "$10 and $20", "$1,234.56 plus $78.90", "$10-$20", "$0.25-$.50", "US$10 / CA$20", "$ 10 and $ 20", "$-10 and $-20", "($10) and ($20)", "$10\n$20", "$10\r\n$20", "$10 **income**, $20 **cost**", ]) { assert.deepEqual(mathNodes(source), [], source); const html = render(source); assert.doesNotMatch(html, /md-inline-math/, source); assert.equal((html.match(/\$/g) || []).length, (source.match(/\$/g) || []).length, source); assert.doesNotMatch(render(`| Amounts |\n| --- |\n| ${source.replace(/\r?\n/g, "
")} |`), /md-inline-math/, source); } }); test("inline math still works next to dollar amounts", () => { for (const expression of ["$x^2$", "$2 + 2$", "$x + y$", String.raw`$\frac{1}{2}$`, String.raw`$x + \$5$`]) { const source = `$10 and $20, then ${expression}, then $30 and $40.`; assert.deepEqual(mathNodes(source), [expression], source); assert.equal((render(source).match(/md-inline-math/g) || []).length, 1, source); assert.match(render(source), /\$10 and \$20/); assert.match(render(source), /\$30 and \$40/); } }); test("escaped, incomplete, spaced, multiline, and double delimiters do not create inline math", () => { for (const source of [String.raw`\$x\$`, "$x", "$ x$", "$x $", "$x\ny$", "$$x$$", "$$\nx\n$$", "`$x$`", "```\n$x$\n```", "$x$2"]) { assert.deepEqual(mathNodes(source), [], source); assert.doesNotMatch(render(source), /md-inline-math/, source); } }); test("highlight and comment formats keep their existing behavior", () => { const source = "==**highlight**== %%comment%%"; const tree = parser.parse(source).toString(); assert.match(tree, /NoteHighlight/); assert.match(tree, /StrongEmphasis/); assert.match(tree, /NoteComment/); assert.match(render(source), /highlight<\/strong><\/mark>/); assert.match(render(source), /md-inline-comment/); });