Skip to content

Instantly share code, notes, and snippets.

@me-shaon
Last active April 15, 2020 23:29
Show Gist options
  • Save me-shaon/fc395f55ec0ea447f54d1441158f2546 to your computer and use it in GitHub Desktop.
Save me-shaon/fc395f55ec0ea447f54d1441158f2546 to your computer and use it in GitHub Desktop.
Github markdown fixer script
const fs = require('fs');
const path = require('path');
const rootDir = process.argv[2] || __dirname; //Take the 'directory path' given in command line argument. Otherwise take the 'current directory'
//List of Regular Expressions need to apply
const changeRules = [{
//Header Elements fixers
'find': /^(#+)([^ #].+)$/,
'replaceWith': "$1 $2"
}, {
//URL Link elements fixers
'find': /(\[.+?\])\s+(\(.*?\))/,
'replaceWith': "$1$2"
}];
const fixItRalph = (dir) => {
const files = fs.readdirSync(dir);
files.forEach((file) => {
file = path.join(dir, file);
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) //recursively search in sub-directories
fixItRalph(file);
else if (stat && path.extname(file) === '.md') {
fs.readFile(file, 'utf8', (err, data) => {
if (err) console.log(err);
let result = data;
changeRules.forEach(elem => {
result = result.replace(new RegExp(elem.find, "g"), elem.replaceWith);
})
fs.writeFile(file, result, 'utf8', err => {
if (err) console.log(err);
});
});
}
});
}
fixItRalph(rootDir);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment