Github最近总是邮件提醒我有些插件过时了,存在安全问题。随着文章数量增多,Hexo的速度越来越慢。因此决定切换到Hugo
安装
安装 Git 和 Go
Hugo是用Golang编写的,使用Hugo前需要安装Git 和 Go 语言开发环境。
安装Hugo
参考官方文档有很多安装方式。对于Windows用户,可直接下载二进制文件,将放置路径添加至环境变量PATH即可。
生成博客
1
2
3
4
|
hugo version # 查看版本
hugo new site myblog # 使用Hugo新建myblog项目,名称自拟
cd myblog # 进入myblog目录
hugo server # 启动本地调试服务
|
安装主题
Hugo没有自带的主题,但其官网的主题库也非常丰富,应该能满足大部分人的需要。安装方式也与Hexo类似,在themes目录中git clone需要的主题即可。目前使用的是even, 推荐的还有LeaveIt,maupassant
配置文件
默认的配置文件为站点目录中的config.toml,该文件可直接从主题的exampleSite/config.toml直接替换,再进一步自定义。
文章迁移
之前Hexo默认的URL为如下形式
1
2
3
|
/2017/01/02/xxxxx
/2017/03/04/yyyyy
/2017/05/06/zzzzz
|
在Hugo的配置文件中,需要做如下设置才能达到相同结构的URL
1
2
|
[permalinks]
posts = "/:year/:month/:day/:filename"
|
Front-matter
Markdown文档开头起说明作用的Front-matter字段应书写规范,Hugo的要求似乎比Hexo更严格,需要检查一下哪些文章没有写好。
- 在
tags, categories等字段设置了多个值,则应该以tags: [mongodb, test]的方式记录。
- Front-matter字段首尾的
---分割符不能缺少。
其他项目
1
2
3
4
|
## 保持分类的原始名字(false会做转小写处理)
preserveTaxonomyNames = true
## 是否禁止URL Path转小写
disablePathToLower = true
|
调试
1
2
|
hugo server
hugo server -D ##强制渲染非草稿的文章
|
Github项目迁移
之前,为了Hexo项目的多地部署,我在sr-c.github.io新开了一个hexo分支用于同步Hexo站点的配置目录。每次更新文章,先将站点目录git push到hexo分支,再使用hexo g -d调用git将public目录的内容提交至master分支。
顺应这个思路,我又新建了一个hugo分支用于同步站点目录,再将每次更新生成的public目录推送至master分支。
向GitHub添加SSH key
参考之前的设置方式,对于已有的SSH key,可直接添加至GitHub.
1
2
3
4
5
|
# Add SSH key to clipboard
clip < ~/.ssh/id_rsa.pub
# Paste the key to GitHub Accout Settings page.
# Test the Key
ssh -T git@github.com
|
提交hugo分支
在新建立的Hugo站点目录中
1
2
3
4
5
6
7
|
cd myblog # 位于新建立的站点目录中
git init # git初始化
git remote add origin https://<USERNAME>/<USERNAME>.github.io.git # 添加仓库地址
git checkout -b hugo # 新建分支hexo并切换到新建的分支hexo
git add . # 添加所有本地文件到git
git commit -m "version_log" # git提交
git push origin hugo # 文件推送到hugo分支
|
提交master分支
Hugo官方文档推荐使用子项目的方式管理并提交public目录至master分支。
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
27
28
29
30
31
|
rm -rf public
#git submodule add -b master https://github.com/<USERNAME>/<USERNAME>.github.io.git public
## deploy.sh
#!/bin/sh
# If a command fails then the deploy stops
set -e
printf "\033[0;32mDeploying updates to GitHub...\033[0m\n"
# Build the project.
hugo # if using a theme, replace with `hugo -t <YOURTHEME>`
# Go To Public folder
cd public
# Ensure submit to object branch
git clone git@github.com:sr-c/sr-c.github.io.git master
# Add changes to git.
git add .
# Commit changes.
msg="rebuilding site $(date)"
if [ -n "$*" ]; then
msg="$*"
fi
git commit -m "$msg"
# Push source and build repos.
git push origin master
|
注意,此处要求先删去已有的public目录,但是public已被注册称为子项目,最好在git中删除该submodule,否则如果下一步提交不成功,那么可能会存在问题。删除submodule的方式记录如下:
1
2
3
4
5
6
7
8
|
# Remove the submodule entry from .git/config
git submodule deinit -f path/to/submodule
# Remove the submodule directory from the superproject's .git/modules directory
rm -rf .git/modules/path/to/submodule
# Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule
git rm -f path/to/submodule
|
托管至master分支中的docs目录
https://sspai.com/post/59904中推荐将Github Page使用docs目录进行部署,但是实测目前该方法已经失效。
Github对于个人用户的<username>.github.io或<orgname>.github.io的Page页面现在只能通过master分支进行部署,不支持自定义,也不支持使用docs目录了。
参考来源
https://scarletsky.github.io/2019/05/02/migrate-hexo-to-hugo/
https://www.flysnow.org/2018/07/29/from-hexo-to-hugo.html
https://io-oi.me/tech/hugo-vs-hexo/
https://ouuan.github.io/post/from-hexo-to-hugo/
https://blog.eric7.site/2020/01/05/%E8%BF%81%E7%A7%BBhexo%E5%8D%9A%E5%AE%A2%E5%88%B0hugo/