基本安装

搭建站点

1
hugo new site <name of site> -f yml

初始化 Git 仓库

1
2
git init
git branch -m main # 兼容 Github 的设置

安装主题

1
git submodule add --depth=1 https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod

若是已经安装过主题的,需要下面的命令激活

1
git submodule update --init --recursive

本地调试

1
hugo server

添加新文章

1
hugo new posts/my-first-post.md

配置

配置 config.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
baseURL: "https://examplesite.com/"
languageCode: zh-cn
title: ExampleSite
theme: PaperMod
timeZone: Asia/Shanghai

enableInlineShortcodes: true
enableGitInfo: true
enableRobotsTXT: true
enableEmoji: true
hasCJKLanguage: true

outputs:
    home: [HTML, RSS, JSON]

Params:
  title: ExampleSite
  description: "ExampleSite description"
  author: xxx
  homeInfoParams:
      Title: Hi there wave
      Content: Can be Info, links, about...

  socialIcons: # optional
      - name: rss
        url: /index.xml

  ShowFullTextinRSS: true
  ShowReadingTime: true
  ShowCodeCopyButtons: true
  DateFormat: "2006-01-02"  # 日期格式化

menu:
  main:
  - identifier: home
    name: 主页
    url: /
    weight: 10
  - identifier: search
    name: 搜索
    url: /search
    weight: 20
  - identifier: tags
    name: 标签
    url: /tags
    weight: 30
  - identifier: archives
    name: 时间轴
    url: /archives
    weight: 40

配置 content/archives.md

1
2
3
4
5
---
title: "时间轴"
layout: "archives"
summary: archives
---

配置 content/search.md

1
2
3
4
5
6
---
title: "搜索" # in any language you want
layout: "search" # is necessary
summary: "search"
placeholder: "Typing something..."
---

增加 Latex 数学公式的支持

layouts/partials 路径下新建文件 extend_head.html

1
{{ if or .Params.math .Site.Params.math }} {{ partial "math.html" . }} {{ end }}

math.html 文件:

 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
<script>
  MathJax = {
    tex: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"],
      ],
      displayMath: [
        ["$$", "$$"],
        ["\\[", "\\]"],
      ],
      processEscapes: true,
      processEnvironments: true,
    },
    options: {
      skipHtmlTags: ["script", "noscript", "style", "textarea", "pre"],
    },
  };

  window.addEventListener("load", (event) => {
    document.querySelectorAll("mjx-container").forEach(function (x) {
      x.parentElement.classList += "has-jax";
    });
  });
</script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script
  type="text/javascript"
  id="MathJax-script"
  async
  src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-svg.js"
></script>

利用 Github Actions 自动发布

编写 Github Actions 脚本

.github/workflows 下新建文件 build.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
36
37
38
39
40
41
42
43
44
# This is a basic workflow to help you get started with Actions

name: Auto Deploy Hugo

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]
  pull_request:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
        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@v2
        with:
          hugo-version: latest
          extended: true

      - name: Build
        run: hugo --minify

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        if: ${{ github.ref == 'refs/heads/main' }}
        with:
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # secret 中设置好私钥
          external_repository: your-repo/your-repo.github.io  # Page 仓库
          publish_branch: main  # Page 仓库的分支
          publish_dir: ./public # 静态网页路径
          commit_message: ${{ github.event.head_commit.message }}

记得在 Page 仓库的设置中开启 Github Pages,选择 main 分支,用你的仓库名替换 your-repo/your-repo.github.io

生成私钥

1
ssh-keygen -t rsa -b 4096 -C "$(git config user.email)" -f gh-pages -N ""

你将得到两个文件:

  • gh-pages.pub 是 Public Key
  • gh-pages 是 Private Key

在 Github 中设置信息

  • 在本项目目录下设置 SectetsACTIONS_DEPLOY_KEY 信息,填入 Private Key
  • 在 Pages 项目下设置 Deploy Keys,填入 Public Key,记得选中 Allow write access
添加 public key Success
deploy-keys-1 deploy-keys-2
添加 private key Success
secrets-1 secrets-2