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