/
NotionParser.ts
192 lines (175 loc) · 4.58 KB
/
NotionParser.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
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
185
186
187
188
189
190
191
192
import {
NotionPageBlock,
NotionImageNodes,
NotionPageText,
} from "gatsby-source-notionso/src/types/notion";
type NotionBlockTypes =
| "meta"
| "page"
| "text"
| "quote"
| "image"
| "bookmark"
| "code"
| "text"
| "bulleted_list"
| "header"
| "sub_header"
| "sub_sub_header";
export const blocksToHtml = (
blocks: NotionPageBlock[],
imageNodes: NotionImageNodes[]
): string => {
let result = "";
for (const block of blocks) {
const converter = Parser[block.type as NotionBlockTypes];
if (converter) {
result += converter(block, imageNodes);
}
}
return result;
};
const notionPageText = (propValue: NotionPageText) => {
const span = document.createElement("span");
let currentParent = span;
for (const att of propValue.atts) {
const attElm = document.createElement(att.att === "c" ? "code" : att.att);
if (att.att === "a" && att.value) {
attElm.setAttribute("href", att.value);
}
currentParent.appendChild(attElm);
currentParent = attElm;
}
currentParent.textContent = propValue.text;
return span;
};
const text = (block: NotionPageBlock) => {
const p = document.createElement("p");
for (const prop of block.properties) {
if (prop.propName === "title") {
for (const propValue of prop.value) {
const elm = notionPageText(propValue);
p.appendChild(elm);
}
}
}
console.log("p", p);
return p.outerHTML;
};
const image = (block: NotionPageBlock, imageNodes: NotionImageNodes[]) => {
const img = document.createElement("img");
for (const prop of block.properties) {
if (prop.propName === "source") {
const [{ text }] = prop.value;
img.src = getPublicImageURL(text, imageNodes);
}
}
return img.outerHTML;
};
const bookmark = (block: NotionPageBlock) => {
const p = document.createElement("p");
const a = document.createElement("a");
p.appendChild(a);
for (const prop of block.properties) {
const [{ text }] = prop.value;
if (prop.propName === "link") {
a.href = text;
} else if (prop.propName === "title") {
a.textContent = text;
}
}
return p.outerHTML;
};
const getPublicImageURL = (src: string, imageNodes: NotionImageNodes[]) => {
const node = imageNodes.find((n) => n.imageUrl === src);
return node ? node.localFile.publicURL : "";
};
const code = (block: NotionPageBlock) => {
const pre = document.createElement("pre");
const code = document.createElement("code");
pre.appendChild(code);
for (const prop of block.properties) {
if (prop.propName === "title") {
const [{ text }] = prop.value;
code.textContent = text;
}
}
return pre.outerHTML;
};
const bulleted_list = (block: NotionPageBlock) => {
const ul = document.createElement("ul");
const li = document.createElement("li");
ul.appendChild(li);
for (const prop of block.properties) {
if (prop.propName === "title") {
for (const propValue of prop.value) {
const elm = notionPageText(propValue);
li.appendChild(elm);
}
}
}
return ul.outerHTML;
};
const quote = (block: NotionPageBlock) => {
const blockquote = document.createElement("blockquote");
const p = document.createElement("p");
blockquote.appendChild(p);
for (const prop of block.properties) {
if (prop.propName === "title") {
for (const propValue of prop.value) {
const elm = notionPageText(propValue);
p.appendChild(elm);
}
}
}
return blockquote.outerHTML;
};
const header = (block: NotionPageBlock) => {
const h1 = document.createElement("h1");
for (const prop of block.properties) {
if (prop.propName === "title") {
for (const propValue of prop.value) {
const elm = notionPageText(propValue);
h1.appendChild(elm);
}
}
}
return h1.outerHTML;
};
const sub_header = (block: NotionPageBlock) => {
const h2 = document.createElement("h2");
for (const prop of block.properties) {
if (prop.propName === "title") {
for (const propValue of prop.value) {
const elm = notionPageText(propValue);
h2.appendChild(elm);
}
}
}
return h2.outerHTML;
};
const sub_sub_header = (block: NotionPageBlock) => {
const h3 = document.createElement("h3");
for (const prop of block.properties) {
if (prop.propName === "title") {
for (const propValue of prop.value) {
const elm = notionPageText(propValue);
h3.appendChild(elm);
}
}
}
return h3.outerHTML;
};
const Parser = {
meta: null,
page: null,
text,
image,
bookmark,
quote,
code,
bulleted_list,
header,
sub_header,
sub_sub_header,
};