Created
May 22, 2018 08:37
-
-
Save netchampfaris/c6433977aa4262f83e28ffc2f4281c09 to your computer and use it in GitHub Desktop.
Converts indented code blocks to fenced code blocks in markdown files
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
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