Last active
December 18, 2015 01:38
migrate blogs from cnblogs to markdown
This file contains 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 feedparser = require('feedparser'), | |
async = require('async'), | |
moment = require('moment'), | |
tomd = require('to-markdown').toMarkdown, | |
fsextra = require('fs-extra'); | |
var source = 'CNBlogs_BlogBackup.xml', | |
target = 'post'; | |
function tidyHtml(html) { | |
return html.replace(/<br \/><\/a>/g, '</a><br />'); | |
} | |
function joinYaml(head, mark) { | |
var mark = mark.replace(/ *\n\[YAML\] ([\w\W]+?)(\n|$)/, function(m, c1){ | |
var ar = c1.replace(/ /g, ' ').split(', '), key, value; | |
ar.forEach(function(item){ | |
key = item.split(': ')[0]; | |
value = item.slice(key.length + 2); | |
head[key.toLowerCase()] = value; | |
}); | |
return ''; | |
}); | |
var yaml = ''; | |
for (key in head) { | |
yaml += key + ': ' + head[key] + '\n' | |
} | |
return '---\n' + yaml + '---\n\n' + mark; | |
} | |
function tidyMark(mark) { | |
var re = /<div class="cnblogs_Highlighter">\n<pre class="brush:(\w+?);gutter:false;">([\w\W]+?)\n?<\/pre>\n<\/div>/g; | |
mark = mark.replace(/\r\n|\r/g, '\n') | |
.replace(re, function(m, c1, c2){ | |
c2 = c2.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&'); | |
return '```' + c1 + '\n' + c2 + '\n```' | |
}) | |
.replace(/(\d)\\./g, '$1.') | |
.replace(/\n\[/g, ' \n['); | |
return mark; | |
} | |
async.waterfall([ | |
function(next){ | |
feedparser.parseFile(source, next); | |
}, | |
function(meta, posts, next){ | |
async.forEach(posts, function(item, next){ | |
var link = (item.origlink ? item.origlink : item.link).split('/').reverse()[0]; | |
var date = moment(item.pubdate); | |
var name = target + '/' + date.format('YYYY-MM-DD') + '-' + link.split('.')[0] + '.md'; | |
var mark = tomd(tidyHtml(item.description)); | |
var head = { | |
title: item.title, | |
date: date.format('YYYY-MM-DD HH:mm:ss') | |
}; | |
mark = tidyMark(joinYaml(head, mark)); | |
fsextra.outputFile(name, mark, next); | |
}, function(){ | |
next(null, posts.length); | |
}); | |
} | |
], function(err, length){ | |
if (err) throw err; | |
console.log('%d posts migrated.', length); | |
}); |
This file contains 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": "migrator-cnblogs", | |
"version": "0.0.1", | |
"description": "migrate blogs from cnblogs to markdown", | |
"dependencies": { | |
"async": "*", | |
"feedparser": "*", | |
"moment": "*", | |
"to-markdown": "*", | |
"fs-extra": "*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice