Skip to content

Instantly share code, notes, and snippets.

@rapsli
Last active December 21, 2015 11:38
Show Gist options
  • Save rapsli/6300172 to your computer and use it in GitHub Desktop.
Save rapsli/6300172 to your computer and use it in GitHub Desktop.
This file contains some functionality that I used to move content from Jekyll to Wintersmith.
var fs = require('fs')
var _ = require('underscore')
var exclude = ['moveit.js', 'node_modules'];
function doIt(path) {
fs.readdir(path,function(err,files){
if(err) throw err;
files.forEach(function(file){
if (_.indexOf(exclude, file) == -1) {
var stats = fs.lstatSync(path+file);
if (stats.isDirectory()) {
doIt(path + file + '/');
}
else {
processFile(path+file, file);
}
}
});
});
}
function processFile(path, file) {
var date = file.substring(0, 10);
// rewriting some content
fs.readFile(path, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
data = data.replace("layout: post", "template: post.html")
var content = data.split("---")
content[1] = content[1] + "date: " + date + "\r\n";
data = content.join("---");
fs.writeFile(path, data, function (err) {
if (err) return console.log(err);
console.log("adding date: " + path)
});
});
// Some addition: This rewrites files from .html to .md and then removes the old file
var extension = file.split('.').pop();
if (extension == 'html') {
console.log("going to rename: " + file)
var newPath = path.replace(".html", ".md");
fs.readFile(path, 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
fs.writeFile(newPath, data, function (err) {
if (err) return console.log(err);
//console.log("success " + path)
fs.unlink(path, function (err) {
if (err) throw err;
});
});
});
}
}
doIt('./');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment