Skip to content

Instantly share code, notes, and snippets.

@congpeijun
Last active November 6, 2019 07:17
Show Gist options
  • Select an option

  • Save congpeijun/bdf308eec07458d2c7f57f4922458559 to your computer and use it in GitHub Desktop.

Select an option

Save congpeijun/bdf308eec07458d2c7f57f4922458559 to your computer and use it in GitHub Desktop.
convert typecho to hexo
<?php
$db = new SQLite3('typecho.db');
$result = $db->query("SELECT a.cid, a.title, a.slug, a.text, a.created, a.status,
CASE c.type
WHEN 'tag' THEN group_concat(c.name)
END tags,
CASE c.type
WHEN 'category' THEN group_concat(c.name)
END categories
FROM typecho_contents AS a
LEFT JOIN typecho_relationships AS b ON b.cid=a.cid
LEFT JOIN typecho_metas AS c ON c.mid=b.mid
GROUP BY a.cid
ORDER BY a.cid DESC"
);
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
generate($row);
}
function generate($data)
{
$foler = __DIR__ . '/source/_drafts/';
$tpl = file_get_contents(__DIR__ . '/scaffolds/post.md');
$filename = $data['slug'] . '.md';
if ($data['status'] == 'publish') {
$foler = __DIR__ . '/source/_posts/';
}
$tpl = str_replace('{{ title }}', $data['title'], $tpl);
$tpl = str_replace('{{ date }}', date('Y-m-d H:i:s', $data['created']), $tpl);
$tpl = str_replace('{{ tags }}', $data['tags'] ? '[' . $data['tags'] . ']' : '', $tpl);
$tpl = str_replace('{{ categories }}', $data['categories'] ? '[' . $data['categories'] . ']' : '', $tpl);
$tpl .= $data['text'];
file_put_contents($foler . $filename, $tpl);
}
@congpeijun
Copy link
Author

不用。你只要找到 typecho.db这个文件,然后 修改对应的的路径就行
https://gist.github.com/congpeijun/bdf308eec07458d2c7f57f4922458559#file-convert-php-L3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment