Skip to content
Snippets Groups Projects
Commit 5b07c8ad authored by Yi Ding's avatar Yi Ding
Browse files

beefed up notion example

parent ca7e61c7
Branches
Tags
No related merge requests found
......@@ -3,13 +3,60 @@ import { program } from "commander";
import { NotionReader } from "llamaindex";
program
.argument("<page>", "Notion page id (must be provided)")
.action(async (page) => {
.argument("[page]", "Notion page id (must be provided)")
.action(async (page, _options, command) => {
// Initializing a client
if (!process.env.NOTION_TOKEN) {
console.log(
"No NOTION_TOKEN found in environment variables. You will need to register an integration https://www.notion.com/my-integrations and put it in your NOTION_TOKEN environment variable.",
);
return;
}
const notion = new Client({
auth: process.env.NOTION_TOKEN,
});
if (!page) {
const response = await notion.search({
filter: {
value: "page",
property: "object",
},
sort: {
direction: "descending",
timestamp: "last_edited_time",
},
});
const { results } = response;
if (results.length === 0) {
console.log(
"No pages found. You will need to share it with your integration. (tap the three dots on the top right, find Add connections, and add your integration)",
);
return;
} else {
const pages = results
.map((result) => {
if (!("url" in result)) {
return null;
}
return {
id: result.id,
url: result.url,
};
})
.filter((page) => page !== null);
console.log("Found pages:");
console.table(pages);
console.log(`To run, run ts-node ${command.name()} [page id]`);
return;
}
}
const reader = new NotionReader({ client: notion });
const documents = await reader.loadData(page);
console.log(documents);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment