CC-Tweaked/projects/web/src/transform.tsx

51 lines
1.8 KiB
TypeScript

/**
* 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 glob 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';
// Our components
import Recipe from "./components/Recipe.js";
import { noChildren } from "./components/support.js";
import { DataExport, WithExport } from "./components/WithExport.js";
(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, "<!doctype HTML>" + renderToStaticMarkup(<WithExport data={dataExport}>{result}</WithExport>));
}
})();