deprecated/write_website.js (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
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 -----");
|