背景

我最初(一看已是十年前)使用为知笔记(WizNote)记录日常,借助它的「发布到博客」功能,结合 Hexo 和 GitHub Pages,搭建了一个自动发布的博客系统。

这个博客也跟我的技术学习一样:一旦准备开始找工作就开始折腾,一旦工作找到了就处于半荒废状态了。

当然了,技术文章和生活思考偶尔还是会写一下的,只不过一直记在了 WizNote 上,没有对外发布。

随着接触到 Obsidian ,我逐渐将记录主力从 WizNote 迁移到 Obsidian。发现 Obsdian 适合知识管理和结构化写作,并支持通过 Git 管理文件版本。

因此,我决定重构博客发布流程,实现:

  • 所有笔记通过 Obsidian 编写和管理
  • 通过 Git 同步 Obsidian 内容
  • 自动发布特定目录下的笔记为博客(如 50_Archives/博客发布 目录)
  • 利用 GitHub Actions 实现一键发布到 GitHub Pages

原始方案(WizNote 方案)

✅ 优点:Wiz上编写博客比较方便,早期 Wiz 有插件可以点击一键发布

❌ 缺点:Wiz笔记还是需要收费的;后面新版貌似没有插件了;需要本地部署一个服务,如果没有外网服务,就只能在内网触发发布;无法定制,格式支持有限

架构如下:

1
2
3
4
5
WizNote(点击发布)
↓ 触发接口
Hexo 本地生成页面
↓ 自动执行 deploy
GitHub Pages 展示博客

改进方案(Obsidian + GitHub Actions 自动发布)

✅ 优点:免费、自动化、灵活、完全自控、与 Obsidian 紧密集成

❌ 缺点:需要配置一次 GitHub Actions、Hexo、Secrets

核心思路

将 Obsidian 的 50_Archives/博客发布/ 目录作为博客内容源,通过 GitHub Action 自动触发 Hexo 构建和发布:

1
2
3
4
5
6
7
Obsidian
↓ 自动同步到 GitHub 仓库 A(life-notes)
GitHub Actions(life-notes/.github/workflows/publish.yml)
↓ 拉取博客项目仓库 B(hexo-blog)
↓ 将 50_Archives/博客发布/ 下笔记复制为 Hexo 文章
↓ hexo g && hexo d
发布到 GitHub Pages 仓库 C(yourname.github.io)

实现步骤(含细节说明)

步骤 1:准备三个 GitHub 仓库

  • 仓库 A(life-notes):存放 Obsidian 的所有笔记
  • 仓库 B(hexo-blog):Hexo 博客项目,含主题等
  • 仓库 C(yourname.github.io):用于部署 GitHub Pages(GitHub Pages 绑定到此仓库)

步骤 2:配置仓库 A(life-notes)

a. 创建目录结构

在 Obsidian 仓库内增加 blog/ 目录用于存放博客笔记。

b. 自动同步 Obsidian 到 GitHub

  1. 安装 Obsidian Git 插件(Community plugins → Obsidian Git)

  2. 配置自动推送选项:

    • 打开设置 → Obsidian Git
    • 启用 Auto pull on startupAuto push on save
    • 填入 GitHub 仓库地址(需本地已有 .git 初始化)
  3. 输入 GitHub Token(需权限包括 repo)

c. 配置 GitHub Secrets

前往 life-notes 仓库 → Settings → Secrets → Actions:

Name 说明
DEPLOY_REPO Hexo deploy 仓库地址,如:youname/youname.github.io
DEPLOY_TOKEN GitHub Token(具有 push 权限)

d:配置 GitHub Actions

新建 .github/workflows/publish.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
51
52
53
54
name: Deploy Hexo Blog

on:
push:
paths:
- '50_Archives/博客发布/**'
branches:
- main

jobs:
build-deploy:
runs-on: ubuntu-latest

steps:
- name: Clone Obsidian Notes (life-notes 仓库)
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18'

- name: Clone Hexo Blog Repo
run: |
git clone https://github.com/yourname/hexo-blog.git blog-temp
# 这是我的主题,可以根据实际情况调整
git clone https://github.com/jerryc127/hexo-theme-butterfly.git blog-temp/themes/butterfly

