使用 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 已设置

祝搭建顺利~

相关推荐
香水5只用六神19 小时前
【DMA】存储器到外设模式实验2
c语言·git·stm32·单片机·嵌入式硬件·github·visual studio
逛逛GitHub19 小时前
团队版 OpenClaw 开源了!这个 GitHub 项目让小龙虾更好的协作。
github
Yupureki19 小时前
《C++实战项目-高并发内存池》4.CentralCache构造
c语言·开发语言·c++·单例模式·github
无限进步_21 小时前
【C++】只出现一次的数字 III:位运算的巧妙应用
数据结构·c++·git·算法·leetcode·github·visual studio
王的宝库21 小时前
Go 语言:结构体:定义、初始化、方法、组合、Tag、对齐
开发语言·后端·学习·golang
河边小咸鱼21 小时前
pdd校招实习生内推【实时更新链接】2027届实习、2026届春招
java·c++·golang
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2026-03-15)
人工智能·ai·大模型·github·ai教程
1104.北光c°1 天前
我理解的Leaf号段模式:美团分布式ID生成系统
java·开发语言·笔记·分布式·github·leaf
无限进步_1 天前
深入解析vector:一个完整的C++动态数组实现
c语言·开发语言·c++·windows·git·github·visual studio
破无差1 天前
Gitee导入的Github仓库同步更新
gitee·github