Last active
August 29, 2015 14:15
-
-
Save macoshita/9dc675f122ba478ceae6 to your computer and use it in GitHub Desktop.
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 toMarkdown = require('html2markdown'); | |
var fm = require('front-matter'); | |
var fs = require('fs'); | |
var glob = require('glob'); | |
var path = require('path'); | |
var mkdirp = require('mkdirp'); | |
glob.sync('./*.md').forEach(function(file) { | |
fs.readFile(file, 'utf8', function(err, data) { | |
var c = fm(data); | |
var name = path.basename(file); | |
if (name === 'index.md') { | |
return; | |
} | |
var date = name.substring(0, 10); | |
var datePath = 'tmp/' + date.replace(/-[0-9]+-[0-9]+$/g, ''); | |
mkdirp.sync(datePath); | |
name = c.attributes.title || name.substring(11, name.length - 3).replace(/-/g, ' '); | |
var body = c.body.replace('{% include JB/setup %}', '') | |
.trim() | |
.replace(/&#([0-9]+);/g, function(str, p1) { | |
return String.fromCharCode(parseInt(p1)); | |
}).replace(/<pre[^>]+>([\S\s]*?)<\/pre>/g, function(str, p1) { | |
return '<pre>' + p1.replace(/<br[^>]*>/g, '\n') + '</pre>'; | |
}).replace(/ /g, ' '); | |
if (body.indexOf('<br />') !== -1 || body.indexOf('<') === 0) { | |
try { | |
body = toMarkdown(body); | |
} catch(e) { | |
} | |
} | |
fs.writeFile(path.join(datePath, file.substring(7)), '---\ntitle: ' + name + '\ndate: ' + date + '\ntemplate: layout.jade\n---\n\n' + body, function(err) { | |
console.log(file); | |
if (err) console.log(err); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment