Typecho 迁移 Hexo 指南
秉着将白嫖进行到底的宗旨,决定不再续费 VPS,博客从转移到 Github,使用 Hexo。
第一步:将 Typecho 文章转为 Hexo 文章格式
Hexo 的文章在网站\source\_posts\
目录下,Markdown 格式,内容包含了文章标题、发布时间、更新时间、分类、标签、固定连接、文章内容等信息,所以流程就是从 Typecho 数据库中查询出这些数据,拼接成 Hexo 文章的格式,写入.md
文件。
查询文章 SQL:
select title,slug,text,created,modified,category,tags from typecho_contents c,(select cid,group_concat(m.name) tags from typecho_metas m,typecho_relationships r where m.mid=r.mid and m.type='tag' group by cid ) t1,(select cid,m.name category from typecho_metas m,typecho_relationships r where m.mid=r.mid and m.type='category') t2 where t1.cid=t2.cid and c.cid=t1.cid
结果入下:
slug
为固定链接,需要将文件名设置为此;text
为文章内容,由于我Typecho文章设置的是 Markdown 格式,所以只需要将<!--markdown-->
这段内容替换为空即可;created
和modified
为发布时间和更新时间,需要转换为字符串,moment.unix(post.created).format("YYYY-MM-DD HH:mm:ss")
;category
和tags
可能有多个,"tags:\n- " + tags.replace(',',"\n- ") + "\n"
;
完整的脚本:https://gist.github.com/0x401/4c87b764b0c3c0a86d87e94ff9b36cdf
第二步:修改 Hexo 配置
Hexo 生成的网页文件在网站\public\
下,目录结构即为最终网站的结构,为了最大程度保证迁移前后链接不变,需要对 Hexo 配置做一些修改。
Hexo 的配置在_config.yml
文件中,hexo g
根据配置文件生成网站。
permalink
为固定链接配置,默认为:year/:month/:day/:title/
;tag_dir
标签目录,archive_dir
归档目录,category_dir
分类目录,pagination_dir
分页目录;- 分类和标签链接默认会直接采用文章中的值,可通过
category_map
和-tag_map
来配置文字和链接别名的对应关系; sitemap
配置站点地图,默认无;theme
网站主题,默认无。
第三步:主题配置
Hexo 主题在网站\themes\
下,通过配置theme
来确定使用哪个主题。
在主题文件夹中通过_config.yml
来配置一些模板中用到的变量,通过config.xxx
来获取。
第四步:发布网站到 Github
直接将 Public 目录中的文件提交到仓库即可。
标签:模板