import { cp, mkdir, readFile, writeFile } from "node:fs/promises"; import { fileURLToPath } from "node:url"; const root = new URL("../", import.meta.url), output = new URL("vendor/text/", root); await mkdir(output, { recursive: true }); const files = { "marked/lib/marked.esm.js": "marked.js", "marked/LICENSE": "marked.LICENSE", "dompurify/dist/purify.es.mjs": "purify.js", "dompurify/LICENSE": "dompurify.LICENSE", "dompurify/LICENSE-MPL": "dompurify.MPL.LICENSE", "@highlightjs/cdn-assets/es/core.min.js": "highlight/core.js", "@highlightjs/cdn-assets/LICENSE": "highlight.LICENSE", }; const languages = [ "xml", "javascript", "typescript", "json", "yaml", "bash", "python", "css", "ini", "nginx", "sql", "markdown", ]; for (const language of languages) files[`@highlightjs/cdn-assets/es/languages/${language}.min.js`] = `highlight/languages/${language}.js`; for (const [source, target] of Object.entries(files)) { const to = new URL(target, output); await mkdir(fileURLToPath(new URL("./", to)), { recursive: true }); await cp(new URL(`node_modules/${source}`, root), to); if (target.endsWith(".js")) await writeFile( to, (await readFile(to, "utf8")).replace(/^\/\/# sourceMappingURL=.*$/gm, ""), ); } const versions = {}; for (const name of ["marked", "dompurify", "@highlightjs/cdn-assets"]) versions[name] = JSON.parse( await readFile(new URL(`node_modules/${name}/package.json`, root), "utf8"), ).version; await writeFile( new URL("manifest.json", output), JSON.stringify( { versions, languages, distribution: "Local browser ES modules; no CDN requests", }, null, 2, ) + "\n", ); console.log("Vendored Markdown, HTML sanitizer and syntax languages.");