// The markdown subset (C7), ported from ui/chat/Markdown.kt. The parser is pure, // so the exact block/span shapes are asserted without a browser; anything outside // the subset must render as plain text and never be dropped. import { test } from "node:test"; import assert from "node:assert/strict"; import { codeBlockLabel, decodeHtmlEntities, parseInline, parseMarkdown } from "../src/markdown.js"; /** Every text run in a span tree, in order, so nothing dropped can hide. */ function spanText(spans) { return spans .map((span) => { if (span.kind === "plain" || span.kind === "code") return span.text; if (span.kind === "strong" || span.kind === "emphasis" || span.kind === "link") { return spanText(span.spans); } return ""; }) .join(""); } /** Every text run in a block list, so nothing dropped can hide. */ function blockText(blocks) { return blocks .map((block) => { if (block.kind === "heading" || block.kind === "paragraph") return block.text; if (block.kind === "code") return block.code; if (block.kind === "quote") return block.lines.join("\n"); if (block.kind === "list") return block.items.join("\n"); if (block.kind === "table") return [...block.header, ...block.rows.flat()].join("|"); return ""; }) .join("\n"); } // --- Entities --------------------------------------------------------------- test("decodeHtmlEntities decodes the named, decimal and hex references", () => { assert.equal(decodeHtmlEntities("a & b < c > d "e" 'f'"), "a & b < c > d \"e\" 'f'"); assert.equal(decodeHtmlEntities("AB"), "AB"); assert.equal(decodeHtmlEntities("…— "), "…—\u00A0"); }); test("decodeHtmlEntities is a single pass, so a decoded ampersand is not re-decoded", () => { assert.equal(decodeHtmlEntities("&lt;"), "<"); assert.equal(decodeHtmlEntities("&amp;"), "&"); }); test("decodeHtmlEntities leaves an unknown or malformed reference literal", () => { assert.equal(decodeHtmlEntities("&nope; &doesnotexist;"), "&nope; &doesnotexist;"); assert.equal(decodeHtmlEntities("&#xZZ; &#; &"), "&#xZZ; &#; &"); }); test("parseMarkdown decodes entities before parsing", () => { const blocks = parseMarkdown("a & b"); assert.deepEqual(blocks, [{ kind: "paragraph", text: "a & b" }]); }); // --- Blocks ----------------------------------------------------------------- test("parseMarkdown parses headings, paragraphs and thematic rules", () => { const blocks = parseMarkdown("# Title\n\nA paragraph\nspanning lines.\n\n---\n\n## Sub ##"); assert.deepEqual(blocks, [ { kind: "heading", level: 1, text: "Title" }, { kind: "paragraph", text: "A paragraph spanning lines." }, { kind: "rule" }, { kind: "heading", level: 2, text: "Sub " }, ]); }); test("parseMarkdown parses a fenced block and keeps its language", () => { const blocks = parseMarkdown("```js\nconst x = 1;\n```"); assert.deepEqual(blocks, [{ kind: "code", language: "js", code: "const x = 1;" }]); assert.equal(codeBlockLabel("js"), "js"); }); test("a fenced block with no language is labelled code", () => { const blocks = parseMarkdown("```\nplain\n```"); assert.deepEqual(blocks, [{ kind: "code", language: "", code: "plain" }]); assert.equal(codeBlockLabel(""), "code"); assert.equal(codeBlockLabel(" "), "code"); }); test("tilde fences are code fences too", () => { const blocks = parseMarkdown("~~~python\nprint(1)\n~~~"); assert.deepEqual(blocks, [{ kind: "code", language: "python", code: "print(1)" }]); }); test("parseMarkdown parses block quotes, bullet lists and ordered lists", () => { assert.deepEqual(parseMarkdown("> one\n> two"), [{ kind: "quote", lines: ["one", "two"] }]); assert.deepEqual(parseMarkdown("- a\n- b"), [{ kind: "list", ordered: false, items: ["a", "b"] }]); assert.deepEqual(parseMarkdown("1. one\n2. two"), [{ kind: "list", ordered: true, items: ["one", "two"] }]); }); test("parseMarkdown parses a table with a --- delimiter row", () => { const blocks = parseMarkdown("| a | b |\n| --- | --- |\n| 1 | 2 |"); assert.deepEqual(blocks, [{ kind: "table", header: ["a", "b"], rows: [["1", "2"]] }]); }); test("a table row shorter than the header is padded, never truncated", () => { const blocks = parseMarkdown("| a | b |\n| --- | --- |\n| 1 |"); assert.deepEqual(blocks[0].rows, [["1", ""]]); }); test("a nested list is flattened into its run, never dropped", () => { const blocks = parseMarkdown("- a\n - b"); assert.deepEqual(blocks, [{ kind: "list", ordered: false, items: ["a", "b"] }]); assert.equal(blockText(blocks), "a\nb"); }); test("more than six hashes is a paragraph, not a heading", () => { assert.deepEqual(parseMarkdown("####### x"), [{ kind: "paragraph", text: "####### x" }]); }); // --- Inline ----------------------------------------------------------------- test("snake_case is never italicised", () => { assert.deepEqual(parseInline("snake_case_name"), [{ kind: "plain", text: "snake_case_name" }]); assert.deepEqual(parseInline("a_b_c"), [{ kind: "plain", text: "a_b_c" }]); }); test("underscore emphasis works only at word boundaries", () => { assert.deepEqual(parseInline("_italic_"), [{ kind: "emphasis", spans: [{ kind: "plain", text: "italic" }] }]); assert.deepEqual(parseInline("a _b_ c"), [ { kind: "plain", text: "a " }, { kind: "emphasis", spans: [{ kind: "plain", text: "b" }] }, { kind: "plain", text: " c" }, ]); }); test("bold, italic and inline code become their spans", () => { assert.deepEqual(parseInline("**bold**"), [{ kind: "strong", spans: [{ kind: "plain", text: "bold" }] }]); assert.deepEqual(parseInline("__bold__"), [{ kind: "strong", spans: [{ kind: "plain", text: "bold" }] }]); assert.deepEqual(parseInline("*it*"), [{ kind: "emphasis", spans: [{ kind: "plain", text: "it" }] }]); assert.deepEqual(parseInline("`code`"), [{ kind: "code", text: "code" }]); }); test("links and bare http(s) autolinks become link spans", () => { assert.deepEqual(parseInline("[site](https://example.com)"), [ { kind: "link", spans: [{ kind: "plain", text: "site" }], url: "https://example.com" }, ]); assert.deepEqual(parseInline("see https://example.com now"), [ { kind: "plain", text: "see " }, { kind: "link", spans: [{ kind: "plain", text: "https://example.com" }], url: "https://example.com" }, { kind: "plain", text: " now" }, ]); }); test("autolinks trim trailing punctuation", () => { const spans = parseInline("go to https://example.com."); const link = spans.find((span) => span.kind === "link"); assert.equal(link.url, "https://example.com"); }); // --- Out-of-subset text is never dropped ------------------------------------ test("out-of-subset HTML renders as plain text and is never dropped", () => { assert.deepEqual(parseInline("hi"), [{ kind: "plain", text: "hi" }]); assert.equal(blockText(parseMarkdown("
x
")), "
x
"); }); test("reference links and strikethrough render as plain text", () => { assert.deepEqual(parseInline("[label][ref]"), [{ kind: "plain", text: "[label][ref]" }]); assert.deepEqual(parseInline("~~gone~~"), [{ kind: "plain", text: "~~gone~~" }]); }); test("a malformed inline marker is kept literally", () => { assert.deepEqual(parseInline("a * b ` c"), [{ kind: "plain", text: "a * b ` c" }]); }); test("every out-of-subset paragraph's text survives the round trip", () => { const source = "
\n[ref][id] and ~~strike~~\n
"; const blocks = parseMarkdown(source); const text = blockText(blocks); assert.ok(text.includes("[ref][id] and ~~strike~~")); }); test("spanText of a mixed inline run keeps every character", () => { const spans = parseInline("plain **bold** `code` [link](https://e.com) tail"); assert.equal(spanText(spans), "plain bold code link tail"); });