import rehypeStringify from "rehype-stringify"; import remarkFrontmatter from "remark-frontmatter"; import remarkParse from "remark-parse"; import remarkRehype from "remark-rehype"; import { unified } from "unified"; import { readdir, readFile, writeFile } from "fs-extra"; export async function frontmatterJson(path) { let output = []; // NOTE Reads the content folder const inputFolder = await readdir(path, { recursive: true, withFileTypes: true, }); // NOTE Iterates over content files for (let file of inputFolder) { if (file.isFile()) { let filepath = file.path + "/" + file.name; console.log(filepath); output.push(await parseFrontmatter(filepath)); } } output .sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()) .reverse(); return output; } // NOTE builds the Markdown --> HTML processor async function parseFrontmatter(filepath) { let meow; let frontmatter; const processor = unified() .use(remarkParse) .use(remarkFrontmatter, "yaml") .use(() => (tree) => { frontmatter = tree.children[0].value; if (tree.children[0].type == "yaml") { frontmatter = Object.fromEntries( frontmatter.split("\n").map((line) => line.split(": ")), ); frontmatter.permalink ??= filepath.split("/").pop().replace(".md", ""); meow = frontmatter; } else { console.error(" ERROR: Frontmatter not found"); process.exit(); } }); processor.run(processor.parse(await readFile(filepath, "utf8"))); return meow; }