最近一个比较好的博客系统Hexo,它的各种好处我就不一一介绍了.我主要是想说一下从octopress迁移到hexo的心得体会.

Hexo安装

其实网上的资料很多,而且是中文的.因为hexo是一个台湾学生开发的.这里就讲一个大概吧.

安装git

我的是ubuntu14.04,git是默认已经安装了的.如果是windows系统,那就不要忘记安装它哦,这里不做过多说明,自行google吧.

安装nodejs

nodejs Linux apt安装方式

1
2
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

之后使用如下命令,查看是否安装成功:

1
nodejs -v

安装nodejs包管理器npm

1
2
3
git clone http://github.com/isaacs/npm.git
cd npm
sudo make install

安装hexo

1
npm install -g hexo-cli

博客系统

初始化

1
2
3
hexo init folder
cd folder
npm install

folder是你想把你的博客系统放在哪个文件夹的名字 推荐直接看这个网站:hexo的初始化

测试hexo

1
2
hexo generate #生成静态文件。
hexo server #生成本地服务器,端口默认4000启动,

打开浏览器,在地址栏输入:http://localhost:4000/.有网页出来就成功了.

安装主题

我使用的是Next主题,这里直接推荐Next官网文档

迁移

文章迁移

文章迁移起来还是很方便,把以前 Octopress 的 source/_post 目录下的文章,拷贝到 Hexo 的同名目录下即可,但需要删除文章中生成文章目录(TOC)的部分,因为hexo默认自动生成

第三方服务迁移

我使用的Next主题,我直接安装Next官网文档重新设置了一下.

github 部署

前期创建github账号,配置SSH,以及创建项目名为github用户名.github.io的项目,这里推荐Github Pages与Hexo教程. 我主要将一下hexo根目录下的_config.yml文件的配置

1
2
3
4
5
deploy:
type: github
repository: git@github.com:你的帐号/你的帐号.github.com.git
例如我的:repository: git@github.com:tveek/tveek.github.com.git
branch: master

github发布

第一次可能部署

1
2
3
4
hexo clean
hexo generate
npm install hexo-deployer-git --save(一个插件)
hexo deploy

之后

1
2
3
hexo clean
hexo generate
hexo deploy

这样就可以把你的博客部署到你的github上了.

mathjax问题

这是hexo的老大难问题,虽然hexo 支持mathjax,但是不完美,有各种特殊字符转义的问题.下面介绍一个方法,几乎完美解决该问题.

安装pandoc

ubuntu下,直接在软件中心下载安装

安装hexo-renderer-pandoc

在hexo根目录

1
npm install hexo-renderer-pandoc --save

配置mathjax

在theme_folder/layout/_partial/创建文件mathjax.swig文件,并写入如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!-- mathjax config similar to math.stackexchange -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ['$','$'], ["\\(","\\)"] ],
processEscapes: true
}
});
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
}
});
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Queue(function() {
var all = MathJax.Hub.getAllJax(), i;
for(i=0; i < all.length; i += 1) {
all[i].SourceElement().parentNode.className += ' has-jax';
}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>

注意,我的next是5.0.1,其他版本文件的后缀(.swig)可能不一样. 还有是,在latex公式中连续出现两个{,hexo也无法生成,解决办法是在两个{之间输入空格,这应该是hexo的bug.