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 double from "./src/double"; import { argv0 } from "process"; import { frontmatterJson } from "./frontmatterJson"; import poetryIndex from "./src/poetryIndex.jsx"; import baseline from "./src/baseline.jsx"; import booknav from "./src/booknav.jsx"; import gallery from "./src/gallery.jsx"; import picture from "./src/picture.jsx"; import h2 from "./src/h2.jsx"; import home from "./src/home.jsx"; import recipeIndex from "./src/recipeIndex.jsx"; import sharp from "sharp"; 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(": ")), ); } else { console.error(" ERROR: Frontmatter not found"); process.exit(); } }) .use(double) .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("./content/", { recursive: true, withFileTypes: true, }); // Iterates over the content folder's subdirectories and ensures they are made for (let folder of input) { if (folder.isDirectory()) { let outpath = folder.path.replace("content", "out") + "/" + folder.name; await ensureDir(outpath); console.log("Created: " + outpath); } } 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 -----");