前言

Hugo 是世界上最快的静态网站生成器之一,使用 Go 语言编写。本文将详细介绍 Hugo 的使用方法,以及如何使用和定制 IMX 主题。

为什么选择 Hugo?

  • 极快的构建速度 - 毫秒级生成网站
  • 🎨 丰富的主题 - 大量现成主题可选
  • 📝 Markdown 写作 - 专注内容创作
  • 🔧 灵活配置 - 强大的模板系统
  • 🚀 易于部署 - 生成纯静态文件
  • 💰 完全免费 - 开源且无需服务器

第一部分:Hugo 基础

1.1 安装 Hugo

Windows

方法 1:使用 Winget(推荐)

winget install Hugo.Hugo.Extended

方法 2:使用 Chocolatey

choco install hugo-extended

方法 3:手动安装

  1. 访问 Hugo Releases
  2. 下载 hugo_extended_*_windows-amd64.zip
  3. 解压到 C:\Hugo\bin
  4. 添加到系统 PATH 环境变量

macOS

brew install hugo

Linux

# Ubuntu/Debian
sudo apt install hugo

# Arch Linux
sudo pacman -S hugo

# 或使用 Snap
sudo snap install hugo

验证安装

hugo version

应该看到类似输出:

hugo v0.163.1+extended windows/amd64

重要:请安装 Extended 版本,它支持 SCSS/SASS 处理。


1.2 创建新站点

# 创建新站点
hugo new site my-blog

# 进入站点目录
cd my-blog

# 初始化 Git 仓库(推荐)
git init

// CONTINUE_HERE

创建后的目录结构:

my-blog/
├── archetypes/     # 内容模板
├── assets/         # 需要处理的资源(CSS、JS)
├── content/        # 网站内容(Markdown 文件)
├── data/           # 数据文件
├── layouts/        # 自定义模板
├── static/         # 静态资源(图片、字体)
├── themes/         # 主题目录
└── hugo.toml       # 配置文件

1.3 安装主题

方法 1:Git Submodule(推荐)

# 添加主题作为子模块
git submodule add https://github.com/你的用户名/imx-theme.git themes/imx-theme

方法 2:直接克隆

# 克隆主题到 themes 目录
git clone https://github.com/你的用户名/imx-theme.git themes/imx-theme

方法 3:使用本地主题

如果你已经有 IMX 主题的代码,直接复制到 themes/imx-theme 目录。


1.4 配置站点

编辑 hugo.toml(或 config.toml):

baseURL = 'https://你的域名.com/'
languageCode = 'zh-cn'
title = '你的博客名称'
theme = 'imx-theme'

[params]
  description = '你的博客描述'
  subtitle = '你的个性签名'
  author = '你的名字'
  avatar = '/images/avatar.jpg'  # 头像路径
  keywords = ['技术', '博客', '编程']

  [params.social]
    github = 'https://github.com/你的用户名'
    email = 'your-email@example.com'

  [params.giscus]
    enabled = true
    repo = "你的用户名/blog-comments"
    repoId = "你的repo-id"
    category = "Announcements"
    categoryId = "你的category-id"

[markup]
  [markup.highlight]
    anchorLineNos = false
    codeFences = true
    style = 'dracula'
    tabWidth = 4

  [markup.tableOfContents]
    endLevel = 4
    startLevel = 2

  [markup.goldmark.renderer]
    unsafe = true

1.5 创建第一篇文章

# 创建新文章
hugo new content posts/my-first-post.md

生成的文件位于 content/posts/my-first-post.md

+++
title = 'My First Post'
date = 2026-06-13T22:00:00+08:00
draft = false
categories = []
tags = []
description = ''
+++

在这里写你的文章内容...

1.6 本地预览

# 启动开发服务器(包含草稿)
hugo server -D

# 仅显示已发布的文章
hugo server

打开浏览器访问:http://localhost:1313/

热重载:修改文件后浏览器会自动刷新!

// CONTINUE_HERE


第二部分:IMX 主题使用指南

2.1 主题特性

IMX 主题是一个现代化、优雅的 Hugo 主题,具有以下特性:

  • 🎨 淡紫色配色 - 优雅的视觉风格
  • 🌓 深色/浅色模式 - 自动切换
  • 💎 毛玻璃效果 - 半透明设计
  • 📱 完全响应式 - 完美适配移动端
  • 📚 目录导航 - 自动生成文章目录
  • 💬 评论系统 - 集成 Giscus
  • 🔍 搜索功能 - 实时搜索文章
  • 📊 阅读进度条 - 显示阅读进度
  • 💻 代码高亮 - Dracula 配色
  • 性能优化 - 懒加载、压缩

2.2 文章 Frontmatter 详解

每篇文章开头的 +++ 之间的内容称为 Frontmatter,用于配置文章元数据。

基础字段

+++
title = '文章标题'                    # 必填
date = '2026-06-13T22:00:00+08:00'  # 必填,发布日期
draft = false                        # 是否为草稿(true=草稿)
+++

完整示例

+++
title = 'Hugo 博客搭建教程'
date = '2026-06-13T22:00:00+08:00'
draft = false
categories = ['技术', '教程']        # 分类(可多个)
tags = ['Hugo', '博客', 'Markdown']  # 标签(可多个)
description = '这是文章的简短描述,用于 SEO 和摘要显示'
image = '/images/post-cover.jpg'    # 封面图(可选)
+++

文章内容从这里开始...

字段说明

字段说明示例
title文章标题'我的第一篇博客'
date发布日期'2026-06-13T22:00:00+08:00'
draft是否草稿false (发布) / true (草稿)
categories文章分类['技术', 'JavaScript']
tags文章标签['Hugo', '教程']
description文章描述'这是一篇关于...'
image封面图'/images/cover.jpg'

2.3 Markdown 写作技巧

标题

## 二级标题
### 三级标题
#### 四级标题

提示:一级标题 # 已被文章标题占用,正文从 ## 开始。

强调

