feat: combine writePageMain and joinComponent
trickyni trickyniv56@gmail.com
Fri, 08 May 2026 23:45:58 +0300
4 files changed,
70 insertions(+),
71 deletions(-)
D
joinComponents.js
@@ -1,45 +0,0 @@
-import rehypeDocument from "rehype-document"; -import rehypeFormat from "rehype-format"; -import rehypeParse from "rehype-parse"; -import rehypeStringify from "rehype-stringify"; -import { extractFrontmatter } from "./extractFrontmatter"; -import { unified } from "unified"; -import { writeComponent } from "./writeComponent"; -import { writePageMain } from "./writePageMain"; - -//NOTE right now we're strinfigying, connecting the strings, then parsing for -//more rehype processing. I'm sure there's a more correct way to do this, but -//that can happen later - -export async function joinComponents(filepath) { - const frontmatter = await extractFrontmatter(filepath); - let recipe = ["before", "header", "main", "footer", "after"]; - for (let i of [0, 1, 3, 4]) { - if (!Object.keys(frontmatter).includes(recipe[i])) { - recipe[i] = null; - } - } - console.log(recipe); - let union = ""; - - for (let i of recipe.filter((x) => !!x)) { - union += - i === "main" - ? await writePageMain(filepath) - : await writeComponent(frontmatter[i], frontmatter); - } - - const processor = unified() - .use(rehypeParse, { fragment: true }) - .use(rehypeDocument, { - css: frontmatter.css ?? "style.css", - title: frontmatter?.title ?? frontmatter?.filename ?? "title", - link: [{ href: "favicon.png", rel: "icon", type: "image/png" }], - }) - .use(rehypeFormat) - .use(rehypeStringify); - - const output = await processor.process(union); - return output.value; -} -console.log(await joinComponents("./input/content/cookie.md"));
M
looper.js
→
looper.js
@@ -1,15 +1,15 @@
-import { emptyDir, ensureDir, ensureFile, writeFile } from "fs-extra"; -import { joinComponents } from "./joinComponents"; +import { emptyDir, writeFile } from "fs-extra"; import cpy from "cpy"; import { glob } from "glob"; +import { writePage } from "./writePage"; //mise en place -await emptyDir("out", { recursive: true }); // clears the output folder +emptyDir("out", { recursive: true }); // clears the output folder await cpy(["input/**", "!input/**/*{.md,.jsx}"], "out", { flat: true }); //copies all the assets into out/ for (const file of await glob("input/**/*.md", { nodir: true })) { await writeFile( file.replace(/input[\/\w]*\//, "out/").replace(".md", ".html"), - await joinComponents(file), + await writePage(file), ); }
A
writePage.js
@@ -0,0 +1,66 @@
+const yaml = require("js-yaml"); +import double from "./addons/double.js"; //NOTE delete from release +import rehypeDocument from "rehype-document"; +import rehypeFormat from "rehype-format"; +import rehypeParse from "rehype-parse"; +import rehypeRaw from "rehype-raw"; +import rehypeStringify from "rehype-stringify"; +import rehypeWrap from "rehype-wrap"; +import remarkFrontmatter from "remark-frontmatter"; +import remarkParse from "remark-parse"; +import remarkRehype from "remark-rehype"; +import { readFile } from "fs-extra"; +import { unified } from "unified"; +import { writeComponent } from "./writeComponent.js"; + +export async function writePage(filepath) { + let page; + const processor = await unified() + .use(remarkParse) + .use(remarkFrontmatter, "yaml") + .use(() => (tree) => { + page = yaml.load(tree.children[0].value, { + schema: yaml.FAILSAFE_SCHEMA, + }); + page.filename = filepath.split("/").pop().split(".").shift(); + }) + .use(double) + .use(remarkRehype, { allowDangerousHtml: true }) + .use(rehypeRaw) + .use(rehypeWrap, { wrapper: "main" }) + .use(rehypeFormat) + .use(rehypeStringify) + .process(await readFile(filepath, "utf-8")); + page.main = processor.value; + + // console.log(page); + + let recipe = ["before", "header", "main", "footer", "after"]; + for (let i of [0, 1, 3, 4]) { + if (!Object.keys(page).includes(recipe[i])) { + recipe[i] = null; + } + } + let union = ""; + + for (let i of recipe.filter((x) => !!x)) { + if (i === "main") { + union += page.main; + } else { + union += await writeComponent(page[i], page); + } + } + + const processor2 = unified() + .use(rehypeParse, { fragment: true }) + .use(rehypeDocument, { + css: page.css ?? "style.css", + title: page?.title ?? page?.filename ?? "title", + link: [{ href: "favicon.png", rel: "icon", type: "image/png" }], + }) + .use(rehypeFormat) + .use(rehypeStringify); + + const output = await processor2.process(union); + return output.value; +}
D
writePageMain.js
@@ -1,22 +0,0 @@
-import { readFile } from "fs-extra"; -import { unified } from "unified"; -import remarkParse from "remark-parse"; -import remarkRehype from "remark-rehype"; -import rehypeStringify from "rehype-stringify"; -import remarkFrontmatter from "remark-frontmatter"; -import rehypeFormat from "rehype-format"; -import rehypeRaw from "rehype-raw"; -import rehypeWrap from "rehype-wrap"; - -export async function writePageMain(filepath) { - const processor = await unified() - .use(remarkParse) - .use(remarkFrontmatter) - .use(remarkRehype, { allowDangerousHtml: true }) - .use(rehypeRaw) - .use(rehypeWrap, { wrapper: "main" }) - .use(rehypeFormat) - .use(rehypeStringify) - .process(await readFile(filepath, "utf-8")); - return processor.value; -}