forked from eri24816/Notion-Hugo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
118 lines (106 loc) · 3.89 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Client, isFullPage, iteratePaginatedAPI } from "@notionhq/client";
import dotenv from "dotenv";
import fs from "fs-extra";
import { savePage } from "./render";
import { loadConfig } from "./config";
import { getAllContentFiles } from "./file";
import { isFullPageOrDatabase } from "@notionhq/client/build/src/helpers";
import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints";
import { getFileName, getPageTitle } from "./helpers";
dotenv.config();
async function main() {
if (process.env.NOTION_TOKEN === "")
throw Error("The NOTION_TOKEN environment vairable is not set.");
const config = await loadConfig();
console.info("[Info] Config loaded ");
const notion = new Client({
auth: process.env.NOTION_TOKEN,
});
const page_ids: string[] = [];
console.info("[Info] Start processing mounted databases");
// process mounted databases
for (const mount of config.mount.databases) {
fs.ensureDirSync(`content/${mount.target_folder}`);
for await (const page of iteratePaginatedAPI(notion.databases.query, {
database_id: mount.database_id,
})) {
if (!isFullPageOrDatabase(page) || page.object !== "page") {
continue;
}
// Skip if the page has a parent page because it will be processed recursively in the parent page
if (await hasParentPage(page, notion)) {
continue;
}
console.info(`[Info] Start processing page ${page.id}`);
page_ids.push(page.id);
page_ids.push(...await savePageRecursive(page, notion, mount.target_folder));
}
}
// process mounted pages
for (const mount of config.mount.pages) {
const page = await notion.pages.retrieve({ page_id: mount.page_id });
if (!isFullPage(page)) continue;
page_ids.push(...await savePageRecursive(page, notion, mount.target_folder));
}
// remove posts that exist locally but not in Notion Database
const contentFiles = getAllContentFiles("content");
for (const file of contentFiles) {
if (!page_ids.includes(file.metadata.id)) {
fs.removeSync(file.filepath);
}
}
}
async function hasParentPage(page: PageObjectResponse, notion: Client) {
const parentProperty = page.properties['Parent item'];
if (parentProperty && parentProperty.type === 'relation' && parentProperty.relation.length > 0) {
return true;
}
return false;
}
async function getSubpages(page: PageObjectResponse, notion: Client) {
const subitemProperty = page.properties['Sub-item'];
if (subitemProperty && subitemProperty.type === 'relation') {
const subpages = [];
for (const item of subitemProperty.relation) {
const subpage = await notion.pages.retrieve({ page_id: item.id });
if (isFullPage(subpage)) {
subpages.push(subpage);
}
}
return subpages;
}
return [];
}
/**
* If the page has a subitems property, save the page and recursively save all subpages
* @returns the list of page ids that are saved
*/
async function savePageRecursive(
page: PageObjectResponse,
notion: Client,
targetFolder: string
): Promise<string[]> {
// Get all subpages of the page
const subpages = await getSubpages(page, notion);
if (subpages.length === 0){
// The page has no subpages, save the page into title.md
await savePage(page, notion, targetFolder);
return [page.id];
}
// The page has subpages, save the page into title/_index.md and save all subpages in title/
// The folder name must be generated by getFileName so links to the folder work correctly
const folder = `${targetFolder}/${getFileName(getPageTitle(page), page.id, "")}`;
fs.ensureDirSync(`content/${folder}`);
const pageIds = [page.id];
await savePage(page, notion, folder, "_index.md");
for (const subpage of subpages) {
pageIds.push(...await savePageRecursive(subpage, notion, folder));
}
return pageIds;
}
main()
.then(() => process.exit(0))
.catch((err) => {
console.error(err);
process.exit(1);
});