**粗体文字**
*斜体文字*
~~删除线~~
\`行内代码\`

列表

- 无序列表项 1
- 无序列表项 2
  - 嵌套列表项

1. 有序列表项 1
2. 有序列表项 2

链接和图片

[链接文字](https://example.com)
![图片描述](/images/photo.jpg)

代码块

支持的语言:python, javascript, bash, go, java, c, cpp, rust 等。

表格

| 列1 | 列2 | 列3 |
|-----|-----|-----|
| A   | B   | C   |
| D   | E   | F   |

2.4 内容组织

目录结构

content/
├── posts/              # 博客文章
│   ├── 2024/
│   │   ├── first.md
│   │   └── second.md
│   └── 2025/
│       └── third.md
├── about.md           # 关于页面
└── projects.md        # 项目页面

URL 结构

默认情况下,文章 URL 与文件路径对应:

  • content/posts/hello.md/posts/hello/
  • content/about.md/about/

2.5 分类和标签

创建分类页面

Hugo 会自动为每个分类生成页面:

  • /categories/ - 所有分类列表
  • /categories/技术/ - “技术"分类下的文章

创建标签页面

同样自动生成:

  • /tags/ - 所有标签列表
  • /tags/hugo/ - “Hugo"标签下的文章

使用建议

  • 分类:用于文章的大类划分(如:技术、生活、思考)
  • 标签:用于文章的详细标记(如:Python、Docker、教程)

示例:

categories = ['技术']
tags = ['Python', 'Django', 'Web开发']

2.6 图片管理

方法 1:使用 static 目录

static/
└── images/
    ├── avatar.jpg
    └── post1/
        ├── image1.jpg
        └── image2.jpg

在文章中引用:

![描述](/images/post1/image1.jpg)

方法 2:Page Bundle

为每篇文章创建独立文件夹:

content/
└── posts/
    └── my-post/
        ├── index.md
        ├── image1.jpg
        └── image2.jpg

index.md 中引用:

![描述](image1.jpg)

2.7 主题自定义

修改配色

编辑 themes/imx-theme/assets/css/main.css

:root {
  --color-primary: #C084FC;      /* 主色调 */
  --color-primary-dark: #A855F7; /* 深色主色 */
  --color-primary-light: #E9D5FF; /* 浅色主色 */
}

自定义 CSS

创建 assets/css/custom.css 并在配置中引入。


第三部分:构建和部署

3.1 构建站点

# 构建生产版本
hugo

# 构建并显示草稿
hugo -D

# 清理并构建
hugo --cleanDestinationDir

# 压缩输出
hugo --minify

构建后的文件在 public/ 目录,可直接部署。


3.2 部署到 GitHub Pages

步骤 1:创建 GitHub 仓库

仓库名:你的用户名.github.io

步骤 2:创建 GitHub Actions

创建 .github/workflows/hugo.yml 配置自动部署。

步骤 3:推送代码

git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/你的用户名/你的用户名.github.io.git
git push -u origin main

步骤 4:配置 GitHub Pages

  1. 进入仓库 Settings → Pages
  2. Source 选择 “GitHub Actions”
  3. 等待部署完成

访问:https://你的用户名.github.io


3.3 部署到 Vercel

  1. 访问 vercel.com 使用 GitHub 登录
  2. 点击 “Import Project” 选择仓库
  3. Framework 选择 “Hugo”
  4. 点击 “Deploy”

3.4 部署到 Netlify

  1. 访问 netlify.com 注册
  2. 创建 netlify.toml 配置文件
  3. 在 Netlify 点击 “New site from Git”
  4. 选择 GitHub 仓库并部署

第四部分:常用命令速查

内容管理

# 创建新文章
hugo new content posts/文章名.md

# 创建新页面
hugo new about.md

# 列出所有内容
hugo list all

# 列出草稿
hugo list drafts

服务器命令

# 启动开发服务器
hugo server

# 包含草稿
hugo server -D

# 指定端口
hugo server -p 8080

# 绑定到所有网络接口
hugo server --bind 0.0.0.0

构建命令

# 构建生产版本
hugo

# 构建草稿
hugo -D

# 清理并构建
hugo --cleanDestinationDir

# 压缩输出
hugo --minify

第五部分:疑难解答

问题 1:主题没有生效

原因:配置文件中主题名称错误。

解决

theme = 'imx-theme'  # 必须与 themes/ 目录下的文件夹名一致

问题 2:文章不显示

原因:文章为草稿状态 draft = true

解决

  • 修改 Frontmatter draft = false
  • 或使用 hugo server -D 显示草稿

问题 3:修改后没有更新

原因:浏览器缓存。

解决:硬刷新 Ctrl + F5 (Windows) 或 Cmd + Shift + R (Mac)

问题 4:样式错乱

原因:使用了非 Extended 版本的 Hugo。

解决

# 检查版本
hugo version

# 应显示 "+extended"
hugo v0.163.1+extended

重新安装 Extended 版本。


总结

通过本教程,你已经掌握了:

  • ✅ Hugo 的安装和基本使用
  • ✅ IMX 主题的配置和定制
  • ✅ Markdown 写作技巧
  • ✅ 内容组织和管理
  • ✅ 网站构建和部署
  • ✅ 常见问题解决

快速参考

操作命令
创建文章hugo new content posts/文章名.md
启动服务器hugo server -D
构建网站hugo --minify

推荐资源

  • Hugo 官方文档:https://gohugo.io/documentation/
  • Hugo 主题站:https://themes.gohugo.io/
  • Markdown 指南:https://www.markdownguide.org/

现在你已经准备好开始你的博客之旅了!记住,最重要的是持续创作分享知识

祝你写作愉快!📝✨