const { readdir, readFile, writeFile } = require("fs/promises");
const matter = require("gray-matter");
const { stringify } = require("yaml");
const directory = "<YOUR-DIRECTORY>";
async function updateFrontMatter(filename) {
const filepath = `${directory}/${filename}`;
const { data: frontMatter, content } = matter(await readFile(filepath));
// remove desc attribute
if (frontMatter.desc === "") {
delete frontMatter["desc"];
}
// parse created date attribute and convert it as timestamp
if (typeof frontMatter.created === "string") {
frontMatter.created = new Date(frontMatter.created).getTime();
}
const newContent = `---
${stringify(frontMatter)}---
${content}`;
await writeFile(filepath, newContent);
console.log(`- [x] ${filepath}`);
}
async function main() {
const filenames = await readdir(directory);
const markdownFilenames = filenames.filter((f) => f.endsWith(".md"));
await Promise.all(markdownFilenames.map(updateFrontMatter));
}
main().catch(console.error);