之前用过很多的 blog 程序,比如 hexo,astro,nextjs 等,这些程序发布完成了,下次写blog就忘记了,一般在项目中写 markdown 文件,体验不好,自己以后总结和查看都不方便,一直想找一个编辑体验如 Notion Obsidian 的笔记软件,展示可以自定义样式,现在用 Vitepress + Notion ,终于实现了这个功能,Notion 作为 markdown 编辑器 和 blog CMS,Vitepress 作为博客渲染器,完美结合。
pnpm install
# 或
npm install复制 .env.example 文件为 .env,并填入你的 Notion API 配置:
cp .env.example .env编辑 .env 文件:
NOTION_TOKEN=ntn_xxxxxxxxxxxx
DATABASE_ID=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
API_HOST=https://api.notion.com/v1- 访问 Notion Developers
- 点击 "New integration" 创建一个新的集成
- 复制 "Internal Integration Token"(格式:
ntn_...) - 将其填入
.env文件的NOTION_TOKEN
- 在 Notion 中打开你的博客数据库
- 从 URL 中复制 Database ID(32位字符)
- 将其填入
.env文件的DATABASE_ID
- 永远不要将
.env文件提交到 Git .env文件已经在.gitignore中,不会被提交- 只提交
.env.example作为模板
本项目使用 Notion API 2025-09-03 版本,提供三个公共方法用于与 Notion 交互:
所有 API 方法位于 .vitepress/theme/notionApi.js:
根据 Notion API 2025-09-03 升级,需要先获取 data_source_id:
import { getDataSourceId } from './.vitepress/theme/notionApi.js'
const dataSourceId = await getDataSourceId()
// 返回: data_source_id (如果获取失败则返回 databaseId)使用 data_source_id 查询已发布的文章:
import { queryNotionDatabase } from './.vitepress/theme/notionApi.js'
const results = await queryNotionDatabase(dataSourceId)
// 返回: 文章数组(已过滤"状态=发布"的记录)查询条件:
filter: 状态 = "发布"sorts: Last edited time 降序
自动分页获取页面的所有 blocks:
import { getPageBlocks } from './.vitepress/theme/notionApi.js'
const blocks = await getPageBlocks(pageId)
// 返回: 所有 blocks 数组(自动处理分页)特性:
- ✅ 自动分页:当 blocks 超过 1000 时自动请求下一页
- ✅ 使用
next_cursor实现分页 - ✅ 直到
has_more: false才停止
分页示例:
Fetched 100 blocks, fetching more... (total so far: 100)
Fetched 100 blocks, fetching more... (total so far: 200)
get blocks success for pageid xxx cached 154 blocks
- API 版本:
2025-09-03 - 变更: 使用 data sources 架构替代原有的 database 查询
- 兼容性: 向后兼容,自动降级到 databaseId
pnpm run dev
# 或
npm run devpnpm run build
# 或
npm run build这个仓库是 根据 airene vitepress-blog-pure 改造的,原来是用本地md 文档管理博客,因为 vitpress 有SSG 的功能,改造成用Notion作为编辑端和管理端,vitepress 作为博客的渲染器,这样可以更好的管理博客文章,写博文是编辑体验更好。
本项目采用 MIT License 开源协议。