From 0f6a685a138eda8972d58a36cd6c33701bfcb6b7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 16 Mar 2026 14:30:55 +0000 Subject: [PATCH] feat: enhance notion-page-reader with file save capability for reading Notion instruction pages Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com> --- .github/workflows/notion-page-reader.yml | 26 +++++++++++++++++++++--- data/notion-pages/.gitkeep | 0 scripts/notion-page-reader.js | 23 +++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 data/notion-pages/.gitkeep diff --git a/.github/workflows/notion-page-reader.yml b/.github/workflows/notion-page-reader.yml index fd1d686e..200c6e2b 100644 --- a/.github/workflows/notion-page-reader.yml +++ b/.github/workflows/notion-page-reader.yml @@ -1,12 +1,14 @@ name: 铸渊 · Notion 页面阅读器 # 从 Notion 页面链接读取内容,输出 Markdown 格式文本 +# 支持保存到文件并提交到仓库 # # 依赖 Secrets: # NOTION_API_TOKEN Notion API token # # dispatch payload / inputs: # notion_page_url Notion 页面链接或页面 ID +# save_to_repo 是否保存到仓库(true/false,默认 false) on: repository_dispatch: @@ -16,13 +18,17 @@ on: notion_page_url: description: 'Notion 页面链接或页面 ID' required: true + save_to_repo: + description: '保存到仓库 data/notion-pages/ 目录(true/false)' + required: false + default: 'false' jobs: read-page: name: 📖 读取 Notion 页面 runs-on: ubuntu-latest permissions: - contents: read + contents: write steps: - name: Checkout @@ -35,6 +41,20 @@ jobs: - name: 📖 读取 Notion 页面内容 env: - NOTION_TOKEN: ${{ secrets.NOTION_API_TOKEN }} - NOTION_PAGE_URL: ${{ github.event.client_payload.notion_page_url || github.event.inputs.notion_page_url }} + NOTION_TOKEN: ${{ secrets.NOTION_API_TOKEN }} + NOTION_PAGE_URL: ${{ github.event.client_payload.notion_page_url || github.event.inputs.notion_page_url }} + NOTION_OUTPUT_DIR: ${{ (github.event.client_payload.save_to_repo == 'true' || github.event.inputs.save_to_repo == 'true') && 'data/notion-pages' || '' }} run: node scripts/notion-page-reader.js + + - name: 💾 提交保存的页面内容 + if: github.event.client_payload.save_to_repo == 'true' || github.event.inputs.save_to_repo == 'true' + run: | + git config user.name "zhuyuan-bot" + git config user.email "zhuyuan-bot@guanghulab.com" + git add data/notion-pages/ + if git diff --cached --quiet; then + echo "没有新文件需要提交" + else + git commit -m "📖 铸渊 · 保存 Notion 页面内容 [skip ci]" + git push + fi diff --git a/data/notion-pages/.gitkeep b/data/notion-pages/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/scripts/notion-page-reader.js b/scripts/notion-page-reader.js index 878bdd6f..4a188527 100644 --- a/scripts/notion-page-reader.js +++ b/scripts/notion-page-reader.js @@ -19,6 +19,8 @@ 'use strict'; const https = require('https'); +const fs = require('fs'); +const path = require('path'); // ══════════════════════════════════════════════════════════ // 常量 @@ -236,6 +238,7 @@ function blocksToMarkdown(blocks) { async function main() { var input = process.env.NOTION_PAGE_URL || process.argv[2]; var token = process.env.NOTION_TOKEN; + var outputDir = process.env.NOTION_OUTPUT_DIR || ''; if (!token) { console.error('❌ 缺少 NOTION_TOKEN 环境变量'); @@ -281,6 +284,26 @@ async function main() { console.log(''); console.log('════════════════════════════════════════'); console.log('✅ 读取完成 · 共 ' + blocks.length + ' 个内容块'); + + // 5. 可选:保存到文件 + if (outputDir) { + var safeTitle = title.replace(/[^a-zA-Z0-9\u4e00-\u9fa5_-]/g, '_').slice(0, 80); + var filename = safeTitle + '-' + pageId.slice(0, 8) + '.md'; + var outputPath = path.join(outputDir, filename); + + var fileContent = '# ' + title + '\n\n'; + fileContent += '> 📖 Notion 页面 ID: `' + pageId + '`\n'; + fileContent += '> 🕐 读取时间: ' + new Date().toISOString() + '\n\n'; + fileContent += '---\n\n'; + fileContent += markdown + '\n'; + + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, { recursive: true }); + } + fs.writeFileSync(outputPath, fileContent, 'utf-8'); + console.log(''); + console.log('💾 已保存到: ' + outputPath); + } } // ══════════════════════════════════════════════════════════