-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
638 lines (553 loc) · 24.3 KB
/
main.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
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import {App, FuzzySuggestModal, Plugin, PluginSettingTab, Setting, TFolder, Notice} from "obsidian";
import {fetchNotionData, getDatabaseName} from "./notionHandling";
import {createMarkdownFiles} from "./markdownCreation";
import fs from "fs";
import tippy from 'tippy.js';
import {TextInputSuggest} from "./suggest";
export class FolderSuggest extends TextInputSuggest<TFolder> {
getSuggestions(inputStr: string): TFolder[] {
const abstractFiles = app.vault.getAllLoadedFiles();
const folders: TFolder[] = [];
const lowerCaseInputStr = inputStr.toLowerCase();
abstractFiles.forEach((folder: TAbstractFile) => {
if (
folder instanceof TFolder &&
folder.path.toLowerCase().contains(lowerCaseInputStr)
) {
folders.push(folder);
}
});
return folders;
}
renderSuggestion(file: TFolder, el: HTMLElement): void {
el.setText(file.path);
}
selectSuggestion(file: TFolder): void {
this.inputEl.value = file.path;
this.inputEl.trigger("input");
this.close();
}
}
interface NotionMigrationSettings {
apiKey: string;
databaseId: string;
migrationPath: string;
migrationLog: string;
attachmentPath: string;
attachPageId: boolean;
importPageContent: boolean;
isImporting: boolean;
createRelationContentPage: boolean;
createSemanticLinking: boolean;
squashDateNamesForDataview: boolean;
enabledProperties: { [key: string]: boolean };
}
const DEFAULT_SETTINGS: NotionMigrationSettings = {
apiKey: "",
databaseId: "",
migrationPath: "",
migrationLog: "",
attachmentPath: "",
attachPageId: false,
importPageContent: true,
isImporting: false,
createRelationContentPage: true,
enabledProperties: {},
createSemanticLinking: true,
squashDateNamesForDataview: true,
};
export default class NotionMigrationPlugin extends Plugin {
settings: NotionMigrationSettings;
importControl = {
isImporting: false
};
async onload() {
await this.loadSettings();
this.addSettingTab(new NotionMigrationSettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class NotionMigrationSettingTab extends PluginSettingTab {
plugin: NotionMigrationPlugin;
collapsibleContent: HTMLElement;
loadingEl: HTMLElement;
statusEl: HTMLElement;
constructor(app: App, plugin: NotionMigrationPlugin) {
super(app, plugin);
this.plugin = plugin;
}
private activeNotice: Notice | null = null;
showLoading(message: string) {
if (this.activeNotice) {
this.activeNotice.hide();
}
this.activeNotice = new Notice(message, 5000); // 5 second timeout
return this.activeNotice;
}
hideLoading(notice: Notice) {
if (notice) {
notice.hide();
}
}
showStatus(message: string, type: 'success' | 'error' | 'info' = 'info') {
const duration = type === 'error' ? 10000 : 3000;
new Notice(message, duration);
}
private listAllDirectories(): TFolder[] {
const vaultPath = this.app.vault.adapter.basePath;
const directories = [];
const items = fs.readdirSync(vaultPath, {withFileTypes: true});
for (const item of items) {
if (item.isDirectory()) {
const folder = this.app.vault.getAbstractFileByPath(item.name);
if (folder instanceof TFolder) {
directories.push(folder);
}
}
}
return directories;
}
// Declare a variable to hold the text input element for Database ID
dbIdInput: HTMLInputElement;
display(): void {
const {containerEl} = this;
containerEl.empty();
let {logWindow, startButton, stopButton} = this.createUI(containerEl);
if (this.plugin.settings.isImporting) {
startButton.textContent = "Migrating...";
startButton.disabled = true;
stopButton.disabled = false;
} else {
startButton.textContent = "Start Migration";
startButton.disabled = false;
stopButton.disabled = true;
}
logWindow.value = this.plugin.settings.migrationLog;
stopButton.disabled = !this.plugin.importControl.isImporting;
startButton.addEventListener("click", async () => {
try {
this.showLoading("Starting migration...");
const migrationPath = this.plugin.settings.migrationPath;
// Check if the folder exists
const folder = this.app.vault.getAbstractFileByPath(migrationPath);
if (!folder || !(folder instanceof TFolder)) {
logWindow.value += `Error: Folder "${migrationPath}" does not exist.\n`;
this.showStatus(`Error: Folder "${migrationPath}" does not exist`, "error");
return;
}
this.plugin.settings.isImporting = true;
await this.plugin.saveSettings();
this.plugin.importControl.isImporting = true;
stopButton.disabled = false;
startButton.textContent = "Migrating...";
startButton.disabled = true;
const logMessage = (message) => {
logWindow.value += `${message}\n`;
logWindow.scrollTop = logWindow.scrollHeight;
this.plugin.settings.migrationLog = logWindow.value;
this.plugin.saveSettings();
};
const dbName = await getDatabaseName(this.plugin.settings.apiKey, this.plugin.settings.databaseId);
if (dbName) {
logMessage(`Starting migration from Notion database: ${dbName}`);
} else {
logMessage("Starting migration from Notion...");
}
logMessage("Fetching data from Notion...");
const allPages = await fetchNotionData(
this.plugin.settings.databaseId,
this.plugin.settings.apiKey
);
logMessage(`${allPages.length} items fetched from Notion.`);
logMessage("Creating markdown files...");
await createMarkdownFiles(
allPages,
this.plugin.settings.migrationPath,
this.plugin.settings.apiKey,
this.app,
this.plugin.settings.attachPageId,
this.plugin.settings.importPageContent,
this.plugin.importControl,
logMessage,
this.plugin.settings.createRelationContentPage,
this.plugin.settings.enabledProperties,
this.plugin.settings.createSemanticLinking,
this.plugin.settings.attachmentPath,
this.plugin.settings.squashDateNamesForDataview,
);
logMessage("Migration completed!");
this.showStatus("Migration completed successfully!", "success");
} catch (error) {
logWindow.value += `Error: ${error.message}\n`;
this.showStatus(`Migration failed: ${error.message}`, "error");
} finally {
this.hideLoading();
startButton.textContent = "Start Migration";
this.plugin.settings.isImporting = false;
await this.plugin.saveSettings();
startButton.disabled = false;
}
});
stopButton.addEventListener("click", async () => {
this.plugin.settings.isImporting = false;
this.plugin.importControl.isImporting = false;
await this.plugin.saveSettings();
stopButton.disabled = true;
startButton.textContent = "Start Migration";
});
}
async displayPageList() {
try {
this.showLoading("Fetching Notion databases...");
const apiKey = this.plugin.settings.apiKey;
if (!apiKey) {
this.showStatus("Please enter your Notion API key first", "error");
return;
}
const query = '';
const requestOptions = {
method: 'POST',
url: 'https://api.notion.com/v1/search',
headers: {
Authorization: `Bearer ${apiKey}`,
'Notion-Version': '2022-06-28',
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: query,
filter: {
value: 'database',
property: 'object',
},
}),
};
const response = await request(requestOptions);
const responseData = JSON.parse(response);
const container = document.getElementById('page-list-container');
container.innerHTML = '';
const buttonContainer = container.createDiv({
cls: 'hide-button-container',
attr: {
style: 'margin-top: 10px; margin-bottom: 10px;display: flex; justify-content: space-between;'
}
});
const hideButton = buttonContainer.createEl('button', {text: 'Hide'});
hideButton.addEventListener('click', () => {
tableWrapper.style.display = tableWrapper.style.display === 'none' ? 'block' : 'none';
});
const messageSpan = buttonContainer.createEl('span', {
text: 'Click on the row to update database ID. Hover to check properties.',
attr: {}
});
const tableWrapper = container.createEl('div', {
attr: {style: 'max-height: 350px; overflow-y: auto;'}
});
const table = tableWrapper.createEl('table', {
attr: {style: 'width: 100%; border-collapse: collapse;position: relative'}
});
const thead = table.createEl('thead');
const headerRow = thead.createEl('tr');
headerRow.createEl('th', {text: 'Page Name', attr: {style: 'padding: 10px; border: 1px solid #ccc;'}});
headerRow.createEl('th', {text: 'Page ID', attr: {style: 'padding: 10px; border: 1px solid #ccc;'}});
const tbody = table.createEl('tbody');
for (const page of responseData.results) {
const pageName = page.title && page.title[0] && page.title[0].plain_text
? page.title[0].plain_text
: '';
const row = tbody.createEl('tr');
row.createEl('td', {text: pageName, attr: {style: 'padding: 10px; border: 1px solid #ccc;'}});
row.createEl('td', {text: page.id, attr: {style: 'padding: 10px; border: 1px solid #ccc;'}});
let propertiesText = "<strong>Properties</strong> <br>";
for (const [key, value] of Object.entries(page.properties || {})) {
propertiesText += `${key} - ${value.type} <br>`;
}
tippy(row, {
content: propertiesText,
allowHTML: true,
theme: 'light',
delay: 100,
arrow: true,
duration: [300, 200],
});
row.addEventListener('click', async () => {
const loadingIndicator = document.createElement('div');
loadingIndicator.innerHTML = 'Loading properties...';
loadingIndicator.style.position = 'absolute';
loadingIndicator.style.top = '50%';
loadingIndicator.style.left = '50%';
loadingIndicator.style.transform = 'translate(-50%, -50%)';
loadingIndicator.style.zIndex = '10';
loadingIndicator.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
loadingIndicator.style.color = 'white';
loadingIndicator.style.padding = '15px';
loadingIndicator.style.borderRadius = '5px';
tableWrapper.appendChild(loadingIndicator);
this.plugin.settings.databaseId = page.id;
if (this.dbIdInput) {
this.dbIdInput.value = page.id;
}
await this.plugin.saveSettings();
this.plugin.settings.enabledProperties = {};
const allPages = await fetchNotionData(page.id, this.plugin.settings.apiKey);
tableWrapper.removeChild(loadingIndicator);
if (allPages.length === 0) {
this.showStatus("This database is empty", "info");
return;
}
if (allPages.length > 0) {
const exampleProperties = allPages[0].properties;
const existingPropertiesTable = document.getElementById('properties-table');
if (existingPropertiesTable) {
existingPropertiesTable.remove();
}
const propertiesTable = this.collapsibleContent.createEl('table', {
attr: {
id: 'properties-table',
style: 'width: 100%; border-collapse: collapse; margin-top: 20px;'
}
});
const propertiesThead = propertiesTable.createEl('thead');
const propertiesHeaderRow = propertiesThead.createEl('tr');
propertiesHeaderRow.createEl('th', {
text: 'Name',
attr: {style: 'padding: 10px; border: 1px solid #ccc;'}
});
propertiesHeaderRow.createEl('th', {
text: 'Type',
attr: {style: 'padding: 10px; border: 1px solid #ccc;'}
});
propertiesHeaderRow.createEl('th', {
text: 'Import',
attr: {style: 'padding: 10px; border: 1px solid #ccc;'}
});
const propertiesTbody = propertiesTable.createEl('tbody');
for (const [key, value] of Object.entries(exampleProperties)) {
const propertyRow = propertiesTbody.createEl('tr');
propertyRow.createEl('td', {
text: key,
attr: {style: 'padding: 10px; border: 1px solid #ccc;'}
});
propertyRow.createEl('td', {
text: value.type,
attr: {style: 'padding: 10px; border: 1px solid #ccc;'}
});
const checkboxCell = propertyRow.createEl('td', {attr: {style: 'padding: 10px; border: 1px solid #ccc;'}});
const checkbox = checkboxCell.createEl('input', {
attr: {
type: 'checkbox',
checked: this.plugin.settings.enabledProperties[key] ?? true
}
});
checkbox.addEventListener('change', async (event) => {
const isChecked = (event.target as HTMLInputElement).checked;
this.plugin.settings.enabledProperties[key] = isChecked;
await this.plugin.saveSettings();
});
}
}
});
}
} catch (error) {
console.error('Error fetching data:', error);
this.showStatus(`Failed to fetch databases: ${error.message}`, "error");
} finally {
this.hideLoading();
}
}
private createUI(containerEl: HTMLElement) {
containerEl.empty();
containerEl.createEl("h1", {
text: "Notion to Obsidian Migration",
cls: "n2o-main-header"
});
// Connection Settings Section
containerEl.createEl("h2", {
text: "Connection Settings",
cls: "n2o-section-header"
});
new Setting(containerEl)
.setName("Notion API Key")
.setDesc(createFragment(frag => {
frag.appendText("Enter your Notion API key here. ");
frag.createEl("a", {
text: "Get your API key",
href: "https://www.notion.so/my-integrations",
cls: "n2o-link"
});
}))
.addText((text) =>
text.setValue(this.plugin.settings.apiKey).onChange(async (value) => {
this.plugin.settings.apiKey = value;
await this.plugin.saveSettings();
})
)
.addButton(button => button
.setButtonText("Search DBs")
.setCta()
.onClick(() => {
this.displayPageList();
})
);
containerEl.createDiv({
attr: {id: 'page-list-container'}
});
// Database Settings Section
containerEl.createEl("h2", {
text: "Database Settings",
cls: "n2o-section-header"
});
new Setting(containerEl)
.setName("Database ID")
.setDesc("Selected database ID - click on a database above to update")
.addText((text) => {
this.dbIdInput = text.inputEl;
text
.setValue(this.plugin.settings.databaseId)
.setDisabled(true)
.onChange(async (value) => {
this.plugin.settings.databaseId = value;
await this.plugin.saveSettings();
});
});
const collapsibleHeader = containerEl.createEl("button", {
text: "Show Table Properties",
attr: {class: 'collapsible'}
});
collapsibleHeader.addEventListener("click", function () {
this.classList.toggle("active");
const content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
this.collapsibleContent = containerEl.createEl("div", {
attr: {class: 'collapsible-content'}
});
// Migration Settings Section
containerEl.createEl("h2", {
text: "Migration Settings",
cls: "n2o-section-header"
});
new Setting(containerEl)
.setName("Migration Path")
.setDesc("Select where to save the migrated notes")
.addText((text) => {
text.setValue(this.plugin.settings.migrationPath)
.onChange(async (value) => {
this.plugin.settings.migrationPath = value;
await this.plugin.saveSettings();
});
new FolderSuggest(text.inputEl);
});
new Setting(containerEl)
.setName("Attachment Path")
.setDesc("Select where to save attachments (images, files)")
.addText((text) => {
text.setValue(this.plugin.settings.attachmentPath)
.onChange(async (value) => {
this.plugin.settings.attachmentPath = value;
await this.plugin.saveSettings();
});
new FolderSuggest(text.inputEl);
});
// Content Settings Section
containerEl.createEl("h2", {
text: "Content Settings",
cls: "n2o-section-header"
});
new Setting(containerEl)
.setName("Create relations also inside the page")
.setDesc("Creates clickable links ([[Page]]) both in frontmatter and note body. Useful for graph view and navigation.")
.addToggle(toggle =>
toggle
.setValue(this.plugin.settings.createRelationContentPage)
.onChange(async value => {
this.plugin.settings.createRelationContentPage = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Create Semantic Linking")
.setDesc("Creates Dataview-compatible links (property:: [[Page]]). Enable if you use Dataview plugin.")
.addToggle(toggle =>
toggle
.setValue(this.plugin.settings.createSemanticLinking)
.onChange(async value => {
this.plugin.settings.createSemanticLinking = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Squash Date Names")
.setDesc("Converts multi-word date fields (e.g., 'Due Date') to camelCase ('DueDate') for Dataview compatibility.")
.addToggle(toggle =>
toggle
.setValue(this.plugin.settings.squashDateNamesForDataview)
.onChange(async value => {
this.plugin.settings.squashDateNamesForDataview = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Attach page ID")
.setDesc("Adds Notion's page ID to filenames to prevent conflicts. Example: 'Note_7b8b0713'")
.addToggle(toggle =>
toggle
.setValue(this.plugin.settings.attachPageId)
.onChange(async value => {
this.plugin.settings.attachPageId = value;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Import page content")
.setDesc("Choose whether to import the content of the pages from Notion.")
.addToggle(toggle =>
toggle
.setValue(this.plugin.settings.importPageContent)
.onChange(async value => {
this.plugin.settings.importPageContent = value;
await this.plugin.saveSettings();
})
);
// Migration Log Section
containerEl.createEl("h2", {
text: "Migration Log",
cls: "n2o-section-header"
});
const logWindow = containerEl.createEl("textarea", {
attr: {
class: "n2o-log-window",
readonly: "readonly"
}
});
const buttonContainer = containerEl.createDiv({
cls: 'n2o-button-container'
});
const startButton = buttonContainer.createEl("button", {
text: "Start Migration",
cls: ["mod-cta", "n2o-start-button"],
});
const stopButton = buttonContainer.createEl("button", {
text: "Stop Migration",
cls: ["mod-warning", "n2o-stop-button"],
});
const clearButton = buttonContainer.createEl("button", {
text: "Clear Log",
cls: "mod-warning",
});
clearButton.addEventListener("click", () => {
logWindow.value = "";
this.plugin.settings.migrationLog = "";
this.plugin.saveSettings();
});
return {logWindow, startButton, stopButton};
}
}