SunDoge's Blog

What TripleZ don't know

0%

使用 Hexo 搭建个人博客

安装 Node.js 和 Hexo

参照 Hexo 官网教程 https://hexo.io/docs/#Install-Node-js

1
2
3
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
nvm install stable
npm install -g hexo-cli

创建你的博客

参照 https://hexo.io/docs/setup

1
hexo init sundoge.github.io

我使用的 hexo-cli 版本为 2.0.0 ,会自动拉取默认主题 landscape 。后面将引入 hexo-theme-next

使用 Git 管理你的博客

Hexo 的设计有个矛盾的地方,theme 有自己的 config 文件,而且不会 merge 到顶层的 config,导致你很难用 git 管理你的 theme。如果你按照 theme-next 的官方文档 install,就相当于在 git 仓库里面创建另 git 仓库,git 是没法管理到仓库里面的仓库的。但是如果单纯使用 git submodule,修改 theme 里面的_config.yml 也是没法用 git 管理的。所以一个折中的方案是,先 fork 一份你要的主题,比如 SunDoge/hexo-theme-next ,然后 add submodule

1
git submodule add git@github.com:SunDoge/hexo-theme-next.git themes/next

这样 theme 和博客都能被管理到。如果需要更新 theme,只需要运行

https://stackoverflow.com/questions/5828324/update-git-submodule-to-latest-commit-on-origin

1
git submodule foreach git pull origin master

这个命令会更新所有的 submodule。

使用 theme-next

修改顶层_config.yml

1
2
3
4
5
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
# theme: landscape
theme: next

修改 theme 里面的_config.yml

themes/next/_config.yml

1
2
3
4
5
# Schemes
# scheme: Muse
scheme: Mist
#scheme: Pisces
#scheme: Gemini

开启 math 支持

https://github.com/theme-next/hexo-theme-next/blob/master/docs/MATH.md

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Math Formulas Render Support
math:
enable: true

# Default (true) will load mathjax / katex script on demand.
# That is it only render those page which has `mathjax: true` in Front-matter.
# If you set it to false, it will load mathjax / katex srcipt EVERY PAGE.
per_page: true

# hexo-renderer-pandoc (or hexo-renderer-kramed) required for full MathJax support.
mathjax:
enable: true
# See: https://mhchem.github.io/MathJax-mhchem/
mhchem: false

# hexo-renderer-markdown-it-plus (or hexo-renderer-markdown-it with markdown-it-katex plugin) required for full Katex support.
katex:
enable: false
# See: https://github.com/KaTeX/KaTeX/tree/master/contrib/copy-tex
copy_tex: false

我暂时使用 mathjax,如果实在太慢,后面再换成 katex。这里还需要替换默认的 render。

1
2
npm un hexo-renderer-marked --save
npm i hexo-renderer-pandoc --save # or hexo-renderer-kramed

这里不用 kramed 的原因是它最后一次更新在 2017 年。还需要安装 pandoc

https://github.com/jgm/pandoc/blob/master/INSTALL.md

安装 pandoc 这点让我很不满意,使用 Hexo 已经要安装很多东西了(node_modules),为了 mathjax 还要整个 pandoc。

部署

https://hexo.io/docs/deployment.html#Git

1
npm install hexo-deployer-git --save
1
2
3
4
5
6
# Deployment
## Docs: https://hexo.io/docs/deployment.html
deploy:
- type: git
repo: git@github.com:SunDoge/sundoge.github.io.git
branch: published

然后在 github 上新建一个 branch 名为 published

https://help.github.com/en/articles/creating-and-deleting-branches-within-your-repository

并把它设为 default branch。然后执行

1
hexo clean && hexo deploy

编译好的文件会被推送到 published 分支。

为什么使用 Hexo

Hugo 虽然性能好,但是大部分主题年久失修,而且质量都不高,从 jekyll 和 hexo 移植的主题也大多有问题,缺少原有的部分功能。最后没办法,还是要用前端写的工具。

为了方便以后的写作,我计划使用 Rust 开发一个 hexo-cli 的子集,只支持 theme-next 的正确编译🕊🕊🕊​。