all repos — kushiyaki @ f18c0bbf8c19a3ddc1cc40e4d2e5f38c7b4c7216

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

extractFrontmatter.js (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
import { readFile } from "fs-extra";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkStringify from "remark-stringify";
import remarkFrontmatter from "remark-frontmatter";
const yaml = require("js-yaml");

export async function extractFrontmatter(filepath) {
  let frontmatterObj;
  await unified()
    .use(remarkParse)
    .use(remarkFrontmatter, "yaml")
    .use(() => (tree) => {
      frontmatterObj = yaml.load(tree.children[0].value);
      frontmatterObj.filename = filepath.split("/").pop().split(".").shift();
    })
    .use(remarkStringify)
    .process(await readFile(filepath, "utf-8"));
  return frontmatterObj;
}