all repos — kushiyaki @ 4bb8cf16bf94ddbed895c506f82ba60ea9e2df9f

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

deprecated/markdownObj.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
import { unified } from "unified";
import { readFile } from "fs-extra";
import remarkStringify from "remark-stringify";
import remarkRehype from "remark-rehype";
import remarkParse from "remark-parse";
import remarkFrontmatter from "remark-frontmatter";
import rehypeStringify from "rehype-stringify";
import rehypeFormat from "rehype-format";
import rehypeRaw from "rehype-raw";
const yaml = require("js-yaml");

//NOTE This function takes a markdown, flattens the frontmatter into an object,
// then parses the body into a HAST (HTML) string and adds it into the object.
// The markdown is carved into simple components, which will later be slotted
// into JSX components
//

export async function markdownObj(filepath) {
  let output;
  const processor = await unified()
    .use(remarkParse)
    .use(remarkFrontmatter, "yaml")
    .use(() => (tree) => {
      output = yaml.load(tree.children[0].value);
      //TODO throw error on invalid frontmatter
    })
    .use(remarkRehype)
    .use(rehypeFormat)
    .use(rehypeStringify)
    //TODO throw error if frontmatter already contains main (or other reserved properties)
    .process(await readFile(filepath, "utf-8"));
  output.filename = filepath.split("/").pop(); //adds the input filename into the object
  output.main = processor.value;
  console.log(output);
  return output;
}

markdownObj("./input/cookie.md");