如何配置 Notion 与博客自动同步

This version was translated with AI from the original article.

概览

这套同步系统能让你直接在 Notion 里写博客,并自动同步到你的个人网站上,而且完全不会改动任何现有的博客模板或样式!

工作流

Notion 数据库 → 自动同步 → Markdown 文件 → 博客 HTML → 网站更新

第一步:创建 Notion 数据库

1.1 新建数据库

  1. 在 Notion 中新建一个页面
  2. 添加一个数据库(Database)
  3. 选择“Table”视图

1.2 设置数据库字段

必填字段:

  • Title - Title 类型 - 文章标题
  • Status - Select 类型 - 选项:'Draft'、'Published'
  • Date - Date 类型 - 发布日期

选填字段:

  • Tags - Multi-select 类型 - 文章标签
  • Summary - Text 类型 - 文章摘要

1.3 数据库示例

第二步:获取 Notion API Key

2.1 创建 Integration

  1. 访问 Notion Integrations
  2. 点击“New integration”
  3. 填写相关信息: - Name:'Blog Sync' - Associated workspace:选择你的工作区 - Capabilities:勾选 'Read content'
  4. 点击“Submit”
  5. 复制 Internal Integration Token(妥善保管好,千万别泄露!)

2.2 关联数据库

  1. 返回你的 Notion 数据库页面
  2. 点击右上角的“...”菜单
  3. 选择“Add connections”
  4. 选中刚刚创建的“Blog Sync”集成

2.3 获取数据库 ID

  1. 在浏览器中打开你的数据库
  2. 从 URL 中复制数据库 ID(一段 32 位的字符串)

第三步:本地配置

3.1 设置环境变量

MacOS/Linux: 添加到 ~/.zshrc 或 ~/.bash_profile

export NOTION_TOKEN="your_integration_token_here" export NOTION_DATABASE_ID="your_database_id_here"

3.2 测试同步(Joyce 专属)

cd blog python3 sync_notion.py

第四步:配置 GitHub 自动化

4.1 设置 GitHub Secrets

  1. 访问你的 GitHub 仓库
  2. 点击“Settings” → “Secrets and variables” → “Actions”
  3. 点击“New repository secret”
  4. 添加两个 Secret: - Name: 'NOTION_TOKEN',Value: 你的 Integration Token - Name: 'NOTION_DATABASE_ID',Value: 你的 Database ID

4.2 自动同步

GitHub Actions 会:

  • 每天自动检查 Notion 的更新
  • 同步已发布的文章
  • 自动构建并部署博客

第五步:日常写作工作流

5.1 在 Notion 中写作

  1. 在数据库中新建一行
  2. 填好标题、标签和摘要
  3. 将 Status 设为 'Draft'
  4. 在页面内尽情写作(支持所有 Notion 格式)

5.2 发布文章

  1. 写完内容后,将 Status 改为 'Published'
  2. 设置发布日期
  3. 等待每天的定时自动同步,或者手动触发同步

5.3 手动同步

本地同步:

cd blog python3 sync_notion.py

GitHub 手动触发:

  1. 访问 GitHub 仓库
  2. 点击“Actions” → “Notion Blog Auto-Sync”
  3. 点击“Run workflow”

常见问题排查

常见问题

Q:同步失败并报 401 错误 A:检查 Integration Token 是否正确,并确认数据库已添加该 Integration 关联。

Q:找不到任何文章 A:确认文章的 Status 是否为“Published”,并核对数据库 ID 是否正确。

Q:文章格式错乱 A:检查 Notion 数据库的字段名是否完全一致(Title、Status、Date、Tags、Summary)。

Q:GitHub Actions 运行失败 A:检查 GitHub Secrets 是否配置正确。