extractFrontmatter.js (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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;
}
|