Notion-to-MD 项目技术文档
1. 安装指南
1.1 安装方式
要安装 notion-to-md
包,请在终端中运行以下命令:
npm install notion-to-md
1.2 依赖项
在安装 notion-to-md
之前,请确保已经安装了 @notionhq/client
包。如果没有安装,请运行以下命令:
npm install @notionhq/client
2. 项目使用说明
2.1 基本使用
以下是一个基本的使用示例,展示了如何将 Notion 页面转换为 Markdown 格式:
const { Client } = require("@notionhq/client");
const { NotionToMarkdown } = require("notion-to-md");
const notion = new Client({
auth: "your integration token",
});
const n2m = new NotionToMarkdown({ notionClient: notion });
(async () => {
const mdblocks = await n2m.pageToMarkdown("target_page_id");
const mdString = n2m.toMarkdownString(mdblocks);
console.log(mdString.parent);
})();
2.2 处理子页面内容
从 v2.7.0 开始,toMarkdownString
不再自动保存子页面内容。现在它返回一个包含子页面 Markdown 内容的对象。以下是如何处理子页面内容的示例:
const { Client } = require("@notionhq/client");
const { NotionToMarkdown } = require("notion-to-md");
const notion = new Client({
auth: "your integration token",
});
const n2m = new NotionToMarkdown({
notionClient: notion,
config: {
separateChildPage: true, // 默认: false
}
});
(async () => {
const mdblocks = await n2m.pageToMarkdown("target_page_id");
const mdString = n2m.toMarkdownString(mdblocks);
console.log(mdString);
})();
2.3 禁用子页面解析
如果不想解析子页面,可以在配置中禁用子页面解析:
const n2m = new NotionToMarkdown({
notionClient: notion,
config: {
parseChildPages: false, // 默认: true
}
});
3. 项目API使用文档
3.1 pageToMarkdown
将 Notion 页面转换为 Markdown 对象。
const mdblocks = await n2m.pageToMarkdown("target_page_id");
3.2 toMarkdownString
将 Markdown 对象转换为 Markdown 字符串。
const mdString = n2m.toMarkdownString(mdblocks);
3.3 blocksToMarkdown
将 Notion 块列表转换为 Markdown 对象。
const { results } = await notion.blocks.children.list({ block_id });
const mdblocks = await n2m.blocksToMarkdown(results);
3.4 blockToMarkdown
将单个 Notion 块转换为 Markdown 字符串。
const result = n2m.blockToMarkdown(block);
3.5 setCustomTransformer
设置自定义转换器,用于处理特定类型的 Notion 块。
n2m.setCustomTransformer("embed", async (block) => {
const { embed } = block;
if (!embed?.url) return "";
return `<figure>
<iframe src="${embed?.url}"></iframe>
<figcaption>${await n2m.blockToMarkdown(embed?.caption)}</figcaption>
</figure>`;
});
4. 项目安装方式
通过 npm 安装 notion-to-md
包:
npm install notion-to-md
确保在安装 notion-to-md
之前,已经安装了 @notionhq/client
包:
npm install @notionhq/client
通过以上步骤,您可以顺利安装并使用 notion-to-md
包,将 Notion 页面转换为 Markdown 格式。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考