Skip to content

Instantly share code, notes, and snippets.

@aronwoost
Created December 12, 2013 10:41
Show Gist options
  • Save aronwoost/7926103 to your computer and use it in GitHub Desktop.
Save aronwoost/7926103 to your computer and use it in GitHub Desktop.
Octopress. Extract date from filename and add to post header.
var fs = require("fs");
var async = require("async");
var folder = "source/_posts";
fs.readdir(folder, function(err, result) {
async.each(result, function(file, callback){
var date = file.split("-").slice(0, 3).join("-"),
dateStr = "date: "+date+" 00:00:00 +0100";
fs.readFile(folder + "/" + file, "UTF-8", function read(err, content) {
if(err) {
return console.log(err);
}
var newContent = content.split("layout: post").join("layout: post\n" + dateStr);
fs.writeFile(folder + "/" + file, newContent, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
callback();
});
});
}, function(err){
if(err) {
return console.log(err);
}
console.log("done");
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment