从Hexo迁移到Hugo

  重新出发就要换个新皮(哈哈哈)
  本文后续优化未完待续

为什么换(可不看)

  • 之前的主题不够简洁不够轻量级,对公式支持不够,排版太丑
    博客皮肤不好看我就不想写文章啊好肤浅,这就是去年更新少的原因优秀的借口
  • hexo坑太多,npm和nodejs对于非前端人员还是不够熟
    崩崩崩,总搞崩,查前端bug比我看论文时间都多(哼╭(╯^╰)╮),受够了!
  • 刚好刷到stack这个主题,简洁且还在维护,发现是hugo的
    上年纪了好像不喜欢花里胡哨了哎呀
  • 早听过hugo,之前一直没有尝试
    还不是觉得hexo凑合凑合也能用!哼╭(╯^╰)╮
  • 趁着第十年写技术博客的契机,干脆丢了hexo,换个新的玩玩
    嘿嘿继续折腾

安装

https://gohugo.io/installation/windows/
winget install Hugo.Hugo.Extended

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ hugo new site hugo
Congratulations! Your new Hugo site was created in I:\hugo.

Just a few more steps...

1. Change the current directory to I:\hugo.
2. Create or install a theme:
   - Create a new theme with the command "hugo new theme <THEMENAME>"
   - Or, install a theme from https://themes.gohugo.io/
3. Edit hugo.toml, setting the "theme" property to the theme name.
4. Create new content with the command "hugo new content <SECTIONNAME>\<FILENAME>.<FORMAT>".
5. Start the embedded web server with the command "hugo server --buildDrafts".

See documentation at https://gohugo.io/.

初始化主题

1
2
3
cd hugo/
git init
git submodule add https://github.com/CaiJimmy/hugo-theme-stack themes/hugo-theme-stack

每个主题一般都会提供一些实例配置与初始页面,开始使用主题时可以将其 exampleSite/ 目录下的文件复制到站点目录下,在此基础上进行调整配置。

创建文章

1
hugo new posts/test/tmp.md

预览

1
hugo server -D

生成静态网页

1
hugo

推送

为了保留之前hexo的分支方便博客整垮了回退,也为了保留以前的github小绿点记录,我创建一个新分支gh-pages推送。 注意:将GitHub Pages仓库的Sittings - Pages - Build and deployment - Branch修改为gh-pages。

1
2
3
4
5
6
cd ./public/
git init -b gh-pages #创建一个新分支gh-pages
git remote add origin 仓库地址.git
git add .
git commit -m 'Hugo部署'
git push -u origin gh-pages

Github Action一键部署

这个主要是为了能够多机同步。

  1. 创建一个博客源文件的仓库(我之前建过,所以新建了一个gh-pages分支放新的)。
  2. 在hugo根目录下,修改远程仓库地址:
1
2
3
git remote -v
git remote set-url origin git@github.com:xxx/blog-source.git # 换成远程仓库地址
git remote -v
  1. 在本地仓库中创建文件.github\workflows\gh-pages.yml
 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
32
33
34
35
name: GitHub Pages

on:
  push:
    branches:
      - gh-pages  # Set a branch to deploy
  pull_request:

jobs:
  deploy:
    runs-on: ubuntu-22.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v4
        with:
          submodules: true  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v3
        with:
          hugo-version: '0.143.0' # 注意修改成你的hugo版本,以防兼容性错误
          extended: true # 是否启用hugo extended

      - name: Build
        run: hugo

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          PERSONAL_TOKEN: ${{ secrets.HUGO }} #注意换成你设置的secret名字
          PUBLISH_DIR: ./public # 注意修改成你使用hugo创建的项目名称
          EXTERNAL_REPOSITORY: xxx/xxx.github.io # 你的Github Pages远程仓库名
          PUBLISH_BRANCH: gh-pages
  1. 在Github个人信息页面下Developer setting-Personal access tokens-Token(classic)-generate new token-Generate new token(classic),创建一个tokens,名为’hugo’,注意勾选repo和workflow权限;注意及时复制token;
  2. 在博客源仓库的Settings-Secrets and variables-Actions-Repository secrets中新建一个新的secret,名为’hugo’,内容为刚刚复制的tokens;
  3. push本地的修改到远程博客源仓库,Github Action会自动完成生成静态页面和推送到GitHub Pages仓库的任务。

优化

以前很多文章是采用的hexo默认链接方式(年月日+文章名),但是如果有中文会转码成很长的乱码,很难看,也不利于seo。为了保证之前的链接可以打开,也为了之后的文章能够用简短的唯一域名,我是这样做的: 在front-matter里:

  • slug可以写唯一域名(8位uuid就行)
  • aliases可以用来作为url重定向
  • url是直接写死(不过我没用url)

一些坑

如果你的文章文件夹和我一样是posts而不是原始的post,记得配置文件里把

1
2
3
permalinks:
    posts: /:slug/
    page: /:slug/

对应的post改成posts!不然permalinks会失效,弃用的是模板里的默认项。

另外,hugo会自动把slug里的字符转为小写。

目前还未解决的问题

  • 博文toc似乎没显示一级标题
  • 如何显示博文修改日期
  • 博文如何按修改日期排序
  • 之前的博文没按标准md格式行末加俩空格换行
  • 如何显示博文点击量和博客浏览量
  • 如何将卜蒜子域名点击量修正(由于url修改)
  • 各种图标样式没来得及弄
  • 首页博文卡片的图片怎么加

不过这些都不太重要,所以这些更新可能比较缓慢。 暂时想到这些,大家要是有解决方案愿意分享可以直接评论!这样我就不用去搜索了哈哈哈。 发现新问题也可以评论留言!帮助本博客做得更好!

参考资料

使用 Hugo 构建
主题 StackJimmy 设计