all repos — kushiyaki @ 6bcc5689a45b3115121066f167b15ca839a076a8

🍢 A tiny static site generator for grilling markdown files to perfection

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