Obsidian 笔记同步到 Hugo 博客使用指南
工作流程概述
- 日常笔记:在 Obsidian (
E:\OBSIDIAN) 中撰写笔记 - 标记发布:在笔记的 front matter 中添加
publish: true - 同步到博客:运行同步脚本,将笔记同步到 Hugo (
E:\poster) - GitHub 集成:项目类笔记可以添加 GitHub 链接,自动显示在网站上
第一步:配置同步脚本
1.1 修改配置文件
编辑 config/sync_config.json,修改以下路径:
{
"obsidian_path": "E:/OBSIDIAN", // 你的 Obsidian 笔记目录
"hugo_content_path": "E:/poster/content", // Hugo content 目录
...
}
注意:Windows 路径使用正斜杠 / 或双反斜杠 \\
1.2 安装 Python 依赖
脚本使用标准库,无需额外安装依赖。确保你的 Python 版本 >= 3.6。
1.3 测试运行
Windows 用户(推荐)
双击 scripts/sync.bat 文件
命令行方式
cd scripts
python sync_notes.py
第二步:在 Obsidian 中标记笔记
2.1 普通文章
在笔记开头添加 front matter:
---
title: "我的文章标题"
date: "2024-01-15"
publish: true
tags: ["标签1", "标签2"]
categories: ["分类"]
---
2.2 项目笔记
---
title: "我的项目"
date: "2024-01-15"
publish: true
type: "projects"
github: "https://github.com/username/repo"
tags: ["Python", "Web"]
---
第三步:运行同步
每次想要发布笔记时,运行:
Windows: 双击 scripts/sync.bat
命令行:
cd scripts
python sync_notes.py
脚本会:
- 扫描 Obsidian 目录
- 找到所有
publish: true的笔记 - 同步到 Hugo content 目录
- 自动处理图片路径转换
- 根据
type字段决定放在posts/还是projects/
第四步:预览和部署
# 预览网站
hugo server
# 生成静态文件
hugo
# 部署(根据你的部署方式)
高级功能
自动同步(可选)
Windows 任务计划程序
- 打开"任务计划程序"
- 创建基本任务
- 设置触发器(如:每天特定时间)
- 操作:启动程序
python E:\blog\obsidian-sync\scripts\sync_notes.py
Linux Cron
# 编辑 crontab
crontab -e
# 添加每天同步一次(示例:每天凌晨2点)
0 2 * * * cd /path/to/blog/obsidian-sync/scripts && python sync_notes.py
GitHub 集成
- 在项目笔记中添加
github字段 - 在 Hugo 模板中显示 GitHub 链接(需要修改模板)
目录结构说明
blog/
├── obsidian-sync/ # 同步工具独立目录
│ ├── scripts/ # 脚本目录
│ │ ├── sync_notes.py # Python 同步脚本
│ │ └── sync.bat # Windows 批处理文件
│ ├── config/ # 配置目录
│ │ └── sync_config.json # 配置文件
│ └── docs/ # 文档目录
│ ├── README_SYNC.md # 使用指南
│ ├── obsidian_template.md # Obsidian 模板
│ └── 第一步:配置和使用说明.md
│
└── myblog/ # Hugo 项目目录(独立)
├── content/ # Hugo 内容目录
│ ├── posts/ # 文章目录(同步目标)
│ └── projects/ # 项目目录(同步目标)
└── ...
常见问题
Q: 如何取消发布某个笔记?
A: 将笔记中的 publish: true 改为 publish: false 或删除该字段,然后重新运行同步脚本。
Q: 图片路径问题?
A: 脚本会自动处理 Obsidian 的 ![[image]] 格式和相对路径,图片会被复制到 static/images/ 目录。
Q: 如何只同步特定目录的笔记?
A: 修改 scripts/sync_notes.py 中的 find_markdown_files 方法,添加目录过滤逻辑。
下一步:GitHub Actions 自动化(可选)
如果你使用 GitHub 托管代码,可以设置 GitHub Actions 自动部署:
- 将 Obsidian 笔记推送到 GitHub
- GitHub Actions 自动运行同步脚本
- 自动构建和部署 Hugo 网站
需要我帮你配置 GitHub Actions 吗?
相关链接:
- 📦 同步工具仓库(如果已上传)
- 🌐 我的 GitHub 主页