generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
783 lines (669 loc) · 20.5 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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
import {
App,
Editor,
MarkdownView,
Notice,
Plugin,
PluginSettingTab,
Setting,
TFile,
FuzzySuggestModal,
} from "obsidian";
enum importListTypes {
Disabled = "DISABLE",
OrderedList = "ORDERED",
UnorderedList = "UNORDERED",
CheckBox = "CHECKBOX",
}
enum orderedListDelimeters {
Parenthesis = ")",
Period = ".",
}
enum itemTypes {
Link = "LINK",
Tag = "TAG",
Alias = "ALIAS",
}
interface AutoMOCSettings {
//general
showRibbonButton: boolean;
linkToHeading: boolean;
linkWithAlias: boolean;
importAsList: string;
orderedListSeparator: string;
ignoredFolders: string;
//notifications
linkingMentionsNotice: boolean;
noNewLinksNotice: boolean;
newLinksAddedNotice: boolean;
}
const DEFAULT_SETTINGS: AutoMOCSettings = {
//general
showRibbonButton: true,
linkToHeading: false,
linkWithAlias: true,
importAsList: importListTypes.Disabled,
orderedListSeparator: orderedListDelimeters.Period,
ignoredFolders: "",
//notifications
linkingMentionsNotice: true,
noNewLinksNotice: true,
newLinksAddedNotice: false,
};
export class TagSuggestModal extends FuzzySuggestModal<string> {
selection: string;
plugin: AutoMOC;
constructor(app: App, plugin: AutoMOC) {
super(app);
this.plugin = plugin;
}
onOpen(): void {
this.setPlaceholder("Import notes with tags matching...");
this.setInstructions([
{ command: "↑↓", purpose: "to navigate" },
{ command: "↵", purpose: "to select tag" },
{ command: "esc", purpose: "to dismiss" },
]);
this.inputEl.focus();
}
onClose() {
let { contentEl } = this;
contentEl.empty();
}
getItems(): string[] {
const allFiles = this.app.metadataCache.resolvedLinks;
let tagsSet = new Set<string>();
Object.keys(allFiles).forEach((key) => {
const file = this.app.vault.getAbstractFileByPath(key);
if (file instanceof TFile) {
const body_tags = app.metadataCache.getFileCache(file).tags;
const frontmatter =
this.app.metadataCache.getFileCache(file).frontmatter;
if (body_tags) {
for (const tag of body_tags) {
tagsSet.add(tag["tag"]);
}
}
let f_tags: Array<string> = [];
if (frontmatter) {
if (Array.isArray(frontmatter["tags"])) {
f_tags = frontmatter["tags"];
for (const f_tag of f_tags) {
tagsSet.add("#" + f_tag);
}
}
if (String.isString(frontmatter["tags"])) {
f_tags = frontmatter["tags"].split(", ");
for (const f_tag of f_tags) {
tagsSet.add("#" + f_tag);
}
}
}
}
});
return Array.from(tagsSet).sort();
}
getItemText(item: string): string {
return item;
}
onChooseItem(item: string, evt: MouseEvent | KeyboardEvent): void {
this.selection = item;
this.plugin.runAutoMOC(itemTypes.Tag, item);
}
}
export class AliasSuggestModal extends FuzzySuggestModal<string> {
selection: string;
plugin: AutoMOC;
constructor(app: App, plugin: AutoMOC) {
super(app);
this.plugin = plugin;
}
onOpen(): void {
this.setPlaceholder("Import notes with alias matching...");
this.setInstructions([
{ command: "↑↓", purpose: "to navigate" },
{ command: "↵", purpose: "to select tag" },
{ command: "esc", purpose: "to dismiss" },
]);
this.inputEl.focus();
}
onClose() {
let { contentEl } = this;
contentEl.empty();
}
getItems(): string[] {
const allFiles = this.app.metadataCache.resolvedLinks;
let aliasSet = new Set<string>();
Object.keys(allFiles).forEach((key) => {
const file = this.app.vault.getAbstractFileByPath(key);
if (file instanceof TFile) {
const frontmatter =
this.app.metadataCache.getFileCache(file).frontmatter;
let aliases: Array<string> = [];
if (frontmatter) {
if (Array.isArray(frontmatter["aliases"])) {
aliases = frontmatter["aliases"];
for (const alias of aliases) {
aliasSet.add(alias);
}
}
if (String.isString(frontmatter["aliases"])) {
aliases = frontmatter["aliases"].split(", ");
for (const alias of aliases) {
aliasSet.add(alias);
}
}
}
}
});
return Array.from(aliasSet).sort();
}
getItemText(item: string): string {
return item;
}
onChooseItem(item: string, evt: MouseEvent | KeyboardEvent): void {
this.selection = item;
this.plugin.runAutoMOC(itemTypes.Alias, item);
}
}
export default class AutoMOC extends Plugin {
settings: AutoMOCSettings;
getPresentLinks(currFilePath: string) {
const allFiles = this.app.metadataCache.resolvedLinks;
const presentLinks = Object.keys(allFiles[currFilePath]);
return presentLinks.sort();
}
getLinkedMentions(currFilePath: string) {
const allFiles = this.app.metadataCache.resolvedLinks;
let linkedMentions: Array<string> = [];
let ignoredFolders = this.settings.ignoredFolders
.trim()
.split(",")
.map((str) => str.trim().replace(/^\/|\/$/g, ""))
.filter((n) => n);
Object.keys(allFiles).forEach((key) => {
if (!ignoredFolders.some((path) => key.includes(path))) {
//check if file is in an ignored folder
if (currFilePath in allFiles[key]) {
linkedMentions.push(key);
}
}
});
return linkedMentions.sort();
}
getTaggedMentions(tag: string) {
const allFiles = this.app.metadataCache.resolvedLinks;
let taggedMentions: Array<string> = [];
const toCompare = tag.replace("#", "");
let ignoredFolders = this.settings.ignoredFolders
.trim()
.split(",")
.map((str) => str.trim().replace(/^\/|\/$/g, ""))
.filter((n) => n);
Object.keys(allFiles).forEach((key) => {
//check if file is in an ignored folder
if (!ignoredFolders.some((path) => key.includes(path))) {
const file = this.app.vault.getAbstractFileByPath(key);
if (file instanceof TFile) {
const body_tags = app.metadataCache.getFileCache(file).tags;
const frontmatter =
this.app.metadataCache.getFileCache(file).frontmatter;
if (body_tags) {
for (const tag of body_tags) {
if (tag["tag"].replace("#", "") === toCompare) {
taggedMentions.push(file.path);
}
}
}
if (frontmatter) {
let f_tags: Array<string> = [];
if (Array.isArray(frontmatter["tags"])) {
f_tags = frontmatter["tags"];
for (const f_tag of f_tags) {
if (f_tag === toCompare) {
taggedMentions.push(file.path);
}
}
}
if (String.isString(frontmatter["tags"])) {
f_tags = frontmatter["tags"].split(", ");
for (const f_tag of f_tags) {
if (f_tag === toCompare) {
taggedMentions.push(file.path);
}
}
}
}
}
}
});
const uniqueTaggedMentions = taggedMentions.filter(
(value, index, array) => array.indexOf(value) === index
);
return uniqueTaggedMentions;
}
getAliasMentions(refAlias: string) {
const allFiles = this.app.metadataCache.resolvedLinks;
let aliasMentions: Array<string> = [];
let ignoredFolders = this.settings.ignoredFolders
.trim()
.split(",")
.map((str) => str.trim().replace(/^\/|\/$/g, ""))
.filter((n) => n);
Object.keys(allFiles).forEach((key) => {
//check if file is in an ignored folder
if (!ignoredFolders.some((path) => key.includes(path))) {
const file = this.app.vault.getAbstractFileByPath(key);
if (file instanceof TFile) {
const frontmatter =
this.app.metadataCache.getFileCache(file).frontmatter;
if (frontmatter) {
let aliases: Array<string> = [];
if (Array.isArray(frontmatter["aliases"])) {
aliases = frontmatter["aliases"];
for (const alias of aliases) {
if (alias === refAlias) {
aliasMentions.push(file.path);
}
}
}
if (String.isString(frontmatter["aliases"])) {
aliases = frontmatter["aliases"].split(", ");
for (const alias of aliases) {
if (alias === refAlias) {
aliasMentions.push(file.path);
}
}
}
}
}
}
});
const uniqueAliasMentions = aliasMentions.filter(
(value, index, array) => array.indexOf(value) === index
);
return uniqueAliasMentions;
}
async addMissingLinks(
activeFileView: MarkdownView,
presentLinks: Array<string>,
allLinkedMentions: Array<string>,
item?: string
) {
let addFlag = false;
// define list delimeters
let listChar = "";
let listIter = -1;
let importPrefix = "";
if (this.settings.importAsList === importListTypes.UnorderedList) {
listChar = "*";
importPrefix = listChar + " ";
} else if (this.settings.importAsList === importListTypes.CheckBox) {
listChar = "- [ ]";
importPrefix = listChar + " ";
} else if (this.settings.importAsList == importListTypes.OrderedList) {
listChar = this.settings.orderedListSeparator;
listIter = 1;
importPrefix = listIter + listChar + " ";
}
//checks for missing links and adds them
for (const path of allLinkedMentions) {
if (!presentLinks.includes(path)) {
const file = this.app.vault.getAbstractFileByPath(path);
if (file instanceof TFile) {
//check for aliases
const frontmatter =
this.app.metadataCache.getFileCache(file).frontmatter;
let alias = "";
if (
this.settings.linkWithAlias &&
frontmatter &&
Array.isArray(frontmatter["aliases"]) &&
frontmatter["aliases"].length > 0
) {
alias = frontmatter.aliases[0];
}
let closestHeading = "";
let allHeadings: Array<string> = [];
if (this.settings.linkToHeading) {
const headingsLocations =
await this.getHeadingsLocationsInFile(path);
const linkTagLocations =
await this.getItemLocationsInFile(
activeFileView,
path,
item
);
for (let i = 0; i < linkTagLocations.length; i++) {
closestHeading = this.determineClosestHeading(
headingsLocations,
linkTagLocations[i]
);
allHeadings.push(closestHeading);
}
}
if (allHeadings.length > 0) {
//if there is a closest heading, link to heading
for (let i = 0; i < allHeadings.length; i++) {
activeFileView.editor.replaceSelection(
importPrefix +
this.app.fileManager.generateMarkdownLink(
file,
activeFileView.file.path,
"#" + allHeadings[i],
alias
) +
"\n"
);
}
} else {
//otherwise just link to note without heading
activeFileView.editor.replaceSelection(
importPrefix +
this.app.fileManager.generateMarkdownLink(
file,
activeFileView.file.path,
undefined,
alias
) +
"\n"
);
}
if (
this.settings.importAsList ===
importListTypes.OrderedList
) {
listIter += 1;
importPrefix = listIter + listChar + " ";
}
addFlag = true;
}
}
}
if (this.settings.newLinksAddedNotice && addFlag)
new Notice("New links added to note");
else if (this.settings.noNewLinksNotice && !addFlag)
new Notice("No new links found");
}
async getHeadingsLocationsInFile(filePath: string) {
const file = this.app.vault.getAbstractFileByPath(filePath);
if (file instanceof TFile) {
const fileContent = await this.app.vault.read(file);
const lines = fileContent.split("\n");
const regXHeader = /#{1,6}\s.+(?=)/g;
let headings: Array<string> = [];
lines.forEach((line) => {
const match = line.match(regXHeader);
if (match) headings.push(match[0].replace(/#{1,6}\s/g, ""));
else headings.push("-1");
});
return headings;
}
}
async getItemLocationsInFile(
activeFileView: MarkdownView,
filePath: string,
item?: string
) {
const file = this.app.vault.getAbstractFileByPath(filePath);
if (file instanceof TFile) {
const fileContent = await this.app.vault.read(file);
const lines = fileContent.split("\n");
let lineContent: Array<string> = [];
const activeFileName = activeFileView.file.name.substring(
0,
activeFileView.file.name.length - 3
);
let toSearch = "";
if (!item) {
toSearch = "[[" + activeFileName + "]]"; // -3 in order to remove ".md" from filepath
} else toSearch = item;
lines.forEach((line) => {
if (line.includes(toSearch)) lineContent.push(toSearch);
else lineContent.push("-1");
});
let toReturn: Array<number> = [];
for (let i = 0; i < lineContent.length; i++) {
if (lineContent[i] === toSearch) toReturn.push(i);
}
return toReturn;
}
}
determineClosestHeading(
headingsLocations: Array<string>,
itemLocation: number
) {
let distances: Array<number> = [];
headingsLocations.forEach((item, index) => {
if (item != "-1") distances.push(Math.abs(index - itemLocation));
else distances.push(-1);
});
let minIndex = -1;
let minValue = Infinity;
for (let i = 0; i < distances.length; i += 1) {
if (distances[i] != -1) {
if (distances[i] < minValue) {
minIndex = i;
minValue = distances[i];
}
}
}
if (minIndex === itemLocation) {
headingsLocations[minIndex] = headingsLocations[minIndex]
.replace(/#/g, "")
.trim(); // need to explicitly remove all tags from name and trim trailing whitespaces
}
return headingsLocations[minIndex];
}
runAutoMOC(itemType: string, item?: string) {
if (!Object.values<string>(itemTypes).includes(itemType)) {
new Notice("Invalid itemType provided");
return;
}
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (view != null && view.file.extension === "md") {
if (this.settings.linkingMentionsNotice)
new Notice("Linking mentions");
const presentLinks = this.getPresentLinks(view.file.path); // links already in the document
let linkTagMentions: Array<string>;
if (itemType == itemTypes.Link) {
linkTagMentions = this.getLinkedMentions(view.file.path); // all linked mentions even those not present
} else if (itemType == itemTypes.Tag) {
linkTagMentions = this.getTaggedMentions(item); // tagged mentions are looked up by basename rather than path
} else if (itemType == itemTypes.Alias) {
linkTagMentions = this.getAliasMentions(item); // alias mentions are looked up by basename rather than path
}
this.addMissingLinks(view, presentLinks, linkTagMentions, item);
} else {
new Notice(
"Failed to link mentions, file type is not a markdown file"
);
}
}
async onload() {
await this.loadSettings();
if (this.settings.showRibbonButton) {
const ribbonIconEl = this.addRibbonIcon(
"sheets-in-box",
"AutoMOC",
(evt: MouseEvent) => {
this.runAutoMOC(itemTypes.Link);
}
);
}
this.addCommand({
id: "add-missing-linked-mentions",
name: "Add missing linked mentions at the cursor position",
editorCallback: (editor: Editor, view: MarkdownView) => {
this.runAutoMOC(itemTypes.Link);
},
});
this.addCommand({
id: "add-missing-notes-by-tag",
name: "Add missing notes with a specific tag at the current cursor location",
editorCallback: (editor: Editor, view: MarkdownView) => {
new TagSuggestModal(this.app, this).open();
},
});
this.addCommand({
id: "add-missing-notes-by-alias",
name: "Add missing notes with a specific alias at the current cursor location",
editorCallback: (editor: Editor, view: MarkdownView) => {
new AliasSuggestModal(this.app, this).open();
},
});
this.addSettingTab(new AutoMOCSettingTab(this.app, this));
}
onunload() {}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class AutoMOCSettingTab extends PluginSettingTab {
plugin: AutoMOC;
constructor(app: App, plugin: AutoMOC) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
//Functionality Settings
containerEl.createEl("h2", { text: "Functionality" });
new Setting(containerEl)
.setName("Show ribbon button")
.setDesc(
"Enable or disable the ribbon button for this plugin. You can still run the plugin with a hotkey (requires restart)"
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.showRibbonButton)
.onChange((showRibbonButton) => {
this.plugin.settings.showRibbonButton =
showRibbonButton;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Link to heading")
.setDesc(
"Creates the link to the heading closest to the link/tag. This is performed in a greedy manner"
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.linkToHeading)
.onChange((linkToHeading) => {
this.plugin.settings.linkToHeading = linkToHeading;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Create link with alias")
.setDesc(
"Creates the link with the first alias from the frontmatter"
)
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.linkWithAlias)
.onChange((linkWithAlias) => {
this.plugin.settings.linkWithAlias = linkWithAlias;
this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName("Import as list")
.setDesc(
"Choose whether to import the links directly or as a ordered/unordered list"
)
.addDropdown((dropdown) => {
dropdown
.addOption(importListTypes.Disabled, "Disabled")
.addOption(importListTypes.OrderedList, "Ordered List")
.addOption(importListTypes.UnorderedList, "Unordered List")
.addOption(importListTypes.CheckBox, "Checkbox")
.setValue(this.plugin.settings.importAsList)
.onChange((importValue) => {
this.plugin.settings.importAsList = importValue;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Ordered list separator character")
.setDesc(
'If "Import as List" is set to "Ordered List" - Set what character separates the number from the list item'
)
.addDropdown((dropdown) => {
dropdown
.addOption(orderedListDelimeters.Period, "Period")
.addOption(orderedListDelimeters.Parenthesis, "Parenthesis")
.setValue(this.plugin.settings.orderedListSeparator)
.onChange((value) => {
this.plugin.settings.orderedListSeparator = value;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("Ignore notes in folders")
.setDesc(
"Specify a comma separated list of folders whose note's backlinks will not be added when AutoMOC is run \
(start from the root of your vault)"
)
.addText((text) =>
text
.setPlaceholder("path1, path2, path 3...")
.setValue(this.plugin.settings.ignoredFolders)
.onChange(async (value) => {
this.plugin.settings.ignoredFolders = value;
await this.plugin.saveSettings();
})
);
//Notification Settings
containerEl.createEl("h2", { text: "Notifications" });
new Setting(containerEl)
.setName("Linking mentions")
.setDesc(
"Enable or disable notifications for when AutoMoc begins to run"
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.linkingMentionsNotice)
.onChange((value) => {
this.plugin.settings.linkingMentionsNotice = value;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("New links added")
.setDesc(
"Enable or disable notifications for when new links are added to a note"
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.newLinksAddedNotice)
.onChange((value) => {
this.plugin.settings.newLinksAddedNotice = value;
this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("No new links found")
.setDesc(
"Enable or disable notifications for when no new links are added to a note"
)
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.noNewLinksNotice)
.onChange((value) => {
this.plugin.settings.noNewLinksNotice = value;
this.plugin.saveSettings();
});
});
}
}