feat: enhance notion-page-reader with file save capability for reading Notion instruction pages
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
This commit is contained in:
parent
84c8131e3a
commit
0f6a685a13
|
|
@ -1,12 +1,14 @@
|
||||||
name: 铸渊 · Notion 页面阅读器
|
name: 铸渊 · Notion 页面阅读器
|
||||||
|
|
||||||
# 从 Notion 页面链接读取内容,输出 Markdown 格式文本
|
# 从 Notion 页面链接读取内容,输出 Markdown 格式文本
|
||||||
|
# 支持保存到文件并提交到仓库
|
||||||
#
|
#
|
||||||
# 依赖 Secrets:
|
# 依赖 Secrets:
|
||||||
# NOTION_API_TOKEN Notion API token
|
# NOTION_API_TOKEN Notion API token
|
||||||
#
|
#
|
||||||
# dispatch payload / inputs:
|
# dispatch payload / inputs:
|
||||||
# notion_page_url Notion 页面链接或页面 ID
|
# notion_page_url Notion 页面链接或页面 ID
|
||||||
|
# save_to_repo 是否保存到仓库(true/false,默认 false)
|
||||||
|
|
||||||
on:
|
on:
|
||||||
repository_dispatch:
|
repository_dispatch:
|
||||||
|
|
@ -16,13 +18,17 @@ on:
|
||||||
notion_page_url:
|
notion_page_url:
|
||||||
description: 'Notion 页面链接或页面 ID'
|
description: 'Notion 页面链接或页面 ID'
|
||||||
required: true
|
required: true
|
||||||
|
save_to_repo:
|
||||||
|
description: '保存到仓库 data/notion-pages/ 目录(true/false)'
|
||||||
|
required: false
|
||||||
|
default: 'false'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
read-page:
|
read-page:
|
||||||
name: 📖 读取 Notion 页面
|
name: 📖 读取 Notion 页面
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
permissions:
|
||||||
contents: read
|
contents: write
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
|
|
@ -35,6 +41,20 @@ jobs:
|
||||||
|
|
||||||
- name: 📖 读取 Notion 页面内容
|
- name: 📖 读取 Notion 页面内容
|
||||||
env:
|
env:
|
||||||
NOTION_TOKEN: ${{ secrets.NOTION_API_TOKEN }}
|
NOTION_TOKEN: ${{ secrets.NOTION_API_TOKEN }}
|
||||||
NOTION_PAGE_URL: ${{ github.event.client_payload.notion_page_url || github.event.inputs.notion_page_url }}
|
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
|
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
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,8 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const https = require('https');
|
const https = require('https');
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
// ══════════════════════════════════════════════════════════
|
// ══════════════════════════════════════════════════════════
|
||||||
// 常量
|
// 常量
|
||||||
|
|
@ -236,6 +238,7 @@ function blocksToMarkdown(blocks) {
|
||||||
async function main() {
|
async function main() {
|
||||||
var input = process.env.NOTION_PAGE_URL || process.argv[2];
|
var input = process.env.NOTION_PAGE_URL || process.argv[2];
|
||||||
var token = process.env.NOTION_TOKEN;
|
var token = process.env.NOTION_TOKEN;
|
||||||
|
var outputDir = process.env.NOTION_OUTPUT_DIR || '';
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
console.error('❌ 缺少 NOTION_TOKEN 环境变量');
|
console.error('❌ 缺少 NOTION_TOKEN 环境变量');
|
||||||
|
|
@ -281,6 +284,26 @@ async function main() {
|
||||||
console.log('');
|
console.log('');
|
||||||
console.log('════════════════════════════════════════');
|
console.log('════════════════════════════════════════');
|
||||||
console.log('✅ 读取完成 · 共 ' + blocks.length + ' 个内容块');
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ══════════════════════════════════════════════════════════
|
// ══════════════════════════════════════════════════════════
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue