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