all repos — kushiyaki @ 5e18b49804cd755b996f3d459b677ac823d40cf4

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

createSiteIndex.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
import { glob } from "glob";
import { readFile } from "fs-extra";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkFrontmatter from "remark-frontmatter";
const yaml = require("js-yaml");

export async function extractFrontmatter(filepath) {
  let frontmatter;

  const processor = unified()
    .use(remarkParse)
    .use(remarkFrontmatter, "yaml")
    .use(() => (tree) => {
      frontmatter = yaml.load(tree.children[0].value, {
        schema: yaml.FAILSAFE_SCHEMA,
      });
      frontmatter.filename = filepath.split("/").pop().split(".").shift();
    });
  processor.run(processor.parse(await readFile(filepath, "utf-8")));

  return frontmatter;
}

export async function createSiteIndex() {
  let siteIndex = [];
  for (const file of await glob("input/**/*.md", { nodir: true })) {
    siteIndex.push(await extractFrontmatter(file));
  }
  return siteIndex;
}