import assert from "node:assert/strict"; import test from "node:test"; import React from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { createJiti } from "jiti"; const jiti = createJiti(import.meta.url, { jsx: { runtime: "automatic" }, tsconfigPaths: true, }); const { MarkdownBody } = await jiti.import("./MarkdownBody.tsx"); const { normalizeDisplayMath } = await jiti.import("../lib/markdown.ts"); function renderMarkdown(markdown) { return renderToStaticMarkup( React.createElement(MarkdownBody, { cwd: "/home/me/project", onOpenFile() {}, }, markdown), ); } test("opens non-file markdown links in a safe new tab", () => { const html = renderMarkdown("[docs](https://example.com/docs)"); assert.match( html, /]*href="https:\/\/example\.com\/docs")(?=[^>]*target="_blank")(?=[^>]*rel="noopener noreferrer")[^>]*>docs<\/a>/, ); assert.doesNotMatch(html, /\snode=/); }); test("keeps local file markdown links in the app", () => { const html = renderMarkdown("[file](components/MarkdownBody.tsx)"); assert.match(html, /file<\/a>/); assert.doesNotMatch(html, /target=|rel=|\snode=/); }); test("renders LaTeX parenthesis delimiters as inline math", () => { const html = renderMarkdown(String.raw`射线为 \(r_c = K^{-1}p\)。`); assert.match(html, /class="katex"/); assert.match(html, /r_c/); }); test("renders paired LaTeX bracket delimiters as display math", () => { const html = renderMarkdown(String.raw`\[ P(\lambda)=o_b+\lambda r_b \]`); const oneLineHtml = renderMarkdown(String.raw`\[P(\lambda)=o_b+\lambda r_b\]`); assert.match(html, /class="katex-display"/); assert.match(html, /lambda/); assert.match(oneLineHtml, /class="katex-display"/); }); test("leaves an unmatched LaTeX bracket delimiter unchanged", () => { const markdown = String.raw`before \[ x + y after`; assert.equal(normalizeDisplayMath(markdown), markdown); }); test("does not normalize LaTeX delimiters inside Markdown code", () => { const markdown = " \\(indented\\)\n\n`code\n\\(inline\\)`\n\n```text\n\\[\nfenced\n\\]\n```"; assert.equal(normalizeDisplayMath(markdown), markdown); }); test("does not normalize LaTeX delimiters inside raw HTML code", () => { const markdown = "\\(inline\\)\n\n
\n\\(block\\)\n
"; assert.equal(normalizeDisplayMath(markdown), markdown); }); test("does not normalize escaped delimiters or link destinations", () => { const escaped = String.raw`Literal: \\(x+y\\).`; const link = String.raw`[docs](https://example.com/\(manual\))`; assert.equal(normalizeDisplayMath(escaped), escaped); assert.equal(normalizeDisplayMath(link), link); });