/
notion-to-md.ts
339 lines (296 loc) · 9.69 KB
/
notion-to-md.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { Client } from "@notionhq/client";
import {
Annotations,
ListBlockChildrenResponseResult,
ListBlockChildrenResponseResults,
MdBlock,
Text,
NotionToMarkdownOptions,
} from "./types";
import * as md from "./utils/md";
import { getBlockChildren } from "./utils/notion";
/**
* Converts a Notion page to Markdown.
*/
export class NotionToMarkdown {
private notionClient: Client;
constructor(options: NotionToMarkdownOptions) {
this.notionClient = options.notionClient;
}
/**
* Converts Markdown Blocks to string
* @param {MdBlock[]} mdBlocks - Array of markdown blocks
* @param {number} nestingLevel - Defines max depth of nesting
* @returns {string} - Returns markdown string
*/
toMarkdownString(mdBlocks: MdBlock[] = [], nestingLevel: number = 0): string {
let mdString = "";
mdBlocks.forEach((mdBlocks) => {
if (mdBlocks.parent) {
mdString += `
${md.addTabSpace(mdBlocks.parent, nestingLevel)}
`;
}
if (mdBlocks.children && mdBlocks.children.length > 0) {
mdString += this.toMarkdownString(mdBlocks.children, nestingLevel + 1);
}
});
return mdString;
}
/**
* Retrieves Notion Blocks based on ID and converts them to Markdown Blocks
* @param {string} id - notion page id (not database id)
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100 (Default=null)
* @returns {Promise<MdBlock[]>} - List of markdown blocks
*/
async pageToMarkdown(
id: string,
totalPage: number | null = null
): Promise<MdBlock[]> {
if (!this.notionClient) {
throw new Error(
"notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md"
);
}
const blocks = await getBlockChildren(this.notionClient, id, totalPage);
const parsedData = await this.blocksToMarkdown(blocks);
return parsedData;
}
/**
* Converts list of Notion Blocks to Markdown Blocks
* @param {ListBlockChildrenResponseResults | undefined} blocks - List of notion blocks
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100
* @param {MdBlock[]} mdBlocks - Defines max depth of nesting
* @returns {Promise<MdBlock[]>} - Array of markdown blocks with their children
*/
async blocksToMarkdown(
blocks?: ListBlockChildrenResponseResults,
totalPage: number | null = null,
mdBlocks: MdBlock[] = []
): Promise<MdBlock[]> {
if (!this.notionClient) {
throw new Error(
"notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md"
);
}
if (!blocks) return mdBlocks;
for (let i = 0; i < blocks.length; i++) {
let block = blocks[i];
if ("has_children" in block && block.has_children) {
let child_blocks = await getBlockChildren(
this.notionClient,
block.id,
totalPage
);
mdBlocks.push({
parent: await this.blockToMarkdown(block),
children: [],
});
let l = mdBlocks.length;
await this.blocksToMarkdown(
child_blocks,
totalPage,
mdBlocks[l - 1].children
);
continue;
}
let tmp = await this.blockToMarkdown(block);
mdBlocks.push({ parent: tmp, children: [] });
}
return mdBlocks;
}
/**
* Converts a Notion Block to a Markdown Block
* @param {ListBlockChildrenResponseResult} block - single notion block
* @returns {string} corresponding markdown string of the passed block
*/
async blockToMarkdown(block: ListBlockChildrenResponseResult) {
if (!("type" in block)) return "";
let parsedData = "";
const { type } = block;
// console.log({ block });
switch (type) {
case "image":
{
let blockContent = block.image;
const image_caption_plain = blockContent.caption
.map((item) => item.plain_text)
.join("");
const image_type = blockContent.type;
if (image_type === "external")
return md.image(image_caption_plain, blockContent.external.url);
if (image_type === "file")
return md.image(image_caption_plain, blockContent.file.url);
}
break;
case "divider": {
return md.divider();
}
case "equation": {
return md.codeBlock(block.equation.expression);
}
case "video":
case "file":
case "pdf":
{
let blockContent;
if (type === "video") blockContent = block.video;
if (type === "file") blockContent = block.file;
if (type === "pdf") blockContent = block.pdf;
if (blockContent) {
const file_type = blockContent.type;
if (file_type === "external")
return md.link("image", blockContent.external.url);
if (file_type === "file")
return md.link("image", blockContent.file.url);
}
}
break;
case "bookmark":
case "embed":
case "link_preview":
{
let blockContent;
if (type === "bookmark") blockContent = block.bookmark;
if (type === "embed") blockContent = block.embed;
if (type === "link_preview") blockContent = block.link_preview;
if (blockContent) return md.link(type, blockContent.url);
}
break;
case "table": {
const { id, has_children } = block;
let tableArr: string[][] = [];
if (has_children) {
const tableRows = await getBlockChildren(this.notionClient, id, 100);
// console.log(">>", tableRows);
let rowsPromise = tableRows?.map(async (row) => {
const { type } = row as any;
const cells = (row as any)[type]["cells"];
/**
* this is more like a hack since matching the type text was
* difficult. So converting each cell to paragraph type to
* reuse the blockToMarkdown function
*/
let cellStringPromise = cells.map(
async (cell: any) =>
await this.blockToMarkdown({
type: "paragraph",
paragraph: { rich_text: cell },
} as ListBlockChildrenResponseResult)
);
const cellStringArr = await Promise.all(cellStringPromise);
// console.log("~~", cellStringArr);
tableArr.push(cellStringArr);
// console.log(tableArr);
});
await Promise.all(rowsPromise || []);
}
parsedData = md.table(tableArr);
return parsedData;
}
// Rest of the types
// "paragraph"
// "heading_1"
// "heading_2"
// "heading_3"
// "bulleted_list_item"
// "numbered_list_item"
// "quote"
// "to_do"
// "toggle"
// "template"
// "synced_block"
// "child_page"
// "child_database"
// "code"
// "callout"
// "breadcrumb"
// "table_of_contents"
// "column_list"
// "column"
// "link_to_page"
// "audio"
// "unsupported"
default: {
// In this case typescript is not able to index the types properly, hence ignoring the error
// @ts-ignore
let blockContent = block[type].text || block[type].rich_text || [];
blockContent.map((content: Text) => {
const annotations = content.annotations;
let plain_text = content.plain_text;
plain_text = this.annotatePlainText(plain_text, annotations);
if (content["href"])
plain_text = md.link(plain_text, content["href"]);
parsedData += plain_text;
});
}
}
switch (type) {
case "code":
{
parsedData = md.codeBlock(parsedData, block[type].language);
}
break;
case "heading_1":
{
parsedData = md.heading1(parsedData);
}
break;
case "heading_2":
{
parsedData = md.heading2(parsedData);
}
break;
case "heading_3":
{
parsedData = md.heading3(parsedData);
}
break;
case "quote":
{
parsedData = md.quote(parsedData);
}
break;
case "callout":
{
parsedData = md.callout(parsedData, block[type].icon);
}
break;
case "bulleted_list_item":
case "numbered_list_item":
{
parsedData = md.bullet(parsedData);
}
break;
case "to_do":
{
parsedData = md.todo(parsedData, block.to_do.checked);
}
break;
}
return parsedData;
}
/**
* Annoate text using provided annotations
* @param {string} text - String to be annotated
* @param {Annotations} annotations - Annotation object of a notion block
* @returns {string} - Annotated text
*/
annotatePlainText(text: string, annotations: Annotations): string {
// if text is all spaces, don't annotate
if (text.match(/^\s*$/)) return text;
const leadingSpaceMatch = text.match(/^(\s*)/);
const trailingSpaceMatch = text.match(/(\s*)$/);
const leading_space = leadingSpaceMatch ? leadingSpaceMatch[0] : "";
const trailing_space = trailingSpaceMatch ? trailingSpaceMatch[0] : "";
text = text.trim();
if (text !== "") {
if (annotations.code) text = md.inlineCode(text);
if (annotations.bold) text = md.bold(text);
if (annotations.italic) text = md.italic(text);
if (annotations.strikethrough) text = md.strikethrough(text);
if (annotations.underline) text = md.underline(text);
}
return leading_space + text + trailing_space;
}
}