使用 Hugo + GitHub Pages + PaperMod 主题 + Obsidian 搭建开发博客

目前(2026年)一套比较稳定、好用的 Hugo 博客搭建流程,特别适合用 Obsidian 写笔记/文章,然后推送到 GitHub Pages 的游戏开发/技术类博主。

参考示例仓库(可对照): https://github.com/ZHLOVEYY/ZHLOVEYY.github.io

1. 本地环境安装(macOS + Homebrew)

复制代码
```bash
# 安装 Hugo(推荐 extended 版,支持 SCSS 等)
brew install hugo

# 新建站点
hugo new site my-game-dev-blog

cd my-game-dev-blog

# 初始化 git 仓库
git init

2. 添加 PaperMod 主题(推荐方式:submodule)

Bash

复制代码
git submodule add https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod

# 或者更轻量的浅克隆
# git submodule add --depth=1 https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod

在配置文件中启用主题:

复制代码
# hugo.toml
theme = "PaperMod"

3. 创建第一篇测试文章

Bash

复制代码
hugo new posts/my-first-godot-tutorial.md

打开文件,修改 front matter 示例:

复制代码
---
title: "我的第一个 Godot 教程"
date: 2026-02-02T12:00:00+08:00
draft: true
description: "Godot 入门小示例"
tags: ["Godot", "游戏开发", "教程"]
categories: ["游戏引擎"]
---

4. 本地预览

复制代码
# 普通预览(不含 draft)
hugo server --minify

# 显示草稿文章(推荐开发时使用)
hugo server -D --minify

访问:http://localhost:1313

5. 图片存放与路径规范(最容易出错的地方)

  • 图片统一放在 static/ 目录,例如:static/images/godot-node.png

  • Markdown 写法(推荐相对路径,从根开始):


  • 重要:关闭 Obsidian 的「附件重命名」「自动调整路径」等插件,避免破坏 Hugo 路径规则

6. GitHub Pages + Actions 自动部署

参考官方文档(2025--2026 推荐方式): https://gohugo.io/hosting-and-deployment/hosting-on-github-pages/

仓库 Settings → Pages → Source 选择 GitHub Actions

在项目根目录创建文件:.github/workflows/hugo.yml

推荐配置(适用于 PaperMod + submodule):

复制代码
name: Deploy Hugo site to Pages

on:
  push:
    branches: ["main"]
  workflow_dispatch:

permissions:
  contents: read
  pages: write
  id-token: write

concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v3
        with:
          hugo-version: 'latest'
          extended: true

      - name: Build
        run: hugo --minify

      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          path: ./public

  deploy:
    needs: build
    runs-on: ubuntu-latest
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    steps:
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

7. 解决线上图片 404 / 路径错误(最关键一步)

  1. 确保 hugo.toml 中 baseURL 正确:

    baseURL = "https://你的用户名.github.io/" # 必须以 / 结尾

  2. 创建自定义图片渲染模板(强烈推荐):

路径:layouts/_default/_markup/render-image.html

内容(保险写法):

复制代码
{{- $u := urls.Parse .Destination -}}
{{- if not $u.IsAbs -}}
  <img src="{{ printf "%s%s" $.Page.Site.BaseURL .Destination | safeURL }}" 
       alt="{{ .Text }}"{{ with .Title }} title="{{ . }}"{{ end }} />
{{- else -}}
  <img src="{{ .Destination | safeURL }}" 
       alt="{{ .Text }}"{{ with .Title }} title="{{ . }}"{{ end }} />
{{- end -}}

8. Obsidian 写作推荐工作流

  • Vault 根目录 ≈ Hugo 项目根目录(可符号链接)
  • 文章统一放在 content/posts/ 文件夹
  • 关闭 Obsidian 的 Wiki 链接自动补全(\[ ] Hugo 默认不识别)
  • 所有图片丢进 static/images/
  • 用 VS Code + Front Matter 插件管理 yaml 元数据(标题、标签、draft、分类等)

9. 常见问题快速定位

  • 图片本地 ok,线上 404 → 检查 baseURL + 是否创建 render-image.html
  • submodule 没内容 → git submodule update --init --recursive
  • public/ 目录别放东西 → 它每次构建都会被清空重建
  • 想预览草稿 → hugo server -D
  • Actions 构建失败 → 确认 submodules: recursive 已设置

祝搭建顺利~

相关推荐
知恒2 分钟前
Go环境搭建与入门
go
fthux34 分钟前
如果你用 Mac,那你可能需要 Noti Shift
macos·开源·github
程序员天天困17 小时前
Loop Engineering 实战:/goal 命令让 AI 自己写完整项目
github
徐小夕17 小时前
我们开源了一款“框架无关”的思维导图编辑器,3分钟集成到任意系统
前端·javascript·github
小爷毛毛_卓寿杰18 小时前
我把 397B 的「Agentic 大脑」塞进了 Xinference,一键部署 Nex-N2
人工智能·架构·github
小爷毛毛_卓寿杰20 小时前
我把一个 3B 模型塞进了 Xinference,然后它干掉了 DeepSeek V3.2
人工智能·开源·github
凌奕20 小时前
别用文档约束你的 Agent:聊聊 Agent 开发流程的思想
llm·github·agent
用户6757049885021 天前
你知道 Go 结构体和结构体指针调用的区别吗?一文带你彻底搞懂!
后端·go
HelloGitHub1 天前
《HelloGitHub》第 123 期
开源·github
唐青枫1 天前
别把泛型写复杂了:Go generic 从类型参数到实战封装
go