使用 Hexo 写博客
原来一直使用 Sphinx 写东西, 2018 年想开始使用一个真正的博客系统来写一些博客,于是选用了 Hexo 。我主要使用 Python 进行开发,那么为什么不选用 Pelican 之类的以 Python 为基础的博客系统呢?因为不够好看,不够方便。下面是我在安装及使用 Hexo 过程中的一些笔记。
快速开始
安装 Hexo
Install Git
- Windows: Download & install git.
- Mac: Install it with Homebrew, MacPorts or installer.
- Linux (Ubuntu, Debian):
sudo apt-get install git-core
- Linux (Fedora, Red Hat, CentOS):
sudo yum install git-core
Install Node.js
The best way to install Node.js is with Node Version Manager.
Thankfully the creators of nvm provide a simple script that automatically installs nvm:
cURL:
1 | $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash |
Wget:
1 | $ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash |
Once nvm is installed, restart the terminal and run the following command to install Node.js:
1 | $ nvm install stable |
Alternatively, download and run the installer.
Install Hexo
Once all the requirements are installed, you can install Hexo with npm:
1 | $ npm install -g hexo-cli |
创建一个新的博客
1 | $ hexo init blog-hexo |
创建一个新的博客帖子
1 | $ hexo new "My New Post" |
运行服务器
1 | $ hexo server |
这样在浏览器中访问 http://localhost:4000/
,就可以看到博客了。
获得帮助
如何发布博客
生成静态文件
发布博客之前首先要生成静态文件
1 | $ hexo generate |
发布到 Git Pages
安装 hexo-deployer-git 插件:
1 | $ npm install hexo-deployer-git --save |
在站点配置文件中加入以下内容:
1 | deploy: |
使用 hexo deploy
命令来发布博客。
更多内容参见: 发布
Theme
NexT [Reloaded]
next theme 已经移到新的地址
通过以下命令安装:
1 | $ cd your-hexo-path |
在配置文件中设置 theme 的名称:
1 | theme: next |
把 theme 配置文件中的所有设置提制到站点配置文件的 theme_config 下:
例如:
1 | theme_config: |
更多内容参见 next theme 文档。
本地搜索
安装搜索插件
1 | npm install hexo-generator-search --save |
修改站点配置文件
1 | search: |
Categories
创建 categroies 文件:
1 | $ hexo new page categories |
以上命令会创建一个名为 your-blog-path/source/categories/index.md
的文件,
把文件内容修改为:
1 | --- |
Tags
创建 tags 文件:
1 | $ hexo new page "tags" |
以上命令会创建一个名为 your-blog-path/source/tags/index.md
的文件,
把文件内容修改为:
1 | --- |
修改站点配置文件:
1 | menu: |
写作
只显示摘要
要实现摘要,只需要在文章开头写好摘要后,另起一行键入 <!−− more −−>
即可,就像这样:
1 | 这是摘要 |
如果不想显示“Read More”而显示别的文字比如“阅读更多”,打开主题的配置文件,按如下设置:
1 | excerpt_link: Read More |
把源文件加入 git
1 | git init |
出错处理
hexo主题next访问报错,出现:
1 | {% extends ‘_layout.swig‘ %} {% import ‘_macro/post.swig‘ as post_template %} |
原因是hexo在5.0之后把swig给删除了需要自己手动安装
1 | npm i hexo-renderer-swig |