Last active
June 25, 2016 19:23
-
-
Save ntotten/ebba3e360316247224380454165440e4 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 lsr = require('lsr'); | |
var path = require('path'); | |
var fs = require('fs'); | |
var matter = require('gray-matter'); | |
var moment = require('moment'); | |
var files = []; | |
function loadPath(loadPath) { | |
var resolvedPath = path.resolve(__dirname, loadPath); | |
lsr | |
.sync(resolvedPath) | |
.forEach(function(fileStat) { | |
var filename = fileStat.path; | |
if (!/\.html$/.test(filename)) return; | |
var filepath = path.resolve(resolvedPath, filename); | |
var text = fs.readFileSync(filepath, 'utf8'); | |
files.push({ | |
filename: path.join('/', loadPath, filename), | |
text: text | |
}); | |
}); | |
} | |
[ | |
'./2007', | |
'./2008', | |
'./2009', | |
'./2010', | |
'./2011', | |
'./2012', | |
'./2013' | |
].map(loadPath); | |
files.forEach(function(file) { | |
var url = file.text.match(/url\=(.*)\"/)[1] | |
url = url.substring(1, url.length - 1).replace(new RegExp('\/', 'g'), '-'); | |
var filepath = path.resolve(__dirname, '_posts', url + '.md'); | |
var text = fs.readFileSync(filepath, 'utf8'); | |
var doc = matter(text); | |
doc.data.redirect_from = file.filename.replace('index.html', ''); | |
doc.data.date = moment(doc.data.date).local().format('YYYY-MM-DD'); | |
var md = matter.stringify(doc.content, doc.data); | |
fs.writeFileSync(filepath, md, 'utf8'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment