Skip to content

Instantly share code, notes, and snippets.

@ddombrow
Created January 23, 2018 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddombrow/e8c199da54e60867986aa53f71f71258 to your computer and use it in GitHub Desktop.
Save ddombrow/e8c199da54e60867986aa53f71f71258 to your computer and use it in GitHub Desktop.
make frontmatter for md files
const { promisify } = require("util");
const fs = require("fs");
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
const readDirAsync = promisify(fs.readdir);
async function doIt() {
let fileList = await readDirAsync(__dirname);
fileList = fileList.filter(f => {
return f.endsWith(".md");
});
const modified = [];
for (f of fileList) {
let fileContent = await readFileAsync(__dirname + "/" + f);
if (!fileContent.toString().startsWith("---")) {
const pageName = f.toString().replace(".md", "");
const title = pageName.substr(0, 1).toUpperCase() + pageName.substr(1, pageName.length);
const frontMatter = `---
path: "/css-docs/${pageName}"
date: "20178-01-23"
title: "${title}"
---
`;
fileContent = frontMatter + fileContent;
await writeFileAsync(__dirname + "/fm-" + f, fileContent);
modified.push(f);
}
}
return modified;
}
doIt()
.then(modded => {
console.log("These files got frontmatter added: \n", modded);
})
.catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment