import { readFile, writeFile } from "fs-extra"; import { unified } from "unified"; import remarkParse from "remark-parse"; import remarkStringify from "remark-stringify"; import remarkFrontmatter from "remark-frontmatter"; import remarkRehype from "remark-rehype"; const yaml = require("js-yaml"); // //NOTE input: filepath. Output- frontmatter pairs in object // export async function extractFrontmatter(filepath) { let frontmatterObj; const processor = await unified() .use(remarkParse) .use(remarkFrontmatter, "yaml") .use(() => (tree) => { frontmatterObj = yaml.load(tree.children[0].value); //FIX something with vfiles //TODO throw error on invalid frontmatter frontmatterObj.filename = filepath.split("/").pop(); frontmatterObj.filepath = filepath; }) .use(remarkStringify) .process(await readFile(filepath, "utf-8")); return frontmatterObj; } // console.log(await extractFrontmatter("./input/content/index.md"));