Created
October 16, 2017 05:57
-
-
Save kylehotchkiss/d3a3353eced688787ed6a3fcf9fdc9ac to your computer and use it in GitHub Desktop.
Wordpress Exports to Jekyll. Fill a folder /xml/ with Wordpress exports and generate a folder /output with jekyll ready post files.
This file contains hidden or 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 fs = require('fs'); | |
var _ = require('lodash'); | |
var moment = require('moment'); | |
var parseXML = require('xml2js').parseString; | |
var existing = [ /* list of your current _post/* filenames, make sure all end in .html */ ] | |
var blacklist = [ /* list of files to exclude from output */ ]; | |
function readFiles( dirname, onFileContent, onError ) { | |
fs.readdir(dirname, function( error, filenames ) { | |
filenames.forEach(function( filename ) { | |
fs.readFile( dirname + filename, 'utf-8', function( error, content ) { | |
if ( error ) { | |
onError( error ); | |
} else { | |
onFileContent( filename, content ); | |
} | |
}); | |
}); | |
}); | |
} | |
function generateFrontmatter( post ) { | |
var frontmatter = ''; | |
var category = _.get(post, 'category[0].$.nicename' ); | |
var excerpt = _.get(post, '["excerpt:encoded"][0]'); | |
var title = post.title[0].replace(/["]/g, '\\"'); | |
frontmatter += '---\n'; | |
frontmatter += 'layout: post\n'; | |
frontmatter += 'title: "' + title +'"\n'; | |
if ( excerpt ) { | |
var clean = excerpt.replace(/["]/g, '\\"'); | |
frontmatter += 'description: "' + clean + '"\n'; | |
} | |
if ( category && category !== 'news' && category !== 'blog' ) { | |
frontmatter += 'category: ' + category +'\n'; | |
} | |
frontmatter += '---\n\n' | |
return frontmatter; | |
} | |
readFiles('./xml/', function( file, content ) { | |
parseXML(content, function( error, result ) { | |
var posts = _.get(result, 'rss.channel[0].item'); | |
var organized = {}; | |
if ( posts && Array.isArray( posts ) ) { | |
posts.forEach(function( post ) { | |
var type = post['wp:post_type'][0]; | |
if ( type !== 'attachment' && type !== 'nav_menu_item' || | |
( type === 'page' && post['content:encoded'][0] ) | |
) { | |
var date = moment( post['wp:post_date'][0] || post.pubDate[0] ) | |
var unix = date.unix(); | |
var filename = date.format('YYYY-MM-DD'); | |
filename += '-' + post['wp:post_name'][0] + '.html'; | |
if ( existing.indexOf(filename) === -1 && blacklist.indexOf(filename) === -1 && post['wp:status'][0] === 'publish' ) { | |
organized[unix] = { | |
filename: filename, | |
title: post.title[0], | |
content: post['content:encoded'][0], | |
frontMatter: generateFrontmatter( post ) | |
} | |
} | |
} | |
; | |
}); | |
for ( var i in organized ) { | |
var post = organized[i]; | |
var finalContent = post.frontMatter + post.content; | |
fs.writeFileSync('output/' + post.filename, finalContent); | |
} | |
} | |
}); | |
}, console.error); |
This file contains hidden or 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
{ | |
"name": "post-archives", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"lodash": "^4.17.4", | |
"moment": "^2.19.1", | |
"xml2js": "^0.4.19" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment