Skip to content

Commit 358169b

Browse files
committed
feat: add support for root level note
- pull in changes from RosiePuddles, but apply to recent code
1 parent 9bfdaa1 commit 358169b

1 file changed

Lines changed: 115 additions & 25 deletions

File tree

main.ts

Lines changed: 115 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ interface WaypointSettings {
2323
useWikiLinks: boolean;
2424
showEnclosingNote: boolean;
2525
folderNoteType: string;
26+
ignoredFolders: string[];
27+
root: string;
2628
}
2729

2830
const DEFAULT_SETTINGS: WaypointSettings = {
@@ -34,6 +36,8 @@ const DEFAULT_SETTINGS: WaypointSettings = {
3436
useWikiLinks: true,
3537
showEnclosingNote: false,
3638
folderNoteType: FolderNoteType.InsideFolder,
39+
ignoredFolders: ["Templates"],
40+
root: null,
3741
};
3842

3943
export default class Waypoint extends Plugin {
@@ -82,6 +86,21 @@ export default class Waypoint extends Plugin {
8286

8387
// This adds a settings tab so the user can configure various aspects of the plugin
8488
this.addSettingTab(new WaypointSettingsTab(this.app, this));
89+
90+
// Add in a hotkey to update the waypoint
91+
this.addCommand({
92+
id: "update-waypoint",
93+
name: "Update waypoint in current file",
94+
hotkeys: [
95+
{
96+
modifiers: ["Ctrl"],
97+
key: "w",
98+
},
99+
],
100+
callback: () => {
101+
this.updateWaypoint(this.app.workspace.getActiveFile());
102+
},
103+
});
85104
}
86105

87106
onunload() {}
@@ -108,10 +127,9 @@ export default class Waypoint extends Plugin {
108127
return;
109128
} else if (file.parent.isRoot()) {
110129
this.log("Found waypoint flag in root folder.");
111-
this.printWaypointError(
112-
file,
113-
`%% Error: Cannot create a waypoint in the root folder of your vault. For more information, check the instructions [here](https://github.com/IdreesInc/Waypoint) %%`
114-
);
130+
this.settings.root = file.name;
131+
await this.saveSettings();
132+
await this.updateWaypoint(file);
115133
return;
116134
} else {
117135
this.log("Found waypoint flag in invalid note.");
@@ -201,6 +219,12 @@ export default class Waypoint extends Plugin {
201219
);
202220
}
203221
}
222+
if (file.parent.isRoot()) {
223+
const splitFileTree = fileTree.split("\n");
224+
fileTree = `- **[[${file.basename}]]**\n${splitFileTree
225+
.slice(1)
226+
.join("\n")}`;
227+
}
204228
const waypoint = `${Waypoint.BEGIN_WAYPOINT}\n${fileTree}\n\n${Waypoint.END_WAYPOINT}`;
205229
const text = await this.app.vault.read(file);
206230
const lines: string[] = text.split("\n");
@@ -255,7 +279,8 @@ export default class Waypoint extends Plugin {
255279
if (node instanceof TFile) {
256280
console.log(node);
257281
// Print the file name
258-
if (node.extension == "md") {
282+
// Check for the parent being the root because otherwise the "root note" would be included in the tree
283+
if (node.extension == "md" && !node.parent.isRoot()) {
259284
if (this.settings.useWikiLinks) {
260285
return `${bullet} [[${node.basename}]]`;
261286
} else {
@@ -276,6 +301,9 @@ export default class Waypoint extends Plugin {
276301
}
277302
return null;
278303
} else if (node instanceof TFolder) {
304+
if (this.settings.ignoredFolders.includes(node.path)) {
305+
return null;
306+
}
279307
let text = "";
280308
if (!topLevel || this.settings.showEnclosingNote) {
281309
// Print the folder name
@@ -447,33 +475,56 @@ export default class Waypoint extends Plugin {
447475
): Promise<TFile> {
448476
this.log("Locating parent waypoint of " + node.name);
449477
let folder = includeCurrentNode ? node : node.parent;
450-
while (folder) {
451-
let folderNote;
452-
if (this.settings.folderNoteType === FolderNoteType.InsideFolder) {
453-
folderNote = this.app.vault.getAbstractFileByPath(
454-
folder.path + "/" + folder.name + ".md"
455-
);
456-
} else if (
457-
this.settings.folderNoteType === FolderNoteType.OutsideFolder
458-
) {
459-
if (folder.parent) {
460-
folderNote = this.app.vault.getAbstractFileByPath(
461-
this.getCleanParentPath(folder) + folder.name + ".md"
462-
);
463-
}
464-
}
465-
if (folderNote instanceof TFile) {
466-
this.log("Found folder note: " + folderNote.path);
467-
const text = await this.app.vault.cachedRead(folderNote);
478+
// When there's a root-level folder note
479+
if (node.parent.isRoot() && this.settings.root !== null) {
480+
const file = this.app.vault.getAbstractFileByPath(
481+
this.settings.root
482+
);
483+
if (file instanceof TFile) {
484+
this.log("Found folder note: " + file.path);
485+
const text = await this.app.vault.cachedRead(file);
468486
if (
469487
text.includes(Waypoint.BEGIN_WAYPOINT) ||
470488
text.includes(this.settings.waypointFlag)
471489
) {
472490
this.log("Found parent waypoint!");
473-
return folderNote;
491+
return file;
474492
}
475493
}
476-
folder = folder.parent;
494+
} else {
495+
while (folder) {
496+
let folderNote;
497+
if (
498+
this.settings.folderNoteType === FolderNoteType.InsideFolder
499+
) {
500+
folderNote = this.app.vault.getAbstractFileByPath(
501+
folder.path + "/" + folder.name + ".md"
502+
);
503+
} else if (
504+
this.settings.folderNoteType ===
505+
FolderNoteType.OutsideFolder
506+
) {
507+
if (folder.parent) {
508+
folderNote = this.app.vault.getAbstractFileByPath(
509+
this.getCleanParentPath(folder) +
510+
folder.name +
511+
".md"
512+
);
513+
}
514+
}
515+
if (folderNote instanceof TFile) {
516+
this.log("Found folder note: " + folderNote.path);
517+
const text = await this.app.vault.cachedRead(folderNote);
518+
if (
519+
text.includes(Waypoint.BEGIN_WAYPOINT) ||
520+
text.includes(this.settings.waypointFlag)
521+
) {
522+
this.log("Found parent waypoint!");
523+
return folderNote;
524+
}
525+
}
526+
folder = folder.parent;
527+
}
477528
}
478529
this.log("No parent waypoint found.");
479530
return null;
@@ -639,6 +690,45 @@ class WaypointSettingsTab extends PluginSettingTab {
639690
await this.plugin.saveSettings();
640691
})
641692
);
693+
new Setting(containerEl)
694+
.setName("Ignored folders")
695+
.setDesc("Folders that Waypoint should ignore")
696+
.addText((text) =>
697+
text
698+
.setPlaceholder(DEFAULT_SETTINGS.ignoredFolders.join(","))
699+
.setValue(this.plugin.settings.ignoredFolders.join(", "))
700+
.onChange(async (value) => {
701+
const previous = this.plugin.settings.ignoredFolders;
702+
this.plugin.settings.ignoredFolders =
703+
value.split(/\s*,\s*/);
704+
await this.plugin.saveSettings();
705+
706+
// Get a list of all new and old folders that need updating
707+
const allFolders = [
708+
...new Set([
709+
...previous,
710+
...this.plugin.settings.ignoredFolders,
711+
]),
712+
];
713+
714+
// Trigger updates in order to remove those folders from existing waypoints
715+
for (let i = 0; i < allFolders.length; i++) {
716+
const file = this.app.vault.getAbstractFileByPath(
717+
allFolders[i]
718+
);
719+
if (file === null) {
720+
continue;
721+
}
722+
await this.plugin
723+
.locateParentWaypoint(file, false)
724+
.then((file) => {
725+
if (file !== null) {
726+
this.plugin.updateWaypoint(file);
727+
}
728+
});
729+
}
730+
})
731+
);
642732
const postscriptElement = containerEl.createEl("div", {
643733
cls: "setting-item",
644734
});

0 commit comments

Comments
 (0)