import { fileURLToPath } from "node:url";
import { createHtmlRenderer } from "@comark/html";
import { MarkdownDocument as ReactDocument } from "@comark/react";
import { MarkdownDocument as VueDocument } from "@comark/vue";
import { createSerializedMarkdownParser, parseMarkdown, type MarkdownDocument } from "comark";
import { createElement } from "react";
import { renderToString as renderReact } from "react-dom/server";
import type { LanguageRegistration } from "shiki";
import { createHighlighterCore } from "shiki/core";
import { createJavaScriptRegexEngine } from "shiki/engine/javascript";
import dark from "shiki/themes/github-dark.mjs";
import light from "shiki/themes/github-light.mjs";
import { build, type Rolldown } from "vite";
import { describe, expect, it, vi } from "vite-plus/test";
import { createSSRApp, h, type Component } from "vue";
import { renderToString as renderVue } from "vue/server-renderer";
import html, { mermaidScript, type PresetOptions } from "../src/html.ts";
import react from "../src/react.ts";
import vue, { components as vueComponents } from "../src/vue.ts";
import { components as lazyComponents } from "../src/vue-lazy.ts";
// No downloads and no Kroki, so the tests stay offline.
const offline: PresetOptions = {
chips: { download: false },
badge: { download: false },
icon: { fetch: false },
diagrams: { draw: false },
};
const renderHtml = (markdown: string, options: PresetOptions = {}) =>
createHtmlRenderer(html({ ...offline, ...options }))(markdown);
it("preserves git-graph source fences owned by a diff", async () => {
const markdown =
'::diff{file="history.log"}\n```git-graph\n* bbbbbbb aaaaaaa (HEAD -> main) Before change\n* aaaaaaa Initial commit\n```\n```git-graph\n* bbbbbbb aaaaaaa (HEAD -> main) After change\n* aaaaaaa Initial commit\n```\n::';
const options = html(offline);
const parsed = await parseMarkdown(markdown, options);
expect(parsed.meta.issues ?? []).toEqual([]);
const output = await renderHtml(markdown);
expect(output).toContain('class="cd-added">+1');
expect(output).toContain('class="cd-removed">−1');
expect(output.match(/
[!TIP]\n> Read this.",
"Released :badge[New]",
"::steps\n### Install\nRun it.\n::",
"::code-group\n```sh [pnpm]\npnpm add x\n```\n```sh [npm]\nnpm i x\n```\n::",
"",
"",
'::card{title="Docs" to="/guide"}\nRead it.\n::',
"```csv\nBird,Seen\nHeron,2\n```",
"Ducks{++ and geese++}.",
"```mermaid\nflowchart LR\n Draft --> Review\n```",
].join("\n\n");
const blocks = [
'class="cgc"',
'class="cs"',
'class="ct"',
'class="ck"',
"data-zoom",
'class="ccr"',
'class="cb"',
// comark-media adds the kind, as in `cm cm-video`.
'class="cm ',
" {
it("draws the links in the properties box as chips, like the document's own", async () => {
const output = await renderHtml(
"---\npr: https://example.com/acme/app/pull/7\ntags: [docs, https://example.com/spec]\n---\n\nSee [the spec](https://example.com/spec).",
{ chips: { download: false, favicon: (host) => `https://icons.example/${host}` } },
);
const box = output.slice(output.indexOf('"));
expect(box).toContain(
'',
);
expect(box).toContain('src="https://icons.example/example.com"');
expect(box).toMatch(/\s*docs\s*<\/span>/);
expect(box).not.toMatch(/cfm-tag">\s* {
const { plugins, components } = html(offline);
const out = await createHtmlRenderer({ plugins, components })(document);
for (const block of blocks) expect(out).toContain(block);
});
it("passes hosted badge options through each preset", async () => {
for (const preset of [html, vue, react]) {
const { plugins } = preset({
...offline,
badge: { endpoint: "https://example.com/badge.svg", download: false },
});
const document = await parseMarkdown(':badge{source="npm-version" package="comark-ui"}', {
plugins,
});
expect(JSON.stringify(document.nodes)).toContain("https://example.com/badge.svg?");
}
});
it("keeps a reference that is a chip's label as text, so no link holds another", async () => {
const out = await renderHtml(
"See [comarkdown/comark#525](https://github.com/comarkdown/comark/issues/525).",
{ github: { references: { repo: "onmax/comark-ui" } } },
);
expect(out).toContain('class="cp"');
expect(out.match(/ {
const { plugins, components } = html({ ...offline, zoom: false, github: { callouts: false } });
expect(Object.keys(components)).not.toContain("zoom");
expect(Object.keys(components)).not.toContain("github-callout");
const out = await createHtmlRenderer({ plugins, components })(document);
expect(out).not.toContain("data-zoom");
expect(out).not.toContain('class="cgc"');
});
it("themes Mermaid diagrams once per document, and turns them off", async () => {
const markdown =
"```mermaid\nsequenceDiagram\n A->>B: Hi\n```\n\n```mermaid\nflowchart LR\n A --> B\n```";
const out = await renderHtml(markdown);
expect(out.match(/", 2);
expect(out.trimStart()).toMatch(/^