import { readFile } from "node:fs/promises"; import { renderHtmlFromDocument } from "@comark/html"; import { MarkdownDocument as ReactDocument } from "@comark/react"; import { MarkdownDocument as VueDocument } from "@comark/vue"; import { type MarkdownDocument, parseMarkdown } from "comark"; import { createElement } from "react"; import { renderToString as renderReact } from "react-dom/server"; import { compile } from "svelte/compiler"; import { describe, expect, it } from "vite-plus/test"; import { createSSRApp, h } from "vue"; import { renderToString as renderVue } from "vue/server-renderer"; import fields from "../src/index.ts"; import { components as htmlComponents } from "../src/html.ts"; import { components as reactComponents } from "../src/react.ts"; import { components as vueComponents } from "../src/vue.ts"; const renderers = { html: (value: MarkdownDocument) => renderHtmlFromDocument(value, { components: htmlComponents }), vue: (value: MarkdownDocument) => renderVue(createSSRApp({ render: () => h(VueDocument, { value, components: vueComponents }) })), react: (value: MarkdownDocument) => renderReact(createElement(ReactDocument, { value, components: reactComponents })), }; // Drops what differs only in serialization: comments, whitespace, and the HTML renderer's inline styles. const normalize = (html: string) => html .replace(/]*>.*?<\/style>/gs, "") .replace(//g, "") .replace(/>\s+<") .replace(/\s+/g, " ") .trim(); describe("comark-fields renderers", () => { it("renders the same fields in every renderer", async () => { const value = await parseMarkdown( '::field-group{#props .wide}\n:::field{name="options" type="object" :required="true" default="{}"}\nThe **options**.\n\n::::field{name="sync" type="string"}\nA key.\n::::\n:::\n::', { plugins: [fields()] }, ); const expected = normalize(await renderers.html(value)); expect(expected).toContain('required'); for (const name of ["vue", "react"] as const) { const html = normalize(await renderers[name](value)); expect(html, name).toContain(expected); expect(html, name).not.toMatch(/id="props"|wide|undefined/); } }); it("ships Svelte components that compile", async () => { for (const name of ["Field", "FieldGroup"]) { const source = await readFile( new URL(`../src/svelte/${name}.svelte`, import.meta.url), "utf8", ); expect(() => compile(source, { filename: `${name}.svelte`, generate: "server" }), ).not.toThrow(); } }); });