/** * Find all HTML files generated by illuaminate and pipe them through a remark. * * This performs compile-time syntax highlighting and expands our custom * components using React SSR. * * Yes, this would be so much nicer with next.js. */ import * as fs from "fs/promises"; import globModule from "glob"; import * as path from "path"; import { createElement, createElement as h, Fragment } from 'react'; import { renderToStaticMarkup } from "react-dom/server"; import rehypeHighlight from "rehype-highlight"; import rehypeParse from 'rehype-parse'; import rehypeReact from 'rehype-react'; import { unified } from 'unified'; import { promisify } from "util"; // Our components import Recipe from "./components/Recipe.js"; import { noChildren } from "./components/support.js"; import { DataExport, WithExport } from "./components/WithExport.js"; const glob = promisify(globModule); (async () => { const base = "build/illuaminate"; const processor = unified() .use(rehypeParse, { emitParseErrors: true }) .use(rehypeHighlight, { prefix: "" }) .use(rehypeReact, { createElement, Fragment, passNode: false, components: { ['mc-recipe']: noChildren(Recipe), ['mcrecipe']: noChildren(Recipe), } as any }); const dataExport = JSON.parse(await fs.readFile("src/export/index.json", "utf-8")) as DataExport; for (const file of await glob(base + "/**/*.html")) { const contents = await fs.readFile(file, "utf-8"); const { result } = await processor.process(contents); const outputPath = path.resolve("build/jsxDocs", path.relative(base, file)); await fs.mkdir(path.dirname(outputPath), { recursive: true }); await fs.writeFile(outputPath, "" + renderToStaticMarkup({result})); } })();