notion-to-md 2.3.4 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +232 -204
- package/build/index.d.ts +1 -1
- package/build/index.js +17 -17
- package/build/index.js.map +1 -1
- package/build/notion-to-md.d.ts +45 -43
- package/build/notion-to-md.d.ts.map +1 -1
- package/build/notion-to-md.js +366 -328
- package/build/notion-to-md.js.map +1 -1
- package/build/notion-to-md.spec.d.ts +2 -0
- package/build/notion-to-md.spec.d.ts.map +1 -0
- package/build/notion-to-md.spec.js +29 -0
- package/build/notion-to-md.spec.js.map +1 -0
- package/build/types/index.d.ts +53 -47
- package/build/types/index.d.ts.map +1 -1
- package/build/types/index.js +2 -2
- package/build/types/index.js.map +1 -1
- package/build/utils/md.d.ts +20 -19
- package/build/utils/md.d.ts.map +1 -1
- package/build/utils/md.js +107 -96
- package/build/utils/md.js.map +1 -1
- package/build/utils/md.spec.d.ts +1 -1
- package/build/utils/md.spec.js +92 -80
- package/build/utils/md.spec.js.map +1 -1
- package/build/utils/notion.d.ts +4 -5
- package/build/utils/notion.d.ts.map +1 -1
- package/build/utils/notion.js +38 -26
- package/build/utils/notion.js.map +1 -1
- package/package.json +51 -51
package/build/notion-to-md.js
CHANGED
|
@@ -1,329 +1,367 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.NotionToMarkdown = void 0;
|
|
27
|
-
const md = __importStar(require("./utils/md"));
|
|
28
|
-
const notion_1 = require("./utils/notion");
|
|
29
|
-
/**
|
|
30
|
-
* Converts a Notion page to Markdown.
|
|
31
|
-
*/
|
|
32
|
-
class NotionToMarkdown {
|
|
33
|
-
constructor(options) {
|
|
34
|
-
this.notionClient = options.notionClient;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (mdBlocks.
|
|
51
|
-
mdString +=
|
|
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
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.NotionToMarkdown = void 0;
|
|
27
|
+
const md = __importStar(require("./utils/md"));
|
|
28
|
+
const notion_1 = require("./utils/notion");
|
|
29
|
+
/**
|
|
30
|
+
* Converts a Notion page to Markdown.
|
|
31
|
+
*/
|
|
32
|
+
class NotionToMarkdown {
|
|
33
|
+
constructor(options) {
|
|
34
|
+
this.notionClient = options.notionClient;
|
|
35
|
+
this.customTransformers = {};
|
|
36
|
+
}
|
|
37
|
+
setCustomTransformer(type, transformer) {
|
|
38
|
+
this.customTransformers[type] = transformer;
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Converts Markdown Blocks to string
|
|
43
|
+
* @param {MdBlock[]} mdBlocks - Array of markdown blocks
|
|
44
|
+
* @param {number} nestingLevel - Defines max depth of nesting
|
|
45
|
+
* @returns {string} - Returns markdown string
|
|
46
|
+
*/
|
|
47
|
+
toMarkdownString(mdBlocks = [], nestingLevel = 0) {
|
|
48
|
+
let mdString = "";
|
|
49
|
+
mdBlocks.forEach((mdBlocks) => {
|
|
50
|
+
if (mdBlocks.parent) {
|
|
51
|
+
mdString += `
|
|
52
|
+
${md.addTabSpace(mdBlocks.parent, nestingLevel)}
|
|
53
|
+
`;
|
|
54
|
+
}
|
|
55
|
+
if (mdBlocks.children && mdBlocks.children.length > 0) {
|
|
56
|
+
mdString += this.toMarkdownString(mdBlocks.children, nestingLevel + 1);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return mdString;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Retrieves Notion Blocks based on ID and converts them to Markdown Blocks
|
|
63
|
+
* @param {string} id - notion page id (not database id)
|
|
64
|
+
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100 (Default=null)
|
|
65
|
+
* @returns {Promise<MdBlock[]>} - List of markdown blocks
|
|
66
|
+
*/
|
|
67
|
+
async pageToMarkdown(id, totalPage = null) {
|
|
68
|
+
if (!this.notionClient) {
|
|
69
|
+
throw new Error("notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md");
|
|
70
|
+
}
|
|
71
|
+
const blocks = await (0, notion_1.getBlockChildren)(this.notionClient, id, totalPage);
|
|
72
|
+
const parsedData = await this.blocksToMarkdown(blocks);
|
|
73
|
+
return parsedData;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Converts list of Notion Blocks to Markdown Blocks
|
|
77
|
+
* @param {ListBlockChildrenResponseResults | undefined} blocks - List of notion blocks
|
|
78
|
+
* @param {number} totalPage - Retrieve block children request number, page_size Maximum = totalPage * 100
|
|
79
|
+
* @param {MdBlock[]} mdBlocks - Defines max depth of nesting
|
|
80
|
+
* @returns {Promise<MdBlock[]>} - Array of markdown blocks with their children
|
|
81
|
+
*/
|
|
82
|
+
async blocksToMarkdown(blocks, totalPage = null, mdBlocks = []) {
|
|
83
|
+
if (!this.notionClient) {
|
|
84
|
+
throw new Error("notion client is not provided, for more details check out https://github.com/souvikinator/notion-to-md");
|
|
85
|
+
}
|
|
86
|
+
if (!blocks)
|
|
87
|
+
return mdBlocks;
|
|
88
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
89
|
+
let block = blocks[i];
|
|
90
|
+
if ("has_children" in block &&
|
|
91
|
+
block.has_children &&
|
|
92
|
+
block.type !== "column_list" &&
|
|
93
|
+
block.type !== "toggle") {
|
|
94
|
+
let child_blocks = await (0, notion_1.getBlockChildren)(this.notionClient, block.id, totalPage);
|
|
95
|
+
mdBlocks.push({
|
|
96
|
+
parent: await this.blockToMarkdown(block),
|
|
97
|
+
children: [],
|
|
98
|
+
});
|
|
99
|
+
let l = mdBlocks.length;
|
|
100
|
+
await this.blocksToMarkdown(child_blocks, totalPage, mdBlocks[l - 1].children);
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
let tmp = await this.blockToMarkdown(block);
|
|
104
|
+
mdBlocks.push({ parent: tmp, children: [] });
|
|
105
|
+
}
|
|
106
|
+
return mdBlocks;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Converts a Notion Block to a Markdown Block
|
|
110
|
+
* @param {ListBlockChildrenResponseResult} block - single notion block
|
|
111
|
+
* @returns {string} corresponding markdown string of the passed block
|
|
112
|
+
*/
|
|
113
|
+
async blockToMarkdown(block) {
|
|
114
|
+
var _a;
|
|
115
|
+
if (typeof block !== "object" || !("type" in block))
|
|
116
|
+
return "";
|
|
117
|
+
let parsedData = "";
|
|
118
|
+
const { type } = block;
|
|
119
|
+
if (type in this.customTransformers && !!this.customTransformers[type])
|
|
120
|
+
return await this.customTransformers[type](block);
|
|
121
|
+
switch (type) {
|
|
122
|
+
case "image":
|
|
123
|
+
{
|
|
124
|
+
let blockContent = block.image;
|
|
125
|
+
const image_caption_plain = blockContent.caption
|
|
126
|
+
.map((item) => item.plain_text)
|
|
127
|
+
.join("");
|
|
128
|
+
const image_type = blockContent.type;
|
|
129
|
+
if (image_type === "external")
|
|
130
|
+
return md.image(image_caption_plain, blockContent.external.url);
|
|
131
|
+
if (image_type === "file")
|
|
132
|
+
return md.image(image_caption_plain, blockContent.file.url);
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
case "divider": {
|
|
136
|
+
return md.divider();
|
|
137
|
+
}
|
|
138
|
+
case "equation": {
|
|
139
|
+
return md.codeBlock(block.equation.expression);
|
|
140
|
+
}
|
|
141
|
+
case "video":
|
|
142
|
+
case "file":
|
|
143
|
+
case "pdf":
|
|
144
|
+
{
|
|
145
|
+
let blockContent;
|
|
146
|
+
if (type === "video")
|
|
147
|
+
blockContent = block.video;
|
|
148
|
+
if (type === "file")
|
|
149
|
+
blockContent = block.file;
|
|
150
|
+
if (type === "pdf")
|
|
151
|
+
blockContent = block.pdf;
|
|
152
|
+
if (blockContent) {
|
|
153
|
+
const file_type = blockContent.type;
|
|
154
|
+
if (file_type === "external")
|
|
155
|
+
return md.link("image", blockContent.external.url);
|
|
156
|
+
if (file_type === "file")
|
|
157
|
+
return md.link("image", blockContent.file.url);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
break;
|
|
161
|
+
case "bookmark":
|
|
162
|
+
case "embed":
|
|
163
|
+
case "link_preview":
|
|
164
|
+
case "link_to_page":
|
|
165
|
+
case "child_page":
|
|
166
|
+
case "child_database":
|
|
167
|
+
{
|
|
168
|
+
let blockContent;
|
|
169
|
+
let title = type;
|
|
170
|
+
if (type === "bookmark")
|
|
171
|
+
blockContent = block.bookmark;
|
|
172
|
+
if (type === "embed")
|
|
173
|
+
blockContent = block.embed;
|
|
174
|
+
if (type === "link_preview")
|
|
175
|
+
blockContent = block.link_preview;
|
|
176
|
+
if (type === "link_to_page" &&
|
|
177
|
+
block.link_to_page.type === "page_id") {
|
|
178
|
+
blockContent = { url: block.link_to_page.page_id };
|
|
179
|
+
}
|
|
180
|
+
if (type === "child_page") {
|
|
181
|
+
blockContent = { url: block.id };
|
|
182
|
+
title = block.child_page.title;
|
|
183
|
+
}
|
|
184
|
+
if (type === "child_database") {
|
|
185
|
+
blockContent = { url: block.id };
|
|
186
|
+
title = block.child_database.title || "child_database";
|
|
187
|
+
}
|
|
188
|
+
if (blockContent)
|
|
189
|
+
return md.link(title, blockContent.url);
|
|
190
|
+
}
|
|
191
|
+
break;
|
|
192
|
+
case "table": {
|
|
193
|
+
const { id, has_children } = block;
|
|
194
|
+
let tableArr = [];
|
|
195
|
+
if (has_children) {
|
|
196
|
+
const tableRows = await (0, notion_1.getBlockChildren)(this.notionClient, id, 100);
|
|
197
|
+
// console.log(">>", tableRows);
|
|
198
|
+
let rowsPromise = tableRows === null || tableRows === void 0 ? void 0 : tableRows.map(async (row) => {
|
|
199
|
+
const { type } = row;
|
|
200
|
+
const cells = row[type]["cells"];
|
|
201
|
+
/**
|
|
202
|
+
* this is more like a hack since matching the type text was
|
|
203
|
+
* difficult. So converting each cell to paragraph type to
|
|
204
|
+
* reuse the blockToMarkdown function
|
|
205
|
+
*/
|
|
206
|
+
let cellStringPromise = cells.map(async (cell) => await this.blockToMarkdown({
|
|
207
|
+
type: "paragraph",
|
|
208
|
+
paragraph: { rich_text: cell },
|
|
209
|
+
}));
|
|
210
|
+
const cellStringArr = await Promise.all(cellStringPromise);
|
|
211
|
+
// console.log("~~", cellStringArr);
|
|
212
|
+
tableArr.push(cellStringArr);
|
|
213
|
+
// console.log(tableArr);
|
|
214
|
+
});
|
|
215
|
+
await Promise.all(rowsPromise || []);
|
|
216
|
+
}
|
|
217
|
+
return md.table(tableArr);
|
|
218
|
+
}
|
|
219
|
+
case "column_list": {
|
|
220
|
+
const { id, has_children } = block;
|
|
221
|
+
if (!has_children)
|
|
222
|
+
return "";
|
|
223
|
+
const column_list_children = await (0, notion_1.getBlockChildren)(this.notionClient, id, 100);
|
|
224
|
+
let column_list_promise = column_list_children.map(async (column) => await this.blockToMarkdown(column));
|
|
225
|
+
let column_list = await Promise.all(column_list_promise);
|
|
226
|
+
return column_list.join("\n\n");
|
|
227
|
+
}
|
|
228
|
+
case "column": {
|
|
229
|
+
const { id, has_children } = block;
|
|
230
|
+
if (!has_children)
|
|
231
|
+
return "";
|
|
232
|
+
const column_children = await (0, notion_1.getBlockChildren)(this.notionClient, id, 100);
|
|
233
|
+
const column_children_promise = column_children.map(async (column_child) => await this.blockToMarkdown(column_child));
|
|
234
|
+
let column = await Promise.all(column_children_promise);
|
|
235
|
+
return column.join("\n\n");
|
|
236
|
+
}
|
|
237
|
+
case "toggle": {
|
|
238
|
+
const { id, has_children } = block;
|
|
239
|
+
const toggle_summary = (_a = block.toggle.rich_text[0]) === null || _a === void 0 ? void 0 : _a.plain_text;
|
|
240
|
+
// empty toggle
|
|
241
|
+
if (!has_children) {
|
|
242
|
+
return md.toggle(toggle_summary);
|
|
243
|
+
}
|
|
244
|
+
const toggle_children_object = await (0, notion_1.getBlockChildren)(this.notionClient, id, 100);
|
|
245
|
+
// parse children blocks to md object
|
|
246
|
+
const toggle_children = await this.blocksToMarkdown(toggle_children_object);
|
|
247
|
+
// convert children md object to md string
|
|
248
|
+
const toggle_children_md_string = this.toMarkdownString(toggle_children);
|
|
249
|
+
return md.toggle(toggle_summary, toggle_children_md_string);
|
|
250
|
+
}
|
|
251
|
+
// Rest of the types
|
|
252
|
+
// "paragraph"
|
|
253
|
+
// "heading_1"
|
|
254
|
+
// "heading_2"
|
|
255
|
+
// "heading_3"
|
|
256
|
+
// "bulleted_list_item"
|
|
257
|
+
// "numbered_list_item"
|
|
258
|
+
// "quote"
|
|
259
|
+
// "to_do"
|
|
260
|
+
// "template"
|
|
261
|
+
// "synced_block"
|
|
262
|
+
// "child_page"
|
|
263
|
+
// "child_database"
|
|
264
|
+
// "code"
|
|
265
|
+
// "callout"
|
|
266
|
+
// "breadcrumb"
|
|
267
|
+
// "table_of_contents"
|
|
268
|
+
// "column_list"
|
|
269
|
+
// "column"
|
|
270
|
+
// "link_to_page"
|
|
271
|
+
// "audio"
|
|
272
|
+
// "unsupported"
|
|
273
|
+
default: {
|
|
274
|
+
// In this case typescript is not able to index the types properly, hence ignoring the error
|
|
275
|
+
// @ts-ignore
|
|
276
|
+
let blockContent = block[type].text || block[type].rich_text || [];
|
|
277
|
+
blockContent.map((content) => {
|
|
278
|
+
const annotations = content.annotations;
|
|
279
|
+
let plain_text = content.plain_text;
|
|
280
|
+
plain_text = this.annotatePlainText(plain_text, annotations);
|
|
281
|
+
if (content["href"])
|
|
282
|
+
plain_text = md.link(plain_text, content["href"]);
|
|
283
|
+
parsedData += plain_text;
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
switch (type) {
|
|
288
|
+
case "code":
|
|
289
|
+
{
|
|
290
|
+
parsedData = md.codeBlock(parsedData, block[type].language);
|
|
291
|
+
}
|
|
292
|
+
break;
|
|
293
|
+
case "heading_1":
|
|
294
|
+
{
|
|
295
|
+
parsedData = md.heading1(parsedData);
|
|
296
|
+
}
|
|
297
|
+
break;
|
|
298
|
+
case "heading_2":
|
|
299
|
+
{
|
|
300
|
+
parsedData = md.heading2(parsedData);
|
|
301
|
+
}
|
|
302
|
+
break;
|
|
303
|
+
case "heading_3":
|
|
304
|
+
{
|
|
305
|
+
parsedData = md.heading3(parsedData);
|
|
306
|
+
}
|
|
307
|
+
break;
|
|
308
|
+
case "quote":
|
|
309
|
+
{
|
|
310
|
+
parsedData = md.quote(parsedData);
|
|
311
|
+
}
|
|
312
|
+
break;
|
|
313
|
+
case "callout":
|
|
314
|
+
{
|
|
315
|
+
parsedData = md.callout(parsedData, block[type].icon);
|
|
316
|
+
}
|
|
317
|
+
break;
|
|
318
|
+
case "bulleted_list_item":
|
|
319
|
+
{
|
|
320
|
+
parsedData = md.bullet(parsedData);
|
|
321
|
+
}
|
|
322
|
+
break;
|
|
323
|
+
case "numbered_list_item":
|
|
324
|
+
{
|
|
325
|
+
parsedData = md.bullet(parsedData, block.numbered_list_item.number);
|
|
326
|
+
}
|
|
327
|
+
break;
|
|
328
|
+
case "to_do":
|
|
329
|
+
{
|
|
330
|
+
parsedData = md.todo(parsedData, block.to_do.checked);
|
|
331
|
+
}
|
|
332
|
+
break;
|
|
333
|
+
}
|
|
334
|
+
return parsedData;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Annoate text using provided annotations
|
|
338
|
+
* @param {string} text - String to be annotated
|
|
339
|
+
* @param {Annotations} annotations - Annotation object of a notion block
|
|
340
|
+
* @returns {string} - Annotated text
|
|
341
|
+
*/
|
|
342
|
+
annotatePlainText(text, annotations) {
|
|
343
|
+
// if text is all spaces, don't annotate
|
|
344
|
+
if (text.match(/^\s*$/))
|
|
345
|
+
return text;
|
|
346
|
+
const leadingSpaceMatch = text.match(/^(\s*)/);
|
|
347
|
+
const trailingSpaceMatch = text.match(/(\s*)$/);
|
|
348
|
+
const leading_space = leadingSpaceMatch ? leadingSpaceMatch[0] : "";
|
|
349
|
+
const trailing_space = trailingSpaceMatch ? trailingSpaceMatch[0] : "";
|
|
350
|
+
text = text.trim();
|
|
351
|
+
if (text !== "") {
|
|
352
|
+
if (annotations.code)
|
|
353
|
+
text = md.inlineCode(text);
|
|
354
|
+
if (annotations.bold)
|
|
355
|
+
text = md.bold(text);
|
|
356
|
+
if (annotations.italic)
|
|
357
|
+
text = md.italic(text);
|
|
358
|
+
if (annotations.strikethrough)
|
|
359
|
+
text = md.strikethrough(text);
|
|
360
|
+
if (annotations.underline)
|
|
361
|
+
text = md.underline(text);
|
|
362
|
+
}
|
|
363
|
+
return leading_space + text + trailing_space;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
exports.NotionToMarkdown = NotionToMarkdown;
|
|
329
367
|
//# sourceMappingURL=notion-to-md.js.map
|