import { describe, expect, it } from "vitest"; import { Marked } from "marked"; import DOMPurify from "dompurify"; import { mathExtension, renderMath } from "./markdownMath"; const marked = new Marked({ gfm: true, breaks: false }).use(mathExtension()); function parseSafe(md: string): HTMLElement { const html = marked.parse(md, { async: false }) as string; const root = DOMPurify.sanitize(html, { USE_PROFILES: { html: true }, ADD_ATTR: ["data-math"], FORBID_TAGS: ["style", "form"], FORBID_ATTR: ["style"], RETURN_DOM: true, }) as HTMLElement; renderMath(root); return root; } describe("markdown LaTeX math", () => { it("renders inline $...$ and avoids currency false positives", () => { const root = parseSafe( "Energy $E = mc^2$ and CJK 设$x=1$时.\n\nPrices: $5 and $10, or $5-$10, or $ 5 $, or \\$x\\$.", ); expect(root.querySelectorAll(".katex")).toHaveLength(2); expect(root.querySelectorAll(".katex-display")).toHaveLength(0); const paras = [...root.querySelectorAll("p")]; expect(paras[1]!.textContent).toBe("Prices: $5 and $10, or $5-$10, or $ 5 $, or $x$."); }); it("prevents * and _ inside math from triggering emphasis while allowing emphasis around math", () => { const root = parseSafe("Formula $(a)_1 + (b)_2$ and $a * b$ and $c * d$, plus **$x^2$**."); expect(root.querySelector("em")).toBeNull(); expect(root.querySelectorAll(".katex")).toHaveLength(4); expect(root.querySelector("strong .katex")).not.toBeNull(); }); it("renders multi-line, single-line, inline, and adjacent $$...$$ blocks", () => { const root = parseSafe( "Para\n$$\n\\begin{aligned}\na &= b \\\\\nc &= d\n\\end{aligned}\n$$\n\n$$E = mc^2$$\n\n$$a$$ and $$b$$", ); expect(root.querySelectorAll(".katex-display")).toHaveLength(4); expect(root.querySelector("p")?.textContent).toBe("Para"); const lastP = [...root.querySelectorAll("p")].at(-1)!; expect(lastP.querySelectorAll(".katex-display")).toHaveLength(2); expect(lastP.textContent).toContain("and"); }); it("renders \\(...\\) and \\[...\\] without breaking escaped links", () => { const root = parseSafe("Inline \\(x + y\\).\n\n\\[\nx^2 + y^2 = z^2\n\\]\n\n\\[text\\](https://example.com)"); expect(root.querySelectorAll(".katex")).toHaveLength(2); expect(root.querySelectorAll(".katex-display")).toHaveLength(1); const lastP = [...root.querySelectorAll("p")].at(-1)!; expect(lastP.textContent).toBe("[text](https://example.com)"); }); it("renders ```math code blocks while keeping ```latex and inline code literal", () => { const root = parseSafe("```math\n\\int_0^1 x^2 dx\n```\n\n```latex\n$x$\n```\n\nInline `$y$`."); expect(root.querySelectorAll(".katex-display")).toHaveLength(1); expect(root.querySelector("pre > code.language-latex")?.textContent).toBe("$x$\n"); expect(root.querySelector("p > code")?.textContent).toBe("$y$"); }); it("renders invalid LaTeX as an error node instead of throwing", () => { const root = parseSafe("Bad $\\unknowncommand{x}$ and $$\\frac{1$$"); expect(root.querySelectorAll(".katex")).toHaveLength(1); expect(root.querySelector(".katex-error")).not.toBeNull(); expect(root.textContent).toContain("\\unknowncommand"); }); it("blocks unsafe LaTeX trust commands and raw HTML style/script injection", () => { const root = parseSafe( [ "$\\href{javascript:alert(1)}{click}$", '