-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotionCMS.js
184 lines (169 loc) · 6.01 KB
/
NotionCMS.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const cheerio = require("cheerio");
const NotionAPI = require("./NotionAPI.js");
const { RichTextList } = require("./RichText.js");
const { DatabaseProperties, PageProperties } = require("./Property");
const mentionResolver = require("./MentionResolver");
class NotionCMS {
constructor(token, mentionResolverCallback) {
this.notionApi = new NotionAPI(token);
mentionResolver.setResolver(mentionResolverCallback);
}
async getContentOfDatabase(databaseId, filter, sorts) {
const databaseObj = await this.notionApi.getDatabase(databaseId);
// TODO handle has_more
const pageListObj = await this.notionApi.queryDatabase(
databaseId,
filter,
sorts
);
const pages = await Promise.all(
pageListObj.results.map(async (pageObj) => {
return {
id: pageObj.id,
object: "page",
created_time: new Date(pageObj.created_time),
last_edited_time: new Date(pageObj.last_edited_time),
archived: pageObj.archived,
url: pageObj.url,
properties: new PageProperties(pageObj.properties).get(),
body: await this._convertPageToHTML(pageObj.id),
};
})
);
return {
id: databaseObj.id,
object: "database",
title: new RichTextList(databaseObj.title).toPlainText(),
created_time: new Date(databaseObj.created_time),
last_edited_time: new Date(databaseObj.last_edited_time),
pages: pages,
};
}
async getContentOfPage(pageId) {
const pageObj = await this.notionApi.getPage(pageId);
return {
id: pageObj.id,
object: "page",
created_time: new Date(pageObj.created_time),
last_edited_time: new Date(pageObj.last_edited_time),
archived: pageObj.archived,
url: pageObj.url,
properties: new PageProperties(pageObj.properties).get(),
body: await this._convertPageToHTML(pageObj.id),
};
}
async _convertPageToHTML(id) {
const dom = cheerio.load("<div></div>", null, false);
await this._convertBlockToHTML(id, dom("div"));
return dom("div").html();
}
async _convertBlockToHTML(id, node) {
// TODO handle has_more
const blockChildren = await this.notionApi.getBlockChildren(id, {});
for (const [i, block] of blockChildren.results.entries()) {
let childNode = node;
switch (block.type) {
case "paragraph":
if (block.has_children) {
childNode.append('<div class="pl-8"></div>');
childNode = childNode.children().last();
}
childNode.append("<p></p>");
childNode = childNode.children().last();
childNode.append(new RichTextList(block.paragraph.text).toHTML());
if (block.has_children) {
childNode = childNode.parent();
childNode.append('<div class="pl-8"></div>');
childNode = childNode.children().last();
}
break;
case "heading_1":
childNode.append("<h1></h1>");
childNode = childNode.children().last();
childNode.append(new RichTextList(block.heading_1.text).toHTML());
break;
case "heading_2":
childNode.append("<h2></h2>");
childNode = childNode.children().last();
childNode.append(new RichTextList(block.heading_2.text).toHTML());
break;
case "heading_3":
childNode.append("<h3></h3>");
childNode = childNode.children().last();
childNode.append(new RichTextList(block.heading_3.text).toHTML());
break;
case "bulleted_list_item":
if (
i == 0 ||
blockChildren.results[i - 1].type != "bulleted_list_item"
) {
childNode.append("<ul></ul>");
childNode = childNode.children().last();
} else {
childNode = childNode.children().last();
}
childNode.append("<li></li>");
childNode = childNode.children().last();
childNode.append(
new RichTextList(block.bulleted_list_item.text).toHTML()
);
break;
case "numbered_list_item":
if (
i == 0 ||
blockChildren.results[i - 1].type != "numbered_list_item"
) {
childNode.append("<ol></ol>");
childNode = childNode.children().last();
} else {
childNode = childNode.children().last();
}
childNode.append("<li></li>");
childNode = childNode.children().last();
childNode.append(
new RichTextList(block.numbered_list_item.text).toHTML()
);
break;
case "to_do":
if (block.has_children) {
childNode.append('<div class="pl-8"></div>');
childNode = childNode.children().last();
}
// TODO: create random id to bind label to input
childNode.append(
`<div><input type="checkbox" disabled ${
block.to_do.checked ? "checked" : ""
}><label></label></div>`
);
childNode = childNode.children().last();
childNode = childNode.children().last();
childNode.append(new RichTextList(block.to_do.text).toHTML());
if (block.has_children) {
childNode = childNode.parent();
childNode.append('<div class="pl-8"></div>');
childNode = childNode.children().last();
}
break;
case "toggle":
childNode.append("<details><summary></summary></details>");
childNode = childNode.children().last();
childNode = childNode.children().last();
childNode.append(new RichTextList(block.toggle.text).toHTML());
if (block.has_children) {
childNode = childNode.parent();
}
break;
case "child_page":
break;
case "unsupported":
break;
default:
break;
}
if (block.has_children) {
await this._convertBlockToHTML(block.id, childNode);
}
}
}
}
module.exports = NotionCMS;