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"));