-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotionHandling.js
539 lines (489 loc) · 14 KB
/
notionHandling.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import {request} from "obsidian";
import path from "path";
import {downloadFile, getImageExtension, writeFilePromise} from "./utilities";
async function getDatabaseName(apiKey, databaseId) {
const requestHeaders = {
Authorization: `Bearer ${apiKey}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
};
try {
const response = await request({
url: `https://api.notion.com/v1/databases/${databaseId}`,
method: "GET",
headers: requestHeaders,
});
const data = JSON.parse(response);
return data.title[0].plain_text; // Extracting the database name from the response
} catch (error) {
console.error("Error fetching database name:", error);
return null; // Return null if there's an error
}
}
async function fetchNotionData(databaseId, apiKey) {
const requestHeaders = {
Authorization: `Bearer ${apiKey}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
};
let results = [];
let hasMore = true;
let startCursor = null;
while (hasMore) {
const requestBody = startCursor ? {start_cursor: startCursor} : {};
const response = await request({
url: `https://api.notion.com/v1/databases/${databaseId}/query`,
method: "POST",
headers: requestHeaders,
body: JSON.stringify(requestBody),
});
const data = JSON.parse(response);
results.push(...data.results);
hasMore = data.has_more;
startCursor = data.next_cursor;
}
return results;
}
async function fetchBlockContent(
blocks,
previousBlockType,
numberCounter,
content,
attachmentPath,
apiKey,
pageName,
fileCounter,
vaultPath,
safeKey,
promises,
subpagesPath,
importControl,
app,
logMessage,
importSubpages
) {
importControl = importControl || {isImporting: false, forceStop: false}; // Default importControl to an object with isImporting and forceStop properties
for (const block of blocks.results) {
if (importControl && importControl.forceStop) {
return content;
}
// Reset the numbered list counter if the block type changes
if (
previousBlockType === "numbered_list_item" &&
block.type !== "numbered_list_item"
) {
numberCounter = 1;
}
switch (block.type) {
case "rich_text":
if (block.rich_text && block.rich_text.length) {
content += `${block.rich_text
.map((text) => text.plain_text)
.join("")}\n\n`;
}
break;
case "paragraph":
if (
block.paragraph &&
block.paragraph.rich_text &&
block.paragraph.rich_text.length
) {
let paragraphContent = "";
for (const richTextElement of block.paragraph.rich_text) {
if (richTextElement.type === "text") {
if (richTextElement.href) {
paragraphContent += `[${richTextElement.plain_text}](${richTextElement.href})`;
} else {
paragraphContent += richTextElement.plain_text;
}
} else if (
richTextElement.type === "mention" &&
richTextElement.mention.type === "date"
) {
const originalDate = new Date(
richTextElement.plain_text
);
const formattedDate = `${originalDate.getDate()}.${
originalDate.getMonth() + 1
}.${originalDate.getFullYear()}`;
paragraphContent += `[[${formattedDate}]]`; // Date formatted as DD.MM.YYYY
} else {
// Handle other types as needed
}
}
content += `${paragraphContent}\n\n`;
}
break;
case "heading_1":
case "heading_2":
case "heading_3":
if (
block[block.type] &&
block[block.type].rich_text &&
block[block.type].rich_text.length
) {
const heading = `#`.repeat(
Number(block.type.split("_")[1])
);
let headingContent = "";
for (const richTextElement of block[block.type].rich_text) {
let text = richTextElement.plain_text;
if (richTextElement.annotations.bold) {
text = `**${text}**`;
}
if (richTextElement.annotations.italic) {
text = `*${text}*`;
}
headingContent += text;
}
content += `${heading} ${headingContent}\n\n`;
}
break;
case "bulleted_list_item":
case "numbered_list_item":
if (
block[block.type] &&
block[block.type].rich_text &&
block[block.type].rich_text.length
) {
const prefix =
block.type === "bulleted_list_item"
? "-"
: `${numberCounter}.`;
// Increment the number counter if it's a numbered list item
if (block.type === "numbered_list_item") {
numberCounter++;
}
let listItemContent = "";
for (const richTextElement of block[block.type].rich_text) {
let textContent = richTextElement.plain_text;
// Apply bold formatting if needed
if (richTextElement.annotations.bold) {
textContent = `**${textContent}**`;
}
// Apply italic formatting if needed
if (richTextElement.annotations.italic) {
textContent = `*${textContent}*`;
}
listItemContent += textContent;
}
content += `${prefix} ${listItemContent}\n`;
}
break;
case "image":
if (importControl && importControl.forceStop) {
return content;
}
if (
block.image &&
block.image.external &&
block.image.external.url
) {
// Handle external images
content += `\n\n`;
} else if (
block.image &&
block.image.file &&
block.image.file.url
) {
// Handle images uploaded to Notion
const imageUrl = block.image.file.url;
const fileExtension = getImageExtension(imageUrl);
const imagePath = path.join(
attachmentPath, // Use attachmentPath here
`image_${Date.now()}.${fileExtension}`
);
await downloadFile(imageUrl, imagePath, app, apiKey); // Assuming app and apiKey are available
content += `![[${path.basename(imagePath)}]]\n\n`;
}
break;
case "to_do":
if (
block.to_do &&
block.to_do.rich_text &&
block.to_do.rich_text.length
) {
const checkbox = block.to_do.checked ? "[x]" : "[ ]";
let todoContent = "";
for (const richTextElement of block.to_do.rich_text) {
let textContent = richTextElement.plain_text;
if (richTextElement.annotations.bold) {
textContent = `**${textContent}**`;
}
if (richTextElement.annotations.italic) {
textContent = `*${textContent}*`;
}
todoContent += textContent;
}
content += `${checkbox} ${todoContent}\n`;
}
break;
case "table":
if (block.table && block.table.headers && block.table.rows) {
content += "| " + block.table.headers.join(" | ") + " |\n";
content +=
"| " +
new Array(block.table.headers.length)
.fill("---")
.join(" | ") +
" |\n";
block.table.rows.forEach((row) => {
content += "| " + row.join(" | ") + " |\n";
});
}
break;
case "code":
if (
block.code &&
block.code.rich_text &&
block.code.rich_text.length
) {
let codeContent = block.code.rich_text
.map((text) => text.plain_text)
.join("");
const language = block.code.language
? block.code.language
: "";
content += `\`\`\`${language}\n${codeContent}\n\`\`\`\n\n`;
}
break;
case "link_preview":
if (block.link_preview && block.link_preview.url) {
const linkUrl = block.link_preview.url;
content += `[Link Preview](${linkUrl})\n\n`;
}
break;
case "toggle":
if (
block.toggle &&
block.toggle.rich_text &&
block.toggle.rich_text.length
) {
let toggleContent = "";
for (const richTextElement of block.toggle.rich_text) {
let textContent = richTextElement.plain_text;
toggleContent += textContent;
}
content += `> [!NOTE]+ ${toggleContent} \n`;
}
break;
case "video":
if (importControl && importControl.forceStop) {
return content;
}
if (
block.video &&
block.video.type === "external" &&
block.video.external.url
) {
const videoUrl = block.video.external.url;
content += `Video: [${videoUrl}](${videoUrl})\n\n`;
}
break;
case "audio":
if (importControl && importControl.forceStop) {
return content;
}
if (block.audio && block.audio.file && block.audio.file.url) {
const audioUrl = block.audio.file.url;
const audioExtension = path.extname(
new URL(audioUrl).pathname
);
const audioFileName = `${pageName}_${fileCounter}${audioExtension}`;
const audioFilePath = path.join(
attachmentPath, // Use attachmentPath here
audioFileName
);
await downloadFile(audioUrl, audioFilePath, app, apiKey); // Assuming app and apiKey are available
content += `![[${path.basename(audioFilePath)}]]\n\n`; // Or however you want to reference the audio file
// Increment the file counter
fileCounter++;
}
break;
case "file":
if (importControl && importControl.forceStop) {
return content;
}
if (block.file && block.file.file && block.file.file.url) {
const fileUrl = block.file.file.url;
const fileExtension = path.extname(
new URL(fileUrl).pathname
);
const fileName = `${pageName}_${fileCounter}${fileExtension}`;
const filePath = path.join(attachmentPath, fileName);
await downloadFile(fileUrl, filePath, app, apiKey);
content += `![[${path.basename(filePath)}]]\n\n`;
// Increment the file counter
fileCounter++;
} else if (block.external && block.external.url) {
const externalUrl = block.external.url;
content += `[[${externalUrl}]]\n\n`; // Adjusted format for external links as well
}
break;
case "bookmark":
if (block.bookmark && block.bookmark.url) {
let titleText = block.bookmark.caption
.map((text) => text.plain_text)
.join("");
titleText = titleText || block.bookmark.url;
const bookmarkUrl = block.bookmark.url;
content += `[${titleText}](${bookmarkUrl})\n\n`;
}
break;
case "child_page":
if (importControl && importControl.forceStop) {
return content;
}
if (block.child_page && block.child_page.title) {
const childPageTitle = block.child_page.title;
// Only process the child page content if importSubpages is true
if (importSubpages) {
const childPageId = block.id;
// Make a recursive call to fetch the content of the child page
const childContent = await extractContentFromPage(
childPageId,
childPageTitle,
apiKey,
attachmentPath,
vaultPath,
subpagesPath,
logMessage,
importControl,
app,
importSubpages // Pass the parameter
);
// Prepare the path to save the subpage in the folder in the vault
const subpagePath = `${vaultPath}/${subpagesPath}/${safeKey(
childPageTitle
)}.md`;
// Log and push to promises array
promises.push(writeFilePromise(subpagePath, childContent));
}
// Add link based on importSubpages setting
if (importSubpages) {
// Internal wikilink if we're importing the page
content += `[[${childPageTitle}]]\n\n`;
} else {
// External link to Notion page if not importing
const notionPageUrl = `https://www.notion.so/${block.id.replace(/-/g, '')}`;
content += `[${childPageTitle}](${notionPageUrl})\n\n`;
}
}
break;
}
// Update the previous block type
previousBlockType = block.type;
if (block.has_children) {
if (importControl && importControl.forceStop) {
return content;
}
// Fetch children of the block
const childBlocksResponse = await request({
url: `https://api.notion.com/v1/blocks/${block.id}/children`,
method: "GET",
headers: {
Authorization: `Bearer ${apiKey}`,
"Notion-Version": "2022-06-28",
},
});
const childBlocks = JSON.parse(childBlocksResponse);
if (importControl && importControl.forceStop) {
return content;
}
// Recursively get the content for child blocks
const childContent = await fetchBlockContent(
{results: childBlocks.results},
previousBlockType,
numberCounter,
"",
attachmentPath,
apiKey,
pageName,
fileCounter,
vaultPath,
safeKey,
promises,
subpagesPath,
importControl,
app,
logMessage,
importSubpages
);
// Add ">" at the start of each line if the block is a "toggle" type
if (block.type === "toggle") {
const indentedChildContent = childContent
.split("\n")
.map((line) => "> " + line)
.join("\n");
content += indentedChildContent;
} else {
content += childContent;
}
}
}
return content;
}
async function extractContentFromPage(
pageId,
pageName,
apiKey,
attachmentPath,
vaultPath,
subpagesPath,
logMessage,
importControl,
app,
importSubpages
) {
importControl = importControl || {isImporting: false, forceStop: false}; // Default importControl to an object with isImporting and forceStop properties
if (importControl && importControl.forceStop) {
logMessage("Import stopped by user.");
return "";
}
const requestHeaders = {
Authorization: `Bearer ${apiKey}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
};
const safeKey = (key) => (/[^\w\s]/.test(key) ? `"${key}"` : key);
const response = await request({
url: `https://api.notion.com/v1/blocks/${pageId}/children`,
method: "GET",
headers: requestHeaders,
});
const blocks = JSON.parse(response);
const promises = [];
let content = "";
let numberCounter = 1;
let fileCounter = 1;
let previousBlockType = null; // Keep track of the previous block type
if (importControl && importControl.forceStop) {
logMessage && logMessage("Import stopped by user.");
return "";
}
content = await fetchBlockContent(
blocks,
previousBlockType,
numberCounter,
content,
attachmentPath,
apiKey,
pageName,
fileCounter,
vaultPath,
safeKey,
promises,
subpagesPath,
importControl,
app,
logMessage,
importSubpages
);
if (importControl && importControl.forceStop) {
logMessage && logMessage("Import stopped by user.");
return content;
}
await Promise.all(promises);
return content;
}
export {fetchNotionData, getDatabaseName, extractContentFromPage};