- name: Clean old blog posts
run: |
rm -rf blog-temp/source/_posts/*

- name: Sync Blog Notes
run: |
cp -r 50_Archives/博客发布/* blog-temp/source/_posts/

- name: Install Dependencies & Build
run: |
cd blog-temp
npm install
npx hexo clean
npx hexo generate

- name: Deploy to GitHub Pages
run: |
cd blog-temp/public
git init
git config user.name "youername"
git config user.email "youremail.gmail.com"
git checkout -b gh-pages
git remote add origin https://x-access-token:${{ secrets.DEPLOY_TOKEN }}@github.com/yourname/yourname.github.io.git
git add .
git commit -m "🚀 Deploy at $(date '+%Y-%m-%d %H:%M:%S')"
git push --force origin gh-pages

步骤 3:配置 Hexo 博客仓库 B(hexo-blog)

a. 初始化 Hexo 项目

1
2
3
npm install -g hexo-cli
hexo init .
npm install

b. 配置Hexo主题

1
# 此步骤省略,需要保证有 source/_posts 目录

c. 推送到Github仓库

1
2
3
4
5
git init
git remote add origin https://github.com/yourname/hexo-blog.git
git add .
git commit -m "init"
git push -u origin main

步骤 4:配置 GitHub Pages 仓库 C(yourname.github.io)

这个仓库用于承载最终生成的博客 HTML 页面,是用户访问你博客时实际访问的地址,例如:

1
https://yourname.github.io/

a. 创建仓库

  1. 打开 GitHub,新建一个名为 yourname.github.io 的仓库(名字必须与用户名一致,GitHub Pages 才能识别为主域)
  2. 不勾选初始化选项(README、LICENSE 等)

b. 启用 GitHub Pages 功能

  1. 打开仓库 → SettingsPages

  2. “Source” 处选择:

    • 分支:gh-pages
    • 路径:/ (root)
  3. 保存,稍等几秒,GitHub 会生成访问地址,如:

    1
    https://yourname.github.io/

c. 首次部署验证

完成 GitHub Actions 配置并推送一次笔记后,会触发自动构建流程。如果一切正常:

  • GitHub Actions 会将 Hexo 生成的内容推送到 gh-pages 分支
  • 页面几分钟内会上线,你可访问上述网址查看效果

d. (可选)自定义域名

如你拥有自己的域名,可以在此仓库根目录添加 CNAME 文件,例如:

1
blog.myname.com

然后去你的域名管理平台设置 CNAME 解析,指向 yourname.github.io

✍️ 博客写作格式要求

Hexo 使用 Markdown,但要求每篇文章前加入 YAML Front Matter,用于识别文章标题、时间、分类等。

✅ 合法格式示例:

1
2
3
4
5
6
7
8
---
title: 我的第一篇博客
date: 2025-07-24 08:00:00
tags: [Obsidian, Hexo]
categories: 技术笔记
---

这是正文内容,可以包含代码块、图片、引用等。

❗避免的问题:

  • 没有 --- Front Matter(Hexo 不会识别)
  • 使用 Obsidian 特有语法(如 ![[链接]]、Dataview 语句)

🔧 模版化写作(推荐做法)

为提高效率并避免格式错误,推荐在 Obsidian 中使用模版插件(如 Templater)实现博客模版写作。

✅ 示例模版:

1
2
3
4
5
6
7
8
9
10
---
title: {{title}}
date: {{date:YYYY-MM-DD HH:mm:ss}}
tags: [笔记]
categories: 默认分类
---

## 简介

## 正文

总结

  • 原始方案依赖 WizNote,适合初学者尝试;
  • 新方案基于 Obsidian + GitHub Action 更自动化、更可控;
  • 只需维护 Obsidian 中的 50_Archives/博客发布/ 目录,即可持续写作和发布;
  • 自动同步 + 自动发布一气呵成,最大限度提升写作效率。

遗留问题

  1. 图片如何展示和同步?
  2. 博客和笔记需要写在一起吗?