Last active
May 21, 2018 08:16
-
-
Save laobubu/a8efd66e4ec7076e7f26 to your computer and use it in GitHub Desktop.
Convert Typecho to Jekyll & DuoShuo
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
/** | |
* Typecho to Jekyll & DuoShuo | |
* | |
* @author laobubu.net | |
* | |
* Usage: | |
* 1. run `npm install dateformat` | |
* 2. export table `typecho_comments` and `typecho_contents` as two json files. you can do this with PHPMySQLAdmin | |
* 3. edit this file, especially line 15 - 20 | |
* 4. run `node t2jd.js` | |
*/ | |
var fs = require('fs'); | |
var df = require('dateformat'); | |
var style = { | |
getDate: (date) => (df(date, 'yyyy-mm-dd hh:MM:ss')), //DO NOT CHANGE! | |
getThreadKey: (post) => ("/archives/" + post.slug), | |
getPermalink: (post) => ("/archives/" + post.slug), | |
getPostURL: (post) => ("http://blog.laobubu.net" + style.getPermalink(post)), | |
} | |
var src_comments = JSONparse(fs.readFileSync('typecho_comments.json', 'utf-8')); | |
var src_contents = JSONparse(fs.readFileSync('typecho_contents.json', 'utf-8')); | |
/** phpMyAdmin non-standard JSON string will not work; use this to parse */ | |
function JSONparse(str) { | |
var tmp; | |
tmp = eval(str.replace(/^\/\/.*$/g, '').trim().replace(/\n/g, '\\n')); | |
return tmp; | |
} | |
var duoshuo = { threads: [], posts: [] }; | |
var comments = {}; | |
var posts = {}; | |
['_drafts', '_posts'].forEach((dir) => { | |
try { fs.mkdirSync(dir) } catch (error) { } | |
}) | |
src_contents.forEach((post) => { | |
post.thread_key = style.getThreadKey(post); | |
post.url = style.getPostURL(post); | |
post.created = new Date(post.created * 1000); | |
var fdata, fn, fstatus; | |
fstatus = "_posts" | |
if (/draft$/.test(post.type)) fstatus = "_drafts" | |
if (/^page/.test(post.type)) fstatus = "." | |
fdata = [ | |
"---", | |
"layout: " + (/^page/.test(post.type) ? "page" : "post"), | |
"title: " + JSON.stringify(post.title), | |
"date: " + style.getDate(post.created), | |
"categories: ", //TODO | |
"permalink: " + style.getPermalink(post), | |
"---", | |
"", | |
post.text.replace(/^<!--markdown-->/g, '') | |
].join('\n'); | |
fn = fstatus + "/" + df(post.created, "yyyy-mm-dd") + "-" + post.slug + ".md"; | |
fs.writeFile(fn, fdata); | |
posts[post.cid] = post; | |
duoshuo.threads.push({ | |
thread_key: post.thread_key, | |
title: post.title, | |
url: post.url | |
}) | |
}) | |
src_comments.forEach((comment) => { | |
var post = posts[comment.cid]; | |
var push = { | |
thread_key: post.thread_key, | |
post_key: "typecho_coid_" + comment.coid, | |
message: comment.text, | |
author_name: comment.author, | |
author_email: comment.mail, | |
author_url: comment.url, | |
ip: comment.ip, | |
agent: comment.agent, | |
created_at: style.getDate(new Date(comment.created * 1000)) | |
}; | |
if (comment.parent) push.parent_key = "typecho_coid_" + comment.parent; | |
duoshuo.posts.push(push) | |
}) | |
fs.writeFile("duoshuo.json", JSON.stringify(duoshuo)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment