import { createHtmlRenderer } from "@comark/html"; import { createMarkdownParser, parseMarkdown, type ParserOptions } from "comark"; import footnotes from "comark/plugins/footnotes"; import highlight from "comark/plugins/highlight"; import { textContent } from "comark/utils"; import { describe, expect, it } from "vite-plus/test"; import mark from "../src/index.ts"; const flat = (html: string) => html.replace(/\s*\n\s*/g, ""); const render = async (markdown: string, options: ParserOptions = {}) => flat( await createHtmlRenderer({ ...options, plugins: [mark(), ...(options.plugins ?? [])] })( markdown, ), ); describe("comark-mark", () => { it("renders ==text== as mark and ^text^ as sup, with Markdown inside", async () => { expect(await render("The ==deploy freeze== starts at 10^30^, ==not **after** lunch==.")).toBe( "
The deploy freeze starts at 1030, " + "not after lunch.
", ); }); it("keeps the AST to plain mark and sup nodes", async () => { const { nodes } = await parseMarkdown("==a== x^2^", { plugins: [mark()] }); expect(nodes).toEqual([["p", {}, ["mark", {}, "a"], " x", ["sup", {}, "2"]]]); }); it("leaves single =, spaced carets, code, and ~sub~ alone", async () => { expect(await render("a = b, ^not this^, `==code==`, H~2~O")).toBe( "a = b, ^not this^, ==code==, H~2~O
210 3
"); }); it("takes {attributes} after a mark", async () => { expect(await render("==hot=={.warning}")).toBe('hot
'); }); it("doesn't read footnote references as superscripts", async () => { const markdown = "Pigeons[^1] and ducks[^2].\n\n[^1]: On the roof.\n[^2]: In the bath."; const options = { plugins: [footnotes()] }; expect(await render(markdown, options)).toBe(flat(await createHtmlRenderer(options)(markdown))); expect(await render("x[^1][^2]", { registerDefaultPlugins: false })).toBe("x[^1][^2]
"); expect(await render("x[^1]")).toBe("x[^1]
"); }); it("leaves footnote references in highlighted code as written", async () => { // Shiki splits `[^1]` into spans, and one of them holds `^1` alone, as an unresolved reference would. A docs site // highlights before its own plugins run. const source = "Pigeons[^1].\n\n[^1]: On the roof."; const { nodes } = await parseMarkdown(`\`\`\`md\n${source}\n\`\`\``, { plugins: [highlight(), mark()], }); expect(textContent(nodes[0]!)).toBe(source); }); it("renders without Comark's default plugins", async () => { expect(await render("==a== b^2^", { registerDefaultPlugins: false })).toBe( "a b2
", ); }); it("leaves an unclosed mark as text while streaming", async () => { const parse = createMarkdownParser({ plugins: [mark()] }); await parse("The ==deploy", { streaming: true }); expect((await parse("The ==deploy freeze", { streaming: true })).nodes).toMatchObject([ ["p", {}, "The ==deploy freeze"], ]); expect((await parse("The ==deploy freeze== ends", { streaming: true })).nodes).toMatchObject([ ["p", {}, "The ", ["mark", {}, "deploy freeze"], " ends"], ]); expect(await render("**bold ==mark")).toBe("bold ==mark
"); }); });