Created
August 7, 2016 11:23
-
-
Save binarymax/c803693514ec838248452b81a753f617 to your computer and use it in GitHub Desktop.
Use commonmark to generate html from a list of markdown files
This file contains hidden or 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
#!/usr/bin/env node | |
var fs = require('fs'); | |
var cm = require('commonmark'); | |
var convert = function(markdown) { | |
var reader = new cm.Parser(); | |
var writer = new cm.HtmlRenderer(); | |
var parsed = reader.parse(markdown); | |
var result = writer.render(parsed); | |
return result; | |
}; | |
var read = function(filename) { | |
var markdown = fs.readFileSync(filename,'utf8'); | |
return convert(markdown); | |
}; | |
var write = function(filename,html) { | |
fs.writeFileSync(filename,html,'utf8'); | |
}; | |
var list = function(path) { | |
path = path + '/'; | |
var files = fs.readdirSync(path); | |
var remrk = /\.md$/i; | |
return files | |
.filter((f)=>remrk.test(f)) | |
.map((f)=>[path+f,path+'html/'+f.replace(remrk,'.html')]); | |
}; | |
list(process.cwd()).forEach((f)=>write(f[1],read(f[0]))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment