↓ 跳过正文
  1. 博客/

用 Hugo 和 Blowfish 搭建你的博客

··2394 字·12 分钟·
小钰大主宰
作者
小钰大主宰
建站工程师
目录

本文将演示如何用 Hugo 和 Blowfish 从零开始把博客跑起来。Hugo 是最流行的开源静态站点生成器之一,Blowfish 则是 Hugo 上一个强大而轻量的主题。

我们会用 Debian 发行版,但这些说明适用于所有 Debian 衍生版。而且,如果你用的是 Fedora 之类的其他发行版,只需改变安装 Git 与 Hugo 的方式。

系统依赖
#

在我们的场景里,Git 分布式版本控制系统是唯一需要的工具。我们会用它把 Blowfish 主题安装为 Git submodule、从本地 Git 仓库读取提交信息、以及在 Github 上保存网站的一份副本。

sudo apt install git

安装 Hugo
#

我们会按官方安装说明的建议安装 Hugo 的扩展版,使用从 Github 最新发布页下载的 Debian 包。下面这组命令会自动完成这件事(如需要,调整第一行的版本号):

HUGO_VERSION="0.167.0"
wget https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \
    --output-document /tmp/hugo_extended_${HUGO_VERSION}_linux-amd64.deb
sudo dpkg --install /tmp/hugo_extended_${HUGO_VERSION}_linux-amd64.deb
rm /tmp/hugo_extended_${HUGO_VERSION}_linux-amd64.deb

需要升级到更新版本的 Hugo 时,下载新版本的包安装、覆盖现有版本即可。

可以用 hugo version 命令查看已安装的版本。

仓库
#

在 Github 上创建一个新仓库。

  • 起一个简短好记的名字,例如 mywebsite。
  • 加上描述,例如「使用 Hugo 和 Blowfish 的个人网站」。
  • 按需设为公开或私有。
  • 可选:勾选添加 README.md 文件。
  • Github 上没有针对 Hugo 的 .gitignore 模板,所以我们会稍后自己建一个。
  • 为项目选一个许可证,例如 AGPL-3.0。

Github 提供了一些写得很好的指南,讲如何写一份好的 README 文件,以及如何为仓库选择许可证。

把仓库克隆到本地。我们会用 ~/Sites 作为项目的父目录,你也可以选别的位置。

cd ~/Sites
git clone [email protected]:<username>/mywebsite.git

参考文档了解如何为 Github 账号配置 SSH 访问。

创建站点
#

创建 Hugo 站点非常简单。刚克隆的仓库根目录里执行下面这条命令即可。

cd ~/Sites/mywebsite
hugo new site . --force

安装 Blowfish
#

我们会按 Blowfish 安装说明的建议,把它安装为 Git submodule。我们使用 main 分支上最新版本的 Blowfish,浅克隆:

cd ~/Sites/mywebsite
git submodule add --branch main --depth 1 \
  https://github.com/nunocoracao/blowfish.git themes/blowfish

可以在主题的 config.toml 文件里查看它支持的最高 Hugo 版本,并据此调整你的 Hugo 版本。

需要升级到更新版本的主题时,在网站根目录执行下面的 Git 命令。

cd ~/Sites/mywebsite
git submodule update --remote --merge

更新 submodule 之后,还需要暂存并提交这些改动:

git add themes/blowfish
git commit -m "chore: bump Blowfish to version x.y.z"

submodule 更新后,站点需要重新构建。

把 submodule 标记为浅克隆,这样日后再次克隆仓库时只下载最新修订:

cd ~/Sites/mywebsite
git config -f .gitmodules submodule.themes/blowfish.shallow true

这条命令只是往 .gitmodules 文件的 themes/blowfish submodule 里加了 shallow = true。

需要把仓库克隆到另一台电脑时,要记住:克隆含 submodule 的仓库必须显式初始化它们。

GIT_SSH_COMMAND="ssh -i ~/.ssh/your_key -o IdentitiesOnly=yes" \
  git clone --recurse-submodules \
  [email protected]:<username>/mywebsite.git

如果你只有一把 SSH 密钥,可以去掉 GIT_SSH_COMMAND 选项。

现在在新仓库目录里执行下面的命令,这样你就能立刻开始工作:

git config core.sshCommand "ssh -i ~/.ssh/your_key -o IdentitiesOnly=yes"
git config user.name "Your Name"
git config user.email "[email protected]"

最后,创建一个 .gitignore 文件,把某些文件自动排除在仓库之外。

# Hugo
.DS_Store
.hugo_build.lock
*.test
imports.*
dist/
public/

现在可以把第一批改动提交到仓库了。

git add .
git commit -a -m "Added Blowfish theme and gitignore file"

随着教程推进,每当你觉得有了新的稳定版本,别忘了把改动提交到本地仓库。命令如下:

命令说明
git add .把所有改动加入暂存区
git commit -m “Message”带提交消息提交改动
git push把改动推送到远程仓库

本文中我会省略 git push 命令,因为默认你会在每节结束时把改动推送到远程仓库。不过你随时都可以推送。

配置 Blowfish
#

删除 Hugo 在网站根目录生成的 hugo.toml 文件,然后把 themes/blowfish/config/_default/ 子目录里的 *.toml 文件复制到 config/_default/ 目录。这样你就有了全部正确的配置文件,可以开始定制它们。

cd ~/Sites/mywebsite
rm hugo.toml
mkdir --parents config/_default
cp --archive themes/blowfish/config/_default/*.toml config/_default/

在创建内容之前,我们先过一遍所有配置文件并设定几条指令。

站点配置
#

首先编辑 hugo.toml,设置主题、语言与站点的 base URL。languageCode 应设为你撰写内容时主要使用的语言。

# config/_default/hugo.toml

theme = "blowfish"
baseURL = "https://mywebsite.com/"
languageCode = "en"

语言
#

接下来配置语言设置。Blowfish 支持多语言,但目前我们只配置主语言。

在 config/_default/ 配置目录里找到 languages.en.toml 文件。如果你刚在 hugo.toml 里配置了其他语言,就把文件名改成对应的。例如用加泰罗尼亚语时,你会写 languageCode = "ca",并把文件重命名为 languages.ca.toml。

title = "The title of my website"

[params]
  logo = "img/logo.png"

[params.author]
  name = "My Name"
  image = "img/author.jpg"
  headline = "This text will appear beneath the author profile in the home page"
  bio = "This text will appear next to the author icon in each article"
  links = [
    { email = "mailto:[email protected]" },
    { github = "https://github.com/<username>/" },
  ]

logo 属性里的图片会显示在页头站点名左侧。

[author] 配置决定作者信息在网站上的展示方式。图片应放在 assets/ 目录。链接按列出的顺序显示。

下面是当前目录结构的概览。

.
├── assets
│   └── img
│       └── author.jpg
├── config
│   └── _default
│       ├── hugo.toml
│       ├── languages.en.toml
│       ├── menus.en.toml
│       └── params.toml
├── content
└── themes
    └── blowfish

配色方案
#

Blowfish 内置了几套配色方案。要更换方案,只需在 config/_default/params.toml 里把 colorScheme 主题参数设为任一合法选项。

colorScheme = "avocado"
defaultAppearance = "dark"
autoSwitchAppearance = true

注意我们把默认外观从 light 改成了 dark。不过,把 autoSwitchAppearance 设为 true 会让这个选项失效,因为 Blowfish 会采用访客操作系统的配置。

另外,回到 config/_default/languages.en.toml 配置文件,我们可以为次要外观(本例中是 light)设置一个互补的 logo:

[params]
  logo = "img/logo.png"
  secondaryLogo = "img/secondary-logo.png"

隐私设置
#

如果你像我一样不打算把 Hugo 网站与外部服务集成,可以利用 hugo.toml 里的隐私配置选项禁用所有服务。

[privacy]
  [privacy.disqus]
    disable = true
  [privacy.googleAnalytics]
    disable = true
  [privacy.instagram]
    disable = true
  [privacy.twitter]
    disable = true
  [privacy.vimeo]
    disable = true
  [privacy.youtube]
    disable = true

可选:把这些改动提交到仓库:

git add .
git commit -m "Configured the site, language, colour and privacy settings"

组织内容
#

在 Hugo 里,内容应按最终渲染出的网站结构来组织。所有内容都放在 content 目录下。虽然 Hugo 支持任意层级的内容嵌套,但最顶层的目录(即 content/<directory>)是特殊的,被视为决定布局的内容类型。

Hugo 默认使用 posts、tags 和 categories。因此 Blowfish 默认配置为使用 posts。查看 config/_default/params.toml 里 showMoreLinkDest 选项的值就能发现这个默认行为。

不过 Blowfish 并不强制你使用某种特定的内容类型。静态站你可能想用 pages,博客用 posts,作品集用 projects。无论最终选哪种,理解 Hugo 期望内容如何组织都很重要,因为主题的设计充分利用了 Hugo 页面包(page bundle)。

此外,分类法(taxonomy)是一种分类机制,用于以合乎逻辑的方式给内容分类,让用户能轻松浏览站点、理解其用途。术语(term)是分类法里的键,值则是被分配给该术语的内容。视觉上看,这就像网站里不同的栏目和页面,或博客里的分类。

默认情况下,Blowfish 在 config/_default/hugo.toml 里定义了以下分类法:

[taxonomies]
  tag = "tags"
  category = "categories"
  author = "authors"
  series = "series"

注意:惯例是写成 单数 = "复数"。

分类(category)用于组织站点,让读者能找到想要的信息。它们是高层次的主题,便于人们理解你博客讲什么、并导航到感兴趣的内容。分类通过把单篇文章和子主题归到几个主主题下,为站点提供结构。

而标签(tag)表明某篇文章讲什么。每个标签是一个到三个词,把文章归入某个归档。标签不应与分类同名,以免重叠。

因此,我们刚找到了一个用 Hugo 搭博客的简易起点:

  • 所有文章都用同一个内容类型 posts。
  • 每篇文章属于一个分类,例如起步用的 frameworks、virtualisation、automation 和 monitoring。
  • 每篇文章有若干标签,例如 hugo、proxmox、ansible、prometheus 等。

使用分类法时,Hugo 会自动生成两类页面:列出该分类法全部术语的页面,以及每个术语对应的内容列表页面。按默认值,Hugo 会生成:

  • mywebsite.com/categories/ 一个页面,列出该分类法下的所有术语。
  • 每个术语各自的分类列表页(例如 mywebsite.com/categories/some-term/),展示所有被标记为该分类法组成部分的页面列表。

注意:mywebsite.com/index.xml 本质上就是你的 RSS 源,改 URL 就能指向站点的任何栏目。例如 mywebsite.com/posts/index.xml 就是 posts 文件夹的 RSS 源。

创建内容
#

那么,为了开始,我们会为 posts 内容类型创建 content/posts/ 子目录,并在 content/ 与 content/posts/ 里创建几个 Markdown 文件。

它们都会在顶部包含 front matter,这是我们给内容添加元数据的方式。Hugo 接受 YAML、TOML 和 JSON 格式的 front matter。虽然 TOML 曾是默认格式,但 YAML 已成为事实标准,所以我们也用它。

可用字段很多,但起步时我们给所有文件设 title、description 和 date,如果是文章,再加上 summary、categories 和 tags。

# Example of content/_index.md
---
title: "The title of the home page"
description: "The description of the home page."
date: "2024-07-18"
---

Additional content displayed in the home page.
# Example of content/about.md
---
title: "The title of the about me page"
description: "The description of the about me page."
date: "2024-07-18"
---

This is the content of the about me page.
# Example of content/posts/_index.md
---
title: "Blog posts"
description: "List of articles, grouped by year."
---

List of articles in the blog, grouped by year.
# Example of content/posts/ansible-proxmox-lxc.md
---
title: "Ansible playbook to provision LXC in Proxmox"
description: "This text will appear in the description meta tag."
summary: "This text will be displayed when the article appears in a list."
date: "2024-08-22"
categories: ["automation"]
tags: ["proxmox", "ansible", "pve"]
slug: "ansible-proxmox-lxc"
---

This article explains how to create an Ansible playbook that leverages the `proxmoxer` module to provision a LinuX Container in our Proxmox Virtual Environment.

以这三个示例为参照,我们可以创建其余文章文件,得到这样的内容树。

.
└── content
    ├── _index.md                     → /
    ├── about.md                      → /about/
    ├── posts
    │   ├── _index.md                 → /posts/
    │   ├── ansible-proxmox-lxc.md    → /posts/ansible-proxmox-lxc/
    │   ├── hugo-installation.md      → /posts/hugo-installation/
    │   ├── hugo-key-concepts.md      → /posts/hugo-key-concepts/
    │   ├── pbs-introduction.md       → /posts/pbs-introduction/
    │   ├── pve-design.md             → /posts/pve-design/
    │   ├── pve-introduction.md       → /posts/pve-introduction/
    │   ├── pve-os-configuration.md   → /posts/pve-os-configuration/
    │   └── pve-os-installation.md    → /posts/pve-os-installation/
    └── privacy.md                    → /privacy/

关于 content/_index.md 与 content/posts/_index.md:_index.md 在 Hugo 里有特殊作用,它让你给 home、section、taxonomy 和 term 页面添加 front matter 与内容。你可以为首页建一个 _index.md,并为每个内容栏目、分类法和术语各建一个。

关于 content/about.md,它就只是一个独立页面。

现在可以在网站根目录启动服务器了。Web 服务器默认在 http://localhost:1313/ 地址上提供服务。

hugo server --buildDrafts

传给服务器的 buildDrafts 选项会让它在 front matter 里标了 draft: true 的文章也包含进来,这在你写新文章但还没写完时非常有用。

假设分类法是这样用的。

文章分类标签
ansible-proxmox-lxcautomationansible, proxmox, pve
hugo-installationframeworkshugo, blowfish
hugo-key-conceptsframeworkshugo
pbs-introductioninfrastructureproxmox, pbs
pve-introductionvirtualisationproxmox, pve
pve-designvirtualisationproxmox, pve
pve-os-configurationvirtualisationproxmox, pve
pve-os-installationvirtualisationproxmox, pve

Hugo 会生成以下额外 URL。

  • http://localhost:1313/categories/automation
  • http://localhost:1313/categories/frameworks
  • http://localhost:1313/categories/infrastructure
  • http://localhost:1313/categories/virtualisation

这些 URL 会各自列出被分配到对应分类的文章。此外,Hugo 还会生成以下额外 URL。

  • http://localhost:1313/tags/ansible
  • http://localhost:1313/tags/blowfish
  • http://localhost:1313/tags/hugo
  • http://localhost:1313/tags/proxmox
  • http://localhost:1313/tags/pbs
  • http://localhost:1313/tags/pve

这些 URL 会各自列出与对应标签关联的文章。

而且,上述所有 URL 都同时包含 index.html 和 index.xml,后者作为该栏目的 RSS 源。

与本节前面给出的首页示例类似,你可以定制任何分类法或术语页面的引导文字与元数据。

# Example of content/categories/_index.md
---
title: "Categories"
description: "The list of categories this blog is split into."
date: "2024-07-18"
---

These are the available categories in this blog. Click on any of them to see the list of articles, its amount being displayed as a counter in each card.
# Example of content/categories/frameworks/_index.md
---
title: "Frameworks"
description: "The list of articles in the frameworks category."
date: "2024-07-18"
---

This is the frameworks category, which covers all articles related to software development frameworks, such as Hugo or Django.
# Example of content/categories/virtualisation/_index.md
---
title: "Virtualisation"
description: "The list of articles in the virtualisation category."
date: "2024-07-18"
---

This is the virtualisation category term, which covers all articles related to technologies that allow dividing of physical computing resources into a series of virtual machines, operating systems, processes or containers, mainly via the Proxmox Virtual Environment.
---

对项目文件所做的大部分改动会自动出现在浏览器里,因为 Hugo 会检测到它们、重新编译必要的文件并为你刷新页面。但有些改动不会,那就需要你停掉服务器再重新启动。

菜单
#

Blowfish 主题包含两个菜单:页头的 main 菜单和出现在页面底部的 footer 菜单。两者都在 menus.en.toml 文件里配置。与语言配置文件类似,如果你在 hugo.toml 里配置了其他语言,就把文件名改成对应的。

name 参数指定菜单链接上显示的文字。可选地,可以提供一个 title,它会用在 HTML 标签对应的属性里。

pageRef 参数让你能方便地引用 Hugo 内容页面和分类法,而 url 参数用于链接外部 URL。

pre 参数让你可以从 Blowfish 的图标集里选一个图标,显示在菜单项名称旁边。

默认情况下菜单按字母顺序排列,这可以用 weight 参数覆盖。之后菜单会按 weight 从小到大排列。

同样,Blowfish 默认配置为使用 posts 内容类型以及 categories 和 tags 分类法,所以为了匹配我们当前的内容,只需在 config/_default/menus.en.toml 里取消若干条目的注释,再加几条。

[[main]]
  name = "Blog"
  title = "The list of articles in this blog"
  pageRef = "posts"
  weight = 10

[[main]]
  name = "Categories"
  pageRef = "categories"
  weight = 20

[[main]]
  name = "Tags"
  pageRef = "tags"
  weight = 30

[[main]]
  name = "About"
  pageRef = "about"
  weight = 40

[[main]]
  name = "About"
  title = "About me"
  pageRef = "about"
  weight = 40

[[main]]
  identifier = "Github"
  pre = "github"
  url = "https://github.com/username"
  weight = 50

[[main]]
  identifier = "Email"
  pre = "email"
  url = "mailto:[email protected]"
  weight = 70

[[footer]]
  name = "Privacy policy"
  pageRef = "privacy"
  weight = 10

缩略图
#

Blowfish 提供了一种简单的方式为文章增加视觉辅助——缩略图,它还会被用于各社交平台上的 oEmbed 卡片。要使用这个特性,你需要把单份 Markdown 文件改成一个文件夹,也就是叶子包(leaf bundle)。

做法是:先创建与文章同名的目录,然后在里面创建 index.md 写入文章内容,最后放一个名为 featured.webp 的图片文件(支持 JPG、PNG、SVG 和 WebP 格式)。

配置文件 config/_default/params.toml 允许通过 cardView 参数把文章列表渲染成行或卡片墙。画布大小决定图片如何渲染:高度在 180 到 320 像素之间,宽度在 260 到约 580 像素之间。

宽高比为 3:2(例如 600x400 像素)的图片会比较合适。RedKetchup 的图片缩放器这类在线工具会很有帮助。

最终效果大致如下。

.
└── content
    ├── _index.md                     → /
    ├── about.md                      → /about/
    ├── posts
    │   ├── ansible-proxmox-lxc
    │   │   ├── index.md              → /posts/ansible-proxmox-lxc/
    │   │   └── featured.webp
    │   ├── hugo-installation
    │   │   ├── index.md              → /posts/hugo-installation/
    │   │   └── featured.webp
    │   ├── hugo-key-concepts
    │   │   ├── index.md              → /posts/hugo-key-concepts/
    │   │   └── featured.webp
    │   ├── pbs-introduction
    │   │   ├── index.md              → /posts/pbs-introduction/
    │   │   └── featured.webp
    │   ├── pve-design
    │   │   ├── index.md              → /posts/pve-design/
    │   │   └── featured.webp
    │   ├── pve-introduction
    │   │   ├── index.md              → /posts/pve-introduction/
    │   │   └── featured.webp
    │   ├── pve-os-configuration
    │   │   ├── index.md              → /posts/pve-os-configuration/
    │   │   └── featured.webp
    │   └── pve-os-installation
    │       ├── index.md              → /posts/pve-os-installation/
    │       └── featured.webp
    └── privacy.md                    → /privacy/

注意存放文章内容的文件 index.md 并不以下划线开头。

Favicon
#

要覆盖 Blowfish 主题提供的默认空白图标,需要把 favicon 资源直接放在 static/ 文件夹下,并按下面的清单命名。

.
└── static
    ├─ android-chrome-192x192.png
    ├─ android-chrome-512x512.png
    ├─ apple-touch-icon.png
    ├─ favicon-16x16.png
    ├─ favicon-32x32.png
    ├─ favicon.ico
    └─ site.webmanifest

推荐的做法是用第三方服务生成这些文件,例如 RedKetchup 的 Favicon 生成器或 favicon.io 的 Favicon 生成器,它们产出的文件名正好就是这些。只需上传你的图片(至少 512x512 像素),下载包含全部所需文件的 Zip 包,直接解压到网站的 static/ 文件夹即可。

微调布局
#

最后一些可选的修饰,用于调整文章、文章列表和分类术语在博客里的展示方式。所有这些参数都在 config/_default/params.toml 配置文件里。

文章
#

适用于文章页的参数非常多,下面是一些最相关的。

  • showDateUpdated 允许在文章头部显示最后更新日期。如果你经常更新文章,这会很有用。
  • enableCodeCopy 决定是否为 <code> 块启用复制到剪贴板的按钮。如果你展示程序代码,这会很有用。
  • showBreadcrumbs 允许在文章头部显示面包屑。
  • showTableOfContents 显示文章目录,默认在右侧一列。
  • showTaxonomies 允许在标题下方显示与文章相关的分类法。
  • showZenMode 为文章启用禅模式阅读功能,它会去掉右侧的目录列,让文章内容占满空间。
enableCodeCopy = true

[article]
  showDateUpdated = true
  showBreadcrumbs = true
  showTableOfContents = true
  # showRelatedContent = true
  # relatedContentLimit = 3
  showTaxonomies = true
  showZenMode = true

当网站内容足够多之后,你可能想在文章底部列出相关文章。Blowfish 的默认配置只要求你取消下面几个参数的注释:

  • showRelatedContent 会为每篇文章显示相关内容。
  • relatedContentLimit 限制显示的相关文章数量。

Hugo 允许微调相关内容建立索引的方式,必要时可在全局和语言级别分别设置。这些配置选项位于 config/_default/hugo.toml 文件的 [related] 段。调整这些参数会改变 Hugo 决定哪些文章出现在 Related 区块的方式。

[related]
  threshold = 0
  toLower = false

    [[related.indices]]
        name = "tags"
        weight = 100

    [[related.indices]]
        name = "categories"
        weight = 100

    [[related.indices]]
        name = "series"
        weight = 50

    [[related.indices]]
        name = "authors"
        weight = 20

    [[related.indices]]
        name = "date"
        weight = 10

    [[related.indices]]
      applyFilter = false
      name = 'fragmentrefs'
      type = 'fragments'
      weight = 10

上面的配置取自 Blowfish 的默认值,它让 Hugo 优先以文章共有的标签和分类为主要判据,其次是文章所属的系列,最后是共有作者与相同日期。最后一个块让 Hugo 为计算相关内容而索引文章的元数据。

文章列表
#

适用于文章列表的参数相当多,下面只是几个值得一提的。

  • showSummary 在每篇列出的文章里包含摘要。摘要若未写在各文章的 front matter 里,会用 config/_default/hugo.toml 里的 summaryLength 自动生成。
  • showCards 让每篇文章以卡片而不是简单内联文本的形式展示。
  • cardView 把列表显示为卡片墙。
  • constrainItemsWidth 把条目宽度限制为摘要的宽度,在没有特色图片时提升可读性。
[list]
  showSummary = true
  showCards = true
  cardView = true
  constrainItemsWidth = false

分类法
#

关于分类法和分类术语,有一个参数对保持博客风格一致很相关:

  • cardView 把列表显示为卡片墙。

它们是:

[taxonomy]
  cardView = true

[term]
  cardView = false

taxonomy 段指的是示例网站里的 /tags/ 和 /categories/,而 term 段指的是每个分类法的每个术语页面,例如 /tags/proxmox/ 或 /categories/frameworks/。

配置首页
#

Blowfish 提供多种首页布局。与文章、列表、分类法和术语一样,你可以在 config/_default/params.toml 里找到一批参数来微调所选布局。

# We only want entries of content type `posts` to be listed
# in the recent section of the homepage.
mainSections = ["posts"]

highlightCurrentMenuArea = true

[homepage]
  layout = "profile"
  showRecent = true
  showRecentItems = 6
  showMoreLink = true
  showMoreLinkDest = "/posts"
  cardView = true

上面的配置会使用默认的 profile 布局、在菜单里高亮当前项、以卡片视图显示最近六篇文章(按元数据里的日期排序),并在最近文章列表底部显示 Show More 按钮,链接到 /posts。

如果你想用背景图,需要改 layout 参数并往 assets/ 文件夹里加一张图片。你会注意到 Blowfish 会让图片适配 config/_default/params.toml 里定义的配色方案以及首页所选布局。

[homepage]
  layout = "background"
  homepageImage = "img/background.webp"

系列
#

如果你想把手头很大的主题拆成多篇文章,也就是把文章归成一组,Blowfish 的系列功能正是你要找的。

把文章归入系列后,会在每篇文章的顶部和底部提供在它们之间快速跳转的方式。Blowfish 在 config/_default/hugo.toml 里预配了 series 分类法,所以我们只需在 front matter 里用 series 和 series_order 参数标记文章。

以我们 virtualisation 分类里的四篇文章为例,我们会把它们的 front matter 改成类似这样。

# content/posts/pve-introduction/index.md
---
title: "Your Proxmox VE 7 cluster at Hetzner"
date: 2024-07-30
description: "Install and configure a multi-node Proxmox Virtual Environment cluster using dedicated servers at Hetzner."
summary: "Plan, install and configure a Proxmox VE cluster using dedicated servers at Hetzner"
categories: ["virtualisation"]
tags: ["proxmox", "pve"]
slug: "pve-introduction"
series: ["PVE"]
series_order: 1
---
# content/posts/pve-design/index.md
---
title: "Design considerations"
date: "2024-07-30"
description: "Giving our Proxmox cluster some thought before getting started."
summary: "Design the network configuration of your cluster before you get started."
categories: ["virtualisation"]
tags: ["proxmox", "pve"]
slug: "pve-design"
series: ["PVE"]
series_order: 2
---

你可以选择系列区块默认折叠还是展开:在全局层面用 config/_default/params.toml 里的 seriesOpened,在文章层面用每篇文章 front matter 里的 seriesOpened 参数。默认行为是折叠。

赋给 series_order 的值不必连续,但由于 Blowfish 渲染该区块的方式,它们理应连续。

用栏目改进内容组织
#

到这时你一定已经意识到,我们一直在给文章的文件夹名加前缀以避免冲突、也让它们更容易被发现。但最终我们的 content/posts/ 文件夹会被大量文件夹和文件塞满。

记住 Hugo 假定用来组织源内容的结构,就是用来组织渲染后站点的结构。

栏目(section)是一个顶层内容目录,或任何含 _index.md 文件的内容目录,也称为分支包(branch bundle)。类比物理上的树枝,分支包可以有后代,包括叶子包和其他分支包。

内容类型也被视为一个栏目,尽管它内部没有 _index.md 文件,因为它是顶层内容目录。

content/posts/ 里含 _index.md 的文件夹(即栏目)会成为 URL 路径的一部分、拥有逻辑上的祖先与后代,这有助于以层级方式渲染内容;同时会有一个列表页(对应其 _index.md)列出该栏目下的文章。

栏目内的文章不会出现在顶层列表(例如 /posts/)里,出现在列表里的是该栏目本身。不过它们仍会出现在最近文章或相关文章区块中。此外,某个栏目(例如 content/posts/hugo/)的文章列表只包含第一层后代。

content/posts/ 里不含 _index.md 的文件夹(例如 content/posts/hugo/),即非栏目,会成为 URL 路径的一部分,但不生成列表页。

因此非栏目会返回 404 Page Not Found,尽管它们不会出现在生成站点的任何列表里。它们唯一的作用是帮助在磁盘上组织内容,而该结构会成为路径的一部分。

以我们的例子展开,可以用非栏目把内容重组得更有层次。我们仍然只用一个内容类型 posts,分类法也保持不变。

.
└── content
    ├── _index.md                     → /
    ├── about.md                      → /about/
    ├── posts
    │   ├── _index.md                 → /posts/
    │   ├── ansible                   → /posts/ansible/
    │   │   └── lxc-provisioning
    │   │       ├── index.md          → /posts/ansible/lxc-provisioning/
    │   │       └── featured.webp
    │   ├── hugo
    │   │   ├── installation
    │   │   │   ├── index.md          → /posts/hugo/installation/
    │   │   │   └── featured.webp
    │   │   └── key-concepts
    │   │       ├── index.md          → /posts/hugo/key-concepts/
    │   │       └── featured.webp
    │   └── proxmox
    │       ├── pbs
    │       │   ├── index.md          → /posts/proxmox/pbs/
    │       │   └── featured.webp
    │       └── pve
    │           ├── _index.md         → /posts/proxmox/pve/
    │           ├── featured.md
    │           ├── design
    │           │   ├── index.md      → /posts/pve/design/
    │           │   └── featured.webp
    │           ├── introduction
    │           │   ├── index.md      → /posts/pve/introduction/
    │           │   └── featured.webp
    │           ├── os-configuration
    │           │   ├── index.md      → /posts/pve/os-configuration/
    │           │   └── featured.webp
    │           └── os-installation
    │               ├── index.md      → /posts/pve/os-installation/
    │               └── featured.webp
    └── privacy.md                    → /privacy/

注意栏目与非栏目分别是怎样使用的。content/posts/ansible/、content/posts/hugo/ 和 content/posts/proxmox/ 是非栏目,只用于把内容组织进子目录;而 content/posts/proxmox/pve/ 是栏目,它把其下所有文章归成一个条目来呈现(而不是让它们各自单独展示)。

# Example of content/posts/proxmox/pve/_index.md
---
title: "Your Proxmox VE cluster"
description: "Install and configure a multi-node Proxmox Virtual Environment cluster."
summary: "Plan, install and configure a Proxmox VE cluster using dedicated servers."
categories: ["virtualisation"]
tags: ["proxmox", "pve"]
---

Install and configure a multi-node Proxmox Virtual Environment cluster using dedicated servers at Hetzner.

如果你想自定义每篇文章的路径,可以在栏目的 _index.md front matter 的 url 参数里,或站点配置文件里使用永久链接令牌。

# Example of config/_default/hugo.toml

[permalinks]
  [permalink.posts]
   posts = "/:sections/:slugorfilename"

突出分类
#

如果你想提升分类的相关性,以便通过建立清晰层级来增强 SEO 和站点导航,可以把分类变成顶层栏目——这样一来它们就不必再出现在文章的 front matter 里。

.
└── content
    ├── _index.md
    ├── about.md
    ├── automation
    │   ├── _index.md
    │   ├── lxc
    │   └── proxmox
    ├── frameworks
    │   ├── _index.md
    │   └── hugo
    ├── infrastructure
    │   ├── _index.md
    │   ├── databases
    │   ├── monitoring
    │   └── pbs
    ├── virtualisation
    │   ├── _index.md
    │   └── pve
    └── privacy.md

这需要相应调整 config/_default/menus.en.toml 里的菜单选项,也许还要新增一个 Articles 菜单项,把这些栏目作为子菜单。

组织内容的方式无穷无尽。和生活中的一切一样,想取得好结果,最好的办法是观察、规划与试验。

发布
#

当文章写完后,或者也许在写作过程中,你希望把改动持续提交到仓库。这让你能追踪改动,必要时回退。

git add .
git commit -m "Added a new article about Hugo and Blowfish"

对博客内容满意之后,你会想发布它。有两种方式:

  • 自动:把仓库连接到支持 Hugo 的托管服务商。
  • 手动:生成静态文件并上传到 Web 服务器。

第二种情况需要用到 hugo 命令:

cd ~/Sites/mywebsite
hugo --minify --cleanDestinationDir

生成的文件会放在 public/ 目录里。

--minify 选项会压缩 HTML、CSS 和 JavaScript 文件,减小文件体积、提升网站加载速度。

--cleanDestinationDir 选项会删除 public/ 目录里那些源文件中已不存在的文件,这有助于保持生成的站点干净。

Cloudflare Pages
#

在把 Hugo 站点部署到 Cloudflare Pages 之前,需要满足一些前提条件:

  1. 你在某个域名注册商处注册了域名。
  2. 你在 Cloudflare 注册了账号。
  3. 你把域名添加到了 Cloudflare。
  4. 你已配置域名使用 Cloudflare 的域名服务器。

Cloudflare 称这种配置为完整接入(full setup)。你不需要在 Cloudflare 注册域名也能用 Cloudflare Pages,但确实需要把域名添加到 Cloudflare 并配置它使用 Cloudflare 的域名服务器。

完成完整接入后,按以下步骤即可轻松部署 Hugo 站点:

  1. 登录你的 Cloudflare 控制台,选择账号(如果你有多个)。
  2. 在账号首页选择 Compute (Workers) > Workers & Pages。
  3. 选择 Pages 标签页。
  4. 在 Import an existing Git repository 卡片上点击 Get started 按钮。
  5. 选择 Github 标签页,点击 Connect GitHub 按钮。
  6. 选择存放 Hugo 站点仓库的 Github 账号。
  7. 选择 Only select repositories,从下拉列表里挑出该仓库。
  8. 点击 Install & Authorize 按钮。
  9. 回到 Cloudflare,选中刚连接的仓库,点击 Begin setup 按钮。
  10. 在 Set up builds and deployments 步骤里填写以下字段,然后点击 Continue:
字段名值备注
Project namemywebsite你的项目名称
Production branchmain默认值即可
Build commandhugo可选:在命令里加上 --minify --cleanDestinationDir
Build output directorypublic
  1. Cloudflare Pages 会开始构建与部署流程。由于仓库里已经有一个可用的 Hugo 站点,操作应在几秒内成功结束。
  2. 点击 Add custom domain 卡片,把域名加到项目上 1。在下一页点击 Set up a custom domain 按钮并输入域名,例如 mywebsite.com,然后点 Continue。
  3. 点击 Activate domain 按钮。Cloudflare 会修改 DNS 记录并签发 SSL 证书。
  4. 下一页会看到 Cloudflare 正在验证改动、等待 DNS 传播完成。完成后 Verifying 状态会变成 Active。此时你可以在 https://mywebsite.com/ 访问网站了。

在 Cloudflare Workers & Pages 控制台会看到四个标签页:

  • Deployments:显示项目的部署列表。点 View details 链接可以查看每次部署的日志。
  • Metrics:显示项目的指标,例如请求数。如果你愿意,可以在这里启用 Web Analytics。
  • Custom domains:显示与项目关联的域名列表。我们刚才就在那里。
  • Settings:允许修改项目设置,例如构建命令;更重要的是,可以通过设置 HUGO_VERSION 环境变量来使用指定版本的 Hugo。如果你想用一个不是最新的特定 Hugo 版本,这会很有用。

新的提交会触发 Cloudflare 自动构建并部署你的改动。


  1. 访问 mywebsite.pages.dev 上的项目即可预览最终效果。↩︎