Created
January 16, 2018 15:04
-
-
Save adhrinae/89dbb39273e6fec35c167dea3ac07217 to your computer and use it in GitHub Desktop.
Change frontmatter of old blog posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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