import { describe, expect, it } from "vitest"; import { parseBlocks } from "./blocks.js"; import { escapeHtml, renderBlockHtml } from "./render.js"; const render = (markdown: string): string => { const [block] = parseBlocks(markdown); if (!block) throw new Error(`no block parsed from: ${markdown}`); return renderBlockHtml(block); }; describe("escapeHtml", () => { it("escapes the five entities", () => { expect(escapeHtml(`&`)).toBe( "<a href="x" title='y'>&", ); }); }); describe("renderBlockHtml", () => { it("never passes raw HTML through", () => { const html = render(""); expect(html).toBe("

<script>alert(1)</script>

"); }); it("renders inline code, bold, italic and links", () => { const html = render("use `plan_submit` and **never** *skip* [the spec](https://example.com/x)"); expect(html).toContain("plan_submit"); expect(html).toContain("never"); expect(html).toContain("skip"); expect(html).toContain('href="https://example.com/x"'); expect(html).toContain('rel="noreferrer noopener"'); }); it("keeps HTML escaped inside inline code", () => { expect(render("``")).toBe("

<b>

"); }); it("renders headings at their level", () => { expect(render("### Step three")).toBe("

Step three

"); }); it("strips closing hashes from a heading", () => { expect(render("## Title ##")).toBe("

Title

"); }); it("renders a code fence verbatim with its language", () => { const html = render("```ts\nconst a = 1 < 2;\n\nif (a) run();\n```"); expect(html).toBe( '
const a = 1 < 2;\n\nif (a) run();
', ); }); it("keeps the last line of an unclosed code fence", () => { const html = render("```ts\nconst a = 1;\nconst b = 2;"); expect(html).toBe( '
const a = 1;\nconst b = 2;
', ); }); it("does not close a fence on a line that has an info string", () => { expect(render("```\nfirst\n```ts\nlast")).toBe( "
first\n```ts\nlast
", ); }); it("does not run inline markup inside code", () => { expect(render("```\n**not bold** \n```")).toBe( "
**not bold** <tag>
", ); }); it("renders unordered and ordered lists", () => { expect(render("- one\n- two")).toBe("
  • one
  • two
"); expect(render("1. one\n2. two")).toBe("
  1. one
  2. two
"); // a continuation line belongs to the item above it expect(render("- one\n continued")).toBe("
  • one continued
"); }); it("renders block quotes with their marker stripped", () => { expect(render("> quoted text")).toBe("

quoted text

"); }); it("renders a table with a header and body", () => { const html = render("| a | b |\n| --- | --- |\n| 1 | 2 |"); expect(html).toBe( "" + "
ab
12
", ); }); it("joins prose lines with
", () => { expect(render("first line\nsecond line")).toBe("

first line
second line

"); }); });