import rehypeFormat from "rehype-format"; import rehypeRaw from "rehype-raw"; import rehypeStringify from "rehype-stringify"; import remarkFrontmatter from "remark-frontmatter"; import remarkParse from "remark-parse"; import remarkRehype from "remark-rehype"; import { unified } from "unified"; import { read } from "to-vfile"; import { copy, emptyDir, ensureDir, readFile, writeFile, readdir, } from "fs-extra"; import { argv0 } from "process"; import baseline from "./input/baseline.jsx"; let frontmatter; // builds the Markdown --> HTML processor const processor = unified() .use(remarkParse) .use(remarkFrontmatter, "yaml") // frontmatter .use(() => (tree) => { frontmatter = tree.children[0].value; if (tree.children[0].type == "yaml") { frontmatter = Object.fromEntries( frontmatter.split("\n").map((line) => line.split(": ")), ); console.log(frontmatter); } else { console.error(" ERROR: Frontmatter not found"); process.exit(); } }); .use(remarkRehype, { allowDangerousHtml: true }) .use(rehypeRaw) .use(rehypeFormat) // turn hast tree into string // consider: add this tree as child to JSX produced tree .use(rehypeStringify); // clears out the output directory and copies static files into it. await emptyDir("out", { recursive: true }); // await copy("css", "out/css"); // await copy("static", "out/static"); // Reads the content folder const input = await readdir("./input/", { recursive: true, withFileTypes: true, }); const jsxProcessor = unified() .use(rehypeFormat) // turn hast tree into string // consider: add this tree as child to JSX produced tree .use(rehypeStringify); // Iterates over content files for (let file of input) { if (file.isFile()) { let filepath = file.path + "/" + file.name; const input_file = await readFile(filepath, "utf8"); console.log(filepath); // Invokes Markdown --> HTML Processor console.log(" " + filepath + " parsing"); let parsed_input = await processor.run(await processor.parse(input_file)); // builds layout // const layoutMain = eval(frontmatter.main)({}); // parses JSX HACK const markdown = () => parsed_input; const output = await jsxProcessor.stringify( await jsxProcessor.run( baseline({ css: frontmatter.css, header: eval(frontmatter.header)?.(frontmatter), before: eval(frontmatter.before)?.(frontmatter), main: markdown(), after: eval(frontmatter.after)?.(frontmatter), footer: eval(frontmatter.footer)?.(frontmatter), }), ), ); // Writes output to a new HTML file await writeFile( file.path.replace("content", "out") + "/" + file.name.split(".")[0] + ".html", String(output), "utf8", ); console.log(" " + filepath + " written"); console.log(frontmatter); } } console.log("\n----- WRITING COMPLETE -----");