import { unified } from "unified"; import { readFile } from "fs-extra"; import remarkStringify from "remark-stringify"; import remarkRehype from "remark-rehype"; import remarkParse from "remark-parse"; import remarkFrontmatter from "remark-frontmatter"; import rehypeStringify from "rehype-stringify"; import rehypeFormat from "rehype-format"; import rehypeRaw from "rehype-raw"; const yaml = require("js-yaml"); //NOTE This function takes a markdown, flattens the frontmatter into an object, // then parses the body into a HAST (HTML) string and adds it into the object. // The markdown is carved into simple components, which will later be slotted // into JSX components // export async function markdownObj(filepath) { let output; const processor = await unified() .use(remarkParse) .use(remarkFrontmatter, "yaml") .use(() => (tree) => { output = yaml.load(tree.children[0].value); //TODO throw error on invalid frontmatter }) .use(remarkRehype) .use(rehypeFormat) .use(rehypeStringify) //TODO throw error if frontmatter already contains main (or other reserved properties) .process(await readFile(filepath, "utf-8")); output.filename = filepath.split("/").pop(); //adds the input filename into the object output.main = processor.value; console.log(output); return output; } markdownObj("./input/cookie.md");