Skip to content

Instantly share code, notes, and snippets.

@netchampfaris
Created May 22, 2018 08:37
Show Gist options
  • Save netchampfaris/c6433977aa4262f83e28ffc2f4281c09 to your computer and use it in GitHub Desktop.
Save netchampfaris/c6433977aa4262f83e28ffc2f4281c09 to your computer and use it in GitHub Desktop.
Converts indented code blocks to fenced code blocks in markdown files
var fs = require("fs"),
path = require("path");
walk('./', function(filepath, stats) {
if (filepath.endsWith('.md')) {
const filecontent = fs.readFileSync(filepath, { encoding: 'utf-8'});
const converted = convert(filecontent);
fs.writeFileSync(filepath, converted);
console.log(path.basename(filepath), 'done');
}
});
function convert(md) {
return md.replace(/((?:^\t[^\n]*\n)+)/gm, function(match, p1, offset, string) {
return "\n```\n" + match + "\n```\n";
});
}
function walk(dir, callback) {
fs.readdir(dir, function(err, files) {
if (err) throw err;
files.forEach(function(file) {
var filepath = path.join(dir, file);
fs.stat(filepath, function(err,stats) {
if (stats.isDirectory()) {
walk(filepath, callback);
} else if (stats.isFile()) {
callback(filepath, stats);
}
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment