Skip to content

Instantly share code, notes, and snippets.

@binarymax
Created August 7, 2016 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binarymax/c803693514ec838248452b81a753f617 to your computer and use it in GitHub Desktop.
Save binarymax/c803693514ec838248452b81a753f617 to your computer and use it in GitHub Desktop.
Use commonmark to generate html from a list of markdown files
#!/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