-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdownCreation.js
420 lines (385 loc) · 10.8 KB
/
markdownCreation.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
import {moment, request} from "obsidian";
import {extractContentFromPage} from "./notionHandling";
import {downloadFile, generateUniqueTitle, sanitizeTitle, writeFilePromise,} from "./utilities";
import path from "path";
async function createMarkdownFiles(
allPages,
folderName,
apiKey,
app,
attachPageId,
importPageContent,
importControl,
logMessage,
createRelationContentPage,
enabledProperties,
createSemanticLinking,
attachmentPath,
squashDateNamesForDataview,
subpagesPath,
importSubpages
) {
const promises = [];
const vaultPath = app.vault.adapter.basePath; // Get the base path of the Obsidian vault
let pageTitle = "";
// Initial check if import should proceed
if (!importControl || !importControl.isImporting) {
logMessage("Import not active or was halted.");
return;
}
for (const page of allPages) {
if (!importControl.isImporting) {
logMessage(
"Import halted by user. Finishing remaining subpages and files..."
);
break;
}
let relationLinks = [];
let relationSemanticLinks = [];
let title = "empty";
for (const [key, property] of Object.entries(page.properties)) {
if (property.title && property.title[0]) {
title = property.title[0].plain_text;
title = sanitizeTitle(title);
break;
}
}
// Append the Notion page ID to the title to ensure uniqueness
if (attachPageId) title = `${title}_${page.id}`;
else title = generateUniqueTitle(title, `${vaultPath}/${folderName}`);
let content = `---\n`;
for (const [key, property] of Object.entries(page.properties)) {
if (enabledProperties[key] === false) {
continue;
}
const safeKey = (key) => (/[^\w\s]/.test(key) ? `"${key}"` : key);
const safeValue = (value) =>
/[\W_]/.test(value) ? `"${value}"` : value;
switch (property.type) {
case "select":
if (property.select) {
content += `${safeKey(key)}: ${safeValue(
property.select.name
)}\n`;
}
break;
case "rich_text":
if (property.rich_text && property.rich_text.length) {
const textContent = property.rich_text
.map((text) => text.plain_text)
.join("")
.replace(/\n/g, " "); // Replacing newline characters with spaces
content += `${safeKey(key)}: >-\n ${safeValue(
textContent
)}\n`;
} else {
content += `${safeKey(key)}: null\n`;
}
break;
case "checkbox":
content += `${safeKey(key)}: ${
property.checkbox ? "true" : "false"
}\n`;
break;
case "date": {
let finalKey = key;
if (squashDateNamesForDataview) {
finalKey = key
.split(" ")
.map(
(word) =>
word.charAt(0).toUpperCase() + word.slice(1)
)
.join("");
}
if (property.date && property.date.start) {
let newDate = moment
.utc(property.date.start)
.toISOString();
content += `${safeKey(finalKey)}: ${newDate}\n`;
} else {
content += `${safeKey(finalKey)}: null\n`;
}
break;
}
case "number":
if (property.number) {
content += `${safeKey(key)}: ${property.number}\n`;
} else {
content += `${safeKey(key)}: \n`;
}
break;
case "status":
if (property.status && property.status.name) {
content += `${safeKey(key)}: ${safeValue(
property.status.name
)}\n`;
} else {
content += `${safeKey(key)}: \n`;
}
break;
case "multi_select":
if (property.multi_select && property.multi_select.length) {
const tags = property.multi_select
.map((tag) => `${tag.name}`)
.join(" ");
content += `${safeKey(key)}: ${tags}\n`;
}
break;
case "files":
content += `${safeKey(key)}:\n`;
if (property.files && property.files.length > 0) {
for (const file of property.files) {
try {
let fileUrl, fileName;
if (file.type === "external") {
fileUrl = file.external?.url;
fileName =
fileUrl.split("/").pop() ||
`external_file_${Date.now()}`;
} else if (file.type === "file") {
fileUrl = file.file?.url;
fileName =
file.name ||
`notion_file_${Date.now()}`;
}
if (!fileUrl) {
console.warn(
`No URL found for file: ${fileName}`
);
continue;
}
const fileExtension = getFileExtension(fileUrl);
const safeFileName = sanitizeTitle(fileName);
const outputPath = path.join(
attachmentPath,
`${safeFileName}${
fileExtension ? "." + fileExtension : ""
}`
);
await downloadFile(
fileUrl,
outputPath,
app,
apiKey
);
content += ` - [[${path.basename(
outputPath
)}]]\n`;
logMessage(`Downloaded file: ${fileName}`);
} catch (error) {
const errorMsg = `Failed to download file: ${error.message}`;
console.error(errorMsg);
logMessage(errorMsg);
content += ` - Failed: ${
file.name || "unnamed file"
} (${error.message})\n`;
}
}
} else {
content += ` []\n`;
}
break;
case "formula":
let formulaType = property.formula.type;
let formulaValue = "";
switch (formulaType) {
case "number":
formulaValue = property.formula.number;
content += `${safeKey(key)}: ${formulaValue}\n`;
break;
case "string":
formulaValue = property.formula.string;
content += `${safeKey(key)}: ${formulaValue}\n`;
break;
case "boolean":
// Handle boolean formula
content += `${safeKey(key)}: ${
property.formula.boolean
}\n`;
break;
default:
console.warn(
`Unknown formula type: ${formulaType}`
);
break;
}
break;
case "created_time":
if (property.created_time) {
let createdDate = moment
.utc(property.created_time)
.toISOString();
content += `${safeKey(key)}: ${createdDate}\n`;
} else {
content += `${safeKey(key)}: \n`;
}
break;
case "relation":
if (property.relation && property.relation.length) {
const requestHeaders = {
Authorization: `Bearer ${apiKey}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
};
let relatedNames = [];
for (const rel of property.relation) {
const pageId = rel.id;
const response = await request({
url: `https://api.notion.com/v1/pages/${pageId}`,
method: "GET",
headers: requestHeaders,
});
const pageData = JSON.parse(response);
const pageName =
pageData.properties.Name.title[0].plain_text;
relatedNames.push(pageName);
}
// Semantic Linking part
if (createSemanticLinking) {
const safeKeyWithUnderscores = safeKey(key).replace(
/ /g,
"_"
);
const semanticLink = `${safeKeyWithUnderscores}:: ${relatedNames
.map((name) => `[[${name}]]`)
.join(", ")}\n`;
relationSemanticLinks.push(semanticLink);
}
if (createRelationContentPage) {
// Create relation in YAML list format - ONLY add to relationLinks, not to content directly
relationLinks.push(`${safeKey(key)}:\n${relatedNames
.map((name) => ` - [[${name}]]`)
.join("\n")}\n`);
} else {
// Create relation in YAML format directly in content
content += `${safeKey(key)}:\n${relatedNames
.map((name) => ` - ${name}`)
.join("\n")}\n`;
}
} else {
content += `${safeKey(key)}: \n`;
}
break;
case "url":
if (property.url) {
content += `${safeKey(key)}: ${property.url}\n`;
} else {
content += `${safeKey(key)}: \n`;
}
break;
case "rollup": {
const rollupArray = property.rollup
? property.rollup.array
: null; // Check if rollup is defined
if (Array.isArray(rollupArray)) {
rollupArray.forEach((rollupItem) => {
switch (rollupItem.type) {
case "formula":
switch (rollupItem.formula.type) {
case "string":
// Handle string formula inside rollup
content += `${safeKey(
key
)}: ${safeValue(
rollupItem.formula.string
)}\n`;
break;
case "boolean":
// Handle boolean formula
content += `${safeKey(key)}: ${
rollupItem.formula.boolean
}\n`;
break;
case "number":
if (
rollupItem.function ===
"percent_per_group"
) {
const numberValue =
rollupItem.number;
content += `${safeKey(
key
)}: ${numberValue}\n`;
}
break;
default:
// Handle other or unknown formula types inside rollup
break;
}
break;
}
});
} else {
content += `${safeKey(key)}: null\n`; // Handle case when rollupArray is not defined
}
}
break;
case "title":
if (property.title && property.title[0]) {
const keyWithUnderscores =
property.title[0].plain_text.replace(/ /g, "_");
const finalKey = safeKey(keyWithUnderscores);
pageTitle = finalKey;
content += `Alias: ${finalKey}\n`;
} else {
content += `Alias: \n`;
}
break;
default:
break;
}
}
content += `---\n`;
// Only add one type of relation formatting based on settings
if (createSemanticLinking && relationSemanticLinks.length > 0) {
content += relationSemanticLinks;
} else if (relationLinks.length > 0) {
content += relationLinks;
}
if (importPageContent) {
try {
await extractContentFromPage(
page.id,
pageTitle,
apiKey,
attachmentPath,
vaultPath,
subpagesPath,
logMessage,
importControl,
app,
importSubpages
).then((result) => (content += result));
} catch (error) {
console.error("Error in extractContentFromPage:", error);
}
}
// Add feedback for current file
logMessage(`Importing: ${title}`);
if (!importControl.forceStop) {
promises.push(
writeFilePromise(
`${vaultPath}/${folderName}/${title}.md`,
content
)
);
}
}
// Wait for all file writes to complete if we should continue
if (importControl.isImporting && !importControl.forceStop) {
await Promise.all(promises);
// Signal completion only if we're still importing
if (importControl.isImporting) {
logMessage("Migration completed automatically!");
importControl.isImporting = false;
}
}
}
// Function to get the file extension from the URL
function getFileExtension(url) {
const urlObj = new URL(url);
const pathname = urlObj.pathname;
return pathname.split(".").pop() || "";
}
export {createMarkdownFiles};