# Parse all Pages & Posts
node ./index path/to/typecho.sql
# Parse Pages & Posts of the User with ID 3
node ./index path/to/typecho.sql 3
Last active
January 14, 2019 13:53
-
-
Save TechQuery/dcc4c5a510111557b7296590b44c607d to your computer and use it in GitHub Desktop.
TypEcho to Hexo
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
#! /usr/bin/env node | |
const { readFileSync, existsSync, mkdirSync, writeFileSync } = require('fs'), | |
{ join, basename } = require('path'); | |
/** | |
* @param {Number} UNIX_TS | |
* | |
* @return {String} Example: `2019-01-14 21:21:21` | |
*/ | |
function formatDate(UNIX_TS) { | |
return (new Date(UNIX_TS * 1000)).toISOString() | |
.split( /[TZ.]/ ).slice(0, 2).join(' '); | |
} | |
/** | |
* @param {String} line | |
* | |
* @return {Object} | |
*/ | |
function parseRecord(line) { | |
line = line.split(', '); | |
const content = line.splice(5, Infinity).join(', '); | |
line = line.concat( content ).map(text => text.replace(/^'?(.+?)'?$/, '$1')); | |
return { | |
name: line[1].replace(/[\/:*?"<>|]/g, '_'), | |
title: line[1], | |
createdAt: formatDate( line[3] ), | |
updatedAt: formatDate( line[4] ), | |
content: line[5] | |
.replace(/^<!--markdown-->/, '') | |
.replace(/\\r\\n/g, '\n') | |
.replace(/\\(['"\\])/g, '$1') | |
}; | |
} | |
const [source, UID] = process.argv.slice(2); | |
console.time('Convert'); | |
const SQL = readFileSync( source ) + '', | |
blog = [ ], | |
folder = source.replace(/\.sql$/i, '') + (UID ? ('-' + UID) : ''); | |
if (! existsSync( folder )) mkdirSync( folder ); | |
SQL.replace(/^(.+), \d+, (\d+), NULL, '(\w+)'/mg, (_, data, author, type) => { | |
if ((UID != null) && (UID != author)) return; | |
data = Object.assign(parseRecord( data ), { author, type }); | |
blog.push( data ); | |
writeFileSync( | |
`${folder}/${blog.length}-${data.name}.md`, | |
`--- | |
title: ${data.title} | |
date: ${data.createdAt} | |
--- | |
${data.content}` | |
); | |
console.count('Article'); | |
console.log(`${data.createdAt.split(' ')[0]} ${data.title}`); | |
}); | |
writeFileSync(`${folder}.json`, JSON.stringify(blog, null, 4)); | |
console.log('-----------'); | |
console.timeEnd('Convert'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment