-
-
Save jasondavis/82d1e7f312c4f974141356793713c5a9 to your computer and use it in GitHub Desktop.
Export wordpress posts to bash echos in jekyll format
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
// first inject jQuery on the page, then run on /wp-admin/edit.php | |
// wait for it then run o.join('\n') | |
// execute the output on _posts folder | |
var o = []; | |
(function() { | |
let posts = [].slice.call(document.querySelectorAll('.post_name')).map(e => { | |
let container = e.parentNode; | |
let id = container.id.split('_')[1]; | |
let editUrl = `https://your.wordpress.com/wp-admin/post.php?post=${id}&action=edit`; | |
let post = { | |
id: id, | |
editUrl: editUrl, | |
title: container.querySelector('.post_title').innerHTML, | |
name: container.querySelector('.post_name').innerHTML, | |
tags: container.querySelector('.tags_input').innerHTML, | |
year: container.querySelector('.aa').innerHTML, | |
month: container.querySelector('.mm').innerHTML, | |
day: container.querySelector('.jj').innerHTML, | |
}; | |
post.tags = post.tags.split(/\s*,\s*/g); | |
return post; | |
}); | |
for (let post of posts) { | |
$.get(post.editUrl, r => { | |
let md = $(r).find('#content').val() | |
.replace(/\n/g, '\\n') | |
.replace(/\[\/?code.*?csharp.*?\]/g, '```csharp') | |
.replace(/\[\/?code.*?\]/g, '```') | |
.replace(/<\/?code>/g, '`') | |
.replace(/^ *$/g, '') | |
.replace(/<div><b>/g, '##') | |
.replace(/<h2>/g, '##').replace(/<\/h2>/g, '') | |
.replace(/<h3>/g, '###').replace(/<\/h3>/g, '') | |
.replace(/<h4>/g, '####').replace(/<\/h4>/g, '') | |
.replace(/<h5>/g, '#####').replace(/<\/h5>/g, '') | |
.replace(/<\/b><\/div>/g, '') | |
.replace(/<div><\/div>/g, '\\n') | |
.replace(/<\/?ul>/g, '') | |
.replace(/<li>/g, ' - ') | |
.replace(/<\/li>/g, '') | |
.replace(/<\/?b>/g, '**') | |
.replace(/<\/?strong>/g, '**') | |
.replace(/<\/?i>/g, '*') | |
.replace(/<\/?div>/g, '') | |
.replace(/<\/?p.*?>/g, '') | |
.replace(/<img.*?src="(.*?)".*?>/g, (m, m1) => ``) | |
.replace(/<a.*?href="(.*?)".*?>(.*?)<\/a>/g, (m, m1, m2) => `[${m2}](${m1})`) | |
.replace(/'/g, '') | |
; | |
let tags = '\\n - ' + post.tags.join('\\n - '); | |
o.push(`echo -e '---\\nlayout: post\\ntitle: "${post.title}"\\ndate: ${post.year}-${post.month}-${post.day} 12:00:00 -0300\\ncategories: general\\ntags:${tags}\\n---\\n${md}' > ${post.year}-${post.month}-${post.day}-${post.name}.md`); | |
}); | |
} | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment