Notion 同步到 Jekyll 博客的实现方式
简要步骤
1. 获取 Notion API Token 和页面 ID
- 登录 Notion,访问 Notion Developers。

- 
    创建一个新的 Integration,获取 API Token。 
- 
    在 Notion 页面中,点击右上角的「Share」按钮,将 Integration 添加到页面。 
- 
    获取 Notion 页面 ID: 
2. 配置环境变量
- 
    在项目根目录创建 .env 文件。 
- 
    添加以下内容: 
NOTION_API_TOKEN=your_notion_integration_token
3. 配置 GitHub Actions 自动化工作流
- 
    在 .github/workflows/ 目录下创建 sync_notion.yml 文件。 
- 
    添加以下内容: 
name: Sync Notion to Jekyll
on:
  schedule:
    - cron: '0 0 * * *' # 每天 UTC 时间 00:00(北京时间 08:00)运行
  workflow_dispatch: # 支持手动触发
jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt
      - name: Run sync script
        env:
          NOTION_API_TOKEN: $
        run: python scripts/sync_notion_to_jekyll.py
      - name: Commit and push changes
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "actions@github.com"
          git add .
          git commit -m "Sync Notion content"
          git push
4. 实现同步
- 
    运行脚本:python3 scripts/sync_notion_to_jekyll.py。 
- 
    脚本会自动将 Notion 内容同步到 Jekyll 博客的 _posts 目录。 
- 
    GitHub Actions 会按照设定的时间自动运行同步任务。 
功能概述
本脚本用于将 Notion 中的内容同步到 Jekyll 博客。它通过 Notion API 获取页面内容,将其转换为 Markdown 格式,并保存到 Jekyll 的 _posts 目录中。支持处理多种 Notion 块类型,如段落、标题、列表、代码块、图片等。
详细实现过程
1. 环境配置
- 
    使用 dotenv 加载环境变量,包括 Notion API Token。 
- 
    初始化 Notion 客户端。 
2. 获取 Notion 页面内容
- 
    通过 get_page_content 函数获取指定页面的内容。 
- 
    递归处理子页面。 
- 
    使用 process_block_content 函数处理不同类型的 Notion 块。 
3. 内容处理
- 
    将 Notion 块转换为 Markdown 格式。 
- 
    支持处理以下块类型: 
4. 保存为 Markdown 文件
- 
    使用 save_to_markdown 函数将处理后的内容保存到 _posts 目录。 
- 
    文件名格式为 YYYY-MM-DD-title.md。 
- 
    自动生成文章元数据,包括标题、日期和布局。 
5. 图片处理
- 
    通过 Unsplash API 获取与文章标题相关的图片作为横幅。 
- 
    支持处理 Notion 中的图片块,直接使用图片 URL。 
关键代码
初始化 Notion 客户端
NOTION_API_TOKEN = os.getenv("NOTION_API_TOKEN")
notion = Client(auth=NOTION_API_TOKEN)
获取页面内容
def get_page_content(page_id):
    page = notion.pages.retrieve(page_id=page_id)
    title = page['properties']['title']['title'][0]['plain_text']
    blocks = notion.blocks.children.list(block_id=page_id)
    # 处理块内容
处理块内容
def process_block_content(block):
    block_type = block['type']
    if block_type == 'paragraph':
        text = block[block_type]['rich_text']
        return ''.join([t['plain_text'] for t in text])
    # 处理其他块类型
保存为 Markdown
def save_to_markdown(title, content):
    date = datetime.now().strftime("%Y-%m-%d")
    filename = f"_posts/{date}-{title.replace(' ', '-').lower()}.md"
    with open(filename, "w", encoding="utf-8") as f:
        f.write("---\n")
        f.write(f"title: {title}\n")
        f.write(f"date: {date}\n")
        f.write("layout: post\n")
        f.write("---\n\n")
        f.write("\n\n".join(content))
使用说明
- 
    配置 Notion API Token 和环境变量。 
- 
    运行脚本:python3 scripts/sync_notion_to_jekyll.py。 
- 
    脚本会自动将 Notion 内容同步到 Jekyll 博客的 _posts 目录。 
注意事项
- 
    确保 Notion 页面已与集成共享。 
- 
    根据需要调整 Markdown 文件的元数据和格式。 
 Walden
  Walden