all repos — kushiyaki @ 9295af41432fa98494bf86bf65860235711d86b5

🍢 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
 46
 47
 48
 49
 50
import { writeComponent } from "./writeComponent";
import { unified } from "unified";
import rehypeParse from "rehype-parse";
import rehypeStringify from "rehype-stringify";
import rehypeDocument from "rehype-document";
import { writePageMain } from "./writePageMain";
import rehypeFormat from "rehype-format";
import { extractFrontmatter } from "./extractFrontmatter";

//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);
  const recipe = Object.keys(frontmatter).filter((x) =>
    ["before", "header", "footer", "after"].includes(x),
  );
  let union = "";

  //HACK
  if (recipe.includes("header")) {
    union += await writeComponent(frontmatter.header, frontmatter, "header");
  }
  if (recipe.includes("before")) {
    union += await writeComponent(frontmatter.before, frontmatter);
  }
  union += await writePageMain(filepath);

  if (recipe.includes("after")) {
    union += await writeComponent(frontmatter.after, frontmatter);
  }
  if (recipe.includes("footer")) {
    union += await writeComponent(frontmatter.footer, frontmatter, "footer");
  }
  //HACK

  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;
}