// Generates tests/cases.weft from the CommonMark spec's examples and cmark-gfm's spec text. // // curl -o commonmark.json https://spec.commonmark.org/0.31.2/spec.json // curl -o gfm.txt https://raw.githubusercontent.com/github/cmark-gfm/master/test/spec.txt // node packages/markdown/scripts/gen-spec.mjs commonmark.json gfm.txt import { mkdirSync, readFileSync, writeFileSync } from "node:fs" import { dirname, join } from "node:path" import { fileURLToPath } from "node:url" const tests = join(dirname(fileURLToPath(import.meta.url)), "..", "tests") mkdirSync(tests, { recursive: true }) const commonmark = JSON.parse(readFileSync(process.argv[2], "utf8")).map((c) => ({ id: c.example, section: c.section, markdown: c.markdown, html: c.html, })) // cmark-gfm's spec.txt: examples are fenced by 32 backticks, `example` then the extension's name. const gfm = [] let section = "" let number = 0 const lines = readFileSync(process.argv[3], "utf8").split("\n") for (let i = 0; i < lines.length; i++) { const heading = lines[i].match(/^#{1,6} (.*)$/) if (heading) section = heading[1] const open = lines[i].match(/^`{32} example(.*)$/) if (!open) continue number++ const extensions = open[1].trim().split(/\s+/).filter(Boolean) let j = i + 1 const source = [] while (lines[j] !== ".") source.push(lines[j++]) j++ const html = [] while (!lines[j].startsWith("`".repeat(32))) html.push(lines[j++]) i = j // `disabled` examples describe behaviour cmark-gfm itself doesn't have. `tagfilter` is for pages that embed raw HTML: // here `html: "escape"` and `"sanitize"` already remove those tags, and `"trust"` means what it says. if (extensions.includes("disabled") || extensions.includes("tagfilter")) continue const text = (l) => l.map((x) => x + "\n").join("").replaceAll("→", "\t") gfm.push({ id: number, section, markdown: text(source), html: text(html), extensions }) } const str = (s) => JSON.stringify(s).replaceAll("\u2028", "\\u2028").replaceAll("\u2029", "\\u2029").replaceAll("$", "\\$") const row = (c) => ` { id: ${c.id}, section: ${str(c.section)}, markdown: ${str(c.markdown)}, html: ${str(c.html)}, gfm: ${(c.extensions ?? []).length > 0} },` writeFileSync( join(tests, "cases.weft"), `// Generated by scripts/gen-spec.mjs. Don't edit it. /// One example. With gfm set it needs GFM's extensions; cmark-gfm runs the others without them. export type Case = { id: number, section: string, markdown: string, html: string, gfm: boolean } /// The examples of the CommonMark spec, version 0.31.2. export let COMMONMARK: Case[] = [ ${commonmark.map(row).join("\n")} ] /// The examples of the GitHub Flavored Markdown spec. export let GFM: Case[] = [ ${gfm.map(row).join("\n")} ] `, ) console.log(`${commonmark.length} CommonMark examples, ${gfm.length} GFM examples`)