Skip to content

Instantly share code, notes, and snippets.

@mikechambers
Created December 16, 2013 18:53
Show Gist options
  • Select an option

  • Save mikechambers/7992288 to your computer and use it in GitHub Desktop.

Select an option

Save mikechambers/7992288 to your computer and use it in GitHub Desktop.
node.js script that generates a publish date for wordpress blog post files exported to jekyll using the wordpress-to-jekyll-exporter https://github.com/benbalter/wordpress-to-jekyll-exporter. The date is based on the file name (which includes the date). I created this because when I exported my data, the publish date was not exported.
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, continue:true */
/*global require, exports, os */
(function () {
"use strict";
var fs = require("fs");
var path = require("path");
var os = require("os");
//path that contains the posts to update. Note, this directory should ONLY contain
//post files, and nothing else.
var POSTS_DIR_PATH = "/Users/mesh/tmp/fix/_posts";
var FILE_EOL_CHAR = os.EOL;
var createPublishDate = function (fileName) {
//2012-02-22
var fileNameParts = fileName.split("-");
var year = fileNameParts[0];
var month = fileNameParts[1];
var date = fileNameParts[2];
//note, we generate a random time stamp for the publish date. It may be out of order
//with other posts on the same day. If this is an issue, you might be able to read
//create / change date of the file and order appropriately.
//note, if you have multiple files published on the same date, they will
//have same publish date / time. So we generate a random minute value, so they wont
//be the same. However, there is a chance we will generate the same minutes. If you
//have a lot of posts on the same day, you may want to make this more robust.
var minutes = Math.floor(Math.random() * 60);
minutes = String(minutes);
if (minutes.length === 1) {
minutes = "0" + minutes;
}
//2013-12-14 21:33:48 -0800
var out = year + "-" + month + "-" + date + " " + "12:" + minutes + ":01 -0800";
return out;
};
var main = function () {
if (!fs.existsSync(POSTS_DIR_PATH)) {
console.log("POST_DIR_PATH does not exist : " + POSTS_DIR_PATH);
return;
}
//note : we are reading syncronous / block
//very un-node like
var posts = fs.readdirSync(POSTS_DIR_PATH);
if (!posts || !posts.length) {
console.log("No posts found in : " + POSTS_DIR_PATH);
return;
}
var len = posts.length;
var i;
var postFileName, data, filePath;
var options = {encoding: "utf8"};
var tmp;
for (i = 0; i < len; i++) {
postFileName = posts[i];
if (postFileName === ".DS_Store") {
continue;
}
//create the path to the file
filePath = path.join(POSTS_DIR_PATH, postFileName);
//generate the publish date to insert
var publishDate = createPublishDate(postFileName);
//read the file
data = fs.readFileSync(filePath, options);
//split it into an array (each item is a line)
tmp = data.split(FILE_EOL_CHAR);
//insert the publish date
tmp.splice(3, 0, "date: " + publishDate);
//write file back out
fs.writeFileSync(filePath, tmp.join(FILE_EOL_CHAR), options);
}
};
main();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment