Skip to content

Instantly share code, notes, and snippets.

@laobubu
Created January 4, 2016 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laobubu/d48aeb6d35913ccf1fce to your computer and use it in GitHub Desktop.
Save laobubu/d48aeb6d35913ccf1fce to your computer and use it in GitHub Desktop.
Convert WordPress exported XML to Jekyll Posts
var parseString = require('xml2js').parseString;
var fs = require("fs");
var path = require("path");
var targetPath = "/home/laobubu/jekyll/_posts";
var wpXMLFile = "wordpress.xml";
try {
fs.mkdirSync(targetPath);
} catch (er){}
function convertContent(str){
var rtn = str;
rtn = rtn.replace(/class="align(\w+)"/g, 'align="$1"');
//rtn = rtn.replace(/\n\n([^\n]+)\n\n/gm, '<p>$1</p>');
rtn = rtn.replace(/(\r\n|\n)\1/gm, '<br><br>');
return rtn;
}
function extract1(arr, key) {
var rtn = [];
for (var i = arr.length; i--; ) {
rtn[i] = arr[i][key]
}
return rtn;
}
function safeStr(str) {
var q = str.toString();
var s = q.replace(/(["])/g,"\\$1");
return '"' + s + '"';
}
function writeFile(o) {
if (o.layout != 'post')
return;
var fn = o.time.getFullYear() + '-' + (o.time.getMonth() + 1) + '-' + o.time.getDate() + '-' + o.id + ".html";
var data = [
'---',
'layout: ' + o.layout,
'title: ' + safeStr(o.title),
'date: ' + (o.time.getFullYear() + '-' + (o.time.getMonth() + 1) + '-' + o.time.getDate() + ' ' +
o.time.getHours() + ':' + o.time.getMinutes() + ':' + o.time.getSeconds()),
'permalink: /' + o.id,
'categories: ' + JSON.stringify(o.categories),
'tags: ' + JSON.stringify(o.tags),
'published: ' + o.published,
'---',
convertContent(o.content)
].join('\n');
fn = path.join(targetPath, fn);
fs.writeFile(fn, data);
console.log('Proceed: ' + o.title);
}
fs.readFile(wpXMLFile,"utf-8",function(err, data) {
parseString(data, function (err, result) {
var items = result.rss.channel[0].item;
for (var i=0;i<items.length;i++) {
var type = items[i]['wp:post_type'];
var title = items[i]['title'];
var author = items[i]['dc:creator'][0];
var time = new Date(items[i]['pubDate']);
var id = items[i]['wp:post_id']; //or wp:post_name if you want slug
var content = items[i]['content:encoded'][0];
var categories = [];
var tags = [];
var published = items[i]['wp:status'] == 'publish';
for (var j in items[i]['category']) {
var itype = items[i]['category'][j].$.domain;
var idata = items[i]['category'][j]._;
if (itype == 'category') categories.push(idata);
if (itype == 'post_tag') tags.push(idata);
}
writeFile({
layout: type,
title: title,
author: author,
time: time,
id: id,
content: content,
categories: categories,
tags: tags,
published: published
});
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment