deprecated/frontmatterJson.js (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
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;
}
|