Skip to content

Instantly share code, notes, and snippets.

@adhrinae
Created January 16, 2018 15:04
Show Gist options
  • Save adhrinae/89dbb39273e6fec35c167dea3ac07217 to your computer and use it in GitHub Desktop.
Save adhrinae/89dbb39273e6fec35c167dea3ac07217 to your computer and use it in GitHub Desktop.
Change frontmatter of old blog posts
const fs = require('fs');
fs.readdir('./', (err, files) => {
if (err) {
throw new Error(err);
}
const oldPosts = files.filter(file => /\.md$/.test(file));
oldPosts.forEach(post => injectRedirectTo(post));
});
function injectRedirectTo(fileName) {
const originalPath = fileName.match(/\d{4}-\d{2}-\d{2}-(.*)\.md$/)[1];
const redirectTo = `https://emaren84.github.io/posts/${originalPath}`;
const replaceText = `redirect_to:\n - ${redirectTo}\ncategories:`;
fs.readFile(fileName, 'utf8', (err, data) => {
if (err) {
throw new Error(err);
}
const result = data.replace(/categories\:/, replaceText);
fs.writeFile(fileName, result, (err) => {
if (err) {
throw new Error(err);
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment