From ff767812a3fd8e39781340b5b66909d07684c019 Mon Sep 17 00:00:00 2001 From: nxe Date: Tue, 24 May 2022 13:20:30 +0100 Subject: [PATCH 1/6] Root files --- main.ts | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/main.ts b/main.ts index d7c9623..b82c7e1 100644 --- a/main.ts +++ b/main.ts @@ -4,14 +4,16 @@ interface WaypointSettings { waypointFlag: string stopScanAtFolderNotes: boolean, showFolderNotes: boolean, - debugLogging: boolean + debugLogging: boolean, + root: string } const DEFAULT_SETTINGS: WaypointSettings = { waypointFlag: "%% Waypoint %%", stopScanAtFolderNotes: false, showFolderNotes: false, - debugLogging: false + debugLogging: false, + root: null } export default class Waypoint extends Plugin { @@ -72,10 +74,12 @@ export default class Waypoint extends Plugin { this.log("Found waypoint flag in folder note!"); await this.updateWaypoint(file); await this.updateParentWaypoint(file.parent, false); - return; + return; } else if (file.parent.isRoot()) { this.log("Found waypoint flag in root folder."); - this.printWaypointError(file, `%% Error: Cannot create a waypoint in the root folder of your vault. For more information, check the instructions [here](https://github.com/IdreesInc/Waypoint) %%`); + this.settings.root = file.name; + await this.saveSettings(); + await this.updateWaypoint(file); return; } else { this.log("Found waypoint flag in invalid note."); @@ -112,7 +116,12 @@ export default class Waypoint extends Plugin { */ async updateWaypoint(file: TFile) { this.log("Updating waypoint in " + file.path); - const fileTree = await this.getFileTreeRepresentation(file.parent, 0, true); + let fileTree = await this.getFileTreeRepresentation(file.parent, 0, true); + if (file.parent.isRoot()) { + let splitFileTree = fileTree.split("\n"); + fileTree = `- **[[${file.basename}]]**\n${splitFileTree.slice(1).join("\n")}` + } + console.log(fileTree); const waypoint = `${Waypoint.BEGIN_WAYPOINT}\n${fileTree}\n\n${Waypoint.END_WAYPOINT}`; const text = await this.app.vault.read(file); const lines: string[] = text.split("\n"); @@ -146,7 +155,8 @@ export default class Waypoint extends Plugin { async getFileTreeRepresentation(node: TAbstractFile, indentLevel: number, topLevel = false): Promise|null { const bullet = " ".repeat(indentLevel) + "-"; if (node instanceof TFile) { - if (node.path.endsWith(".md")) { + // Chack for the parent being the root because otherwise the "root note" would be included in the tree + if (node.path.endsWith(".md") && !node.parent.isRoot()) { return `${bullet} [[${node.basename}]]`; } return null; @@ -227,17 +237,29 @@ export default class Waypoint extends Plugin { async locateParentWaypoint(node: TAbstractFile, includeCurrentNode: boolean): Promise { this.log("Locating parent waypoint of " + node.name); let folder = includeCurrentNode ? node : node.parent; - while (folder) { - const folderNote = this.app.vault.getAbstractFileByPath(folder.path + "/" + folder.name + ".md"); - if (folderNote instanceof TFile) { - this.log("Found folder note: " + folderNote.path); - const text = await this.app.vault.cachedRead(folderNote); + if (node.parent.isRoot() && this.settings.root !== null) { + const file = this.app.vault.getAbstractFileByPath(this.settings.root) + if (file instanceof TFile) { + this.log("Found folder note: " + file.path); + const text = await this.app.vault.cachedRead(file); if (text.includes(Waypoint.BEGIN_WAYPOINT) || text.includes(this.settings.waypointFlag)) { this.log("Found parent waypoint!"); - return folderNote; + return file; + } + } + } else { + while (folder) { + const folderNote = this.app.vault.getAbstractFileByPath(folder.path + "/" + folder.name + ".md"); + if (folderNote instanceof TFile) { + this.log("Found folder note: " + folderNote.path); + const text = await this.app.vault.cachedRead(folderNote); + if (text.includes(Waypoint.BEGIN_WAYPOINT) || text.includes(this.settings.waypointFlag)) { + this.log("Found parent waypoint!"); + return folderNote; + } } + folder = folder.parent; } - folder = folder.parent; } this.log("No parent waypoint found."); return null; From 49197fd31bd5aee208cf7f9c2a768e0c3a4b254b Mon Sep 17 00:00:00 2001 From: nxe Date: Tue, 24 May 2022 13:20:54 +0100 Subject: [PATCH 2/6] ignored files --- main.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/main.ts b/main.ts index b82c7e1..c0f35df 100644 --- a/main.ts +++ b/main.ts @@ -5,6 +5,7 @@ interface WaypointSettings { stopScanAtFolderNotes: boolean, showFolderNotes: boolean, debugLogging: boolean, + ignoredFolders: string[], root: string } @@ -13,6 +14,7 @@ const DEFAULT_SETTINGS: WaypointSettings = { stopScanAtFolderNotes: false, showFolderNotes: false, debugLogging: false, + ignoredFolders: ["Templates"], root: null } @@ -161,6 +163,7 @@ export default class Waypoint extends Plugin { } return null; } else if (node instanceof TFolder) { + if (this.settings.ignoredFolders.includes(node.path)) { return null } let text = `${bullet} **${node.name}**`; const folderNote = this.app.vault.getAbstractFileByPath(node.path + "/" + node.name + ".md"); if (folderNote instanceof TFile) { @@ -342,6 +345,28 @@ class WaypointSettingsTab extends PluginSettingTab { await this.plugin.saveSettings(); }) ); + new Setting(containerEl) + .setName("Ignored folders") + .setDesc("Folders that Waypoint should ignore") + .addText(text => text + .setPlaceholder(DEFAULT_SETTINGS.ignoredFolders.join(",")) + .setValue(this.plugin.settings.ignoredFolders.join(", ")) + .onChange(async (value) => { + let previous = this.plugin.settings.ignoredFolders; + this.plugin.settings.ignoredFolders = value.split(/\s*,\s*/); + await this.plugin.saveSettings(); + + // Get a list of all new and old folders that need updating + let allFolders = [...new Set([...previous, ...this.plugin.settings.ignoredFolders])] + for (let i = 0; i < allFolders.length; i++) { + let file = this.app.vault.getAbstractFileByPath(allFolders[i]); + if (file === null) { continue } + await this.plugin.locateParentWaypoint(file, false).then((file) => { + if (file !== null) { this.plugin.updateWaypoint(file) } + }) + } + }) + ); const postscriptElement = containerEl.createEl("div", { cls: "setting-item", }); From 568f1cb23b26ed72d4a7fdd9e7a4212db7388549 Mon Sep 17 00:00:00 2001 From: nxe Date: Tue, 24 May 2022 13:21:09 +0100 Subject: [PATCH 3/6] update waypoint hotkey --- main.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/main.ts b/main.ts index c0f35df..8b1e38b 100644 --- a/main.ts +++ b/main.ts @@ -56,6 +56,17 @@ export default class Waypoint extends Plugin { // This adds a settings tab so the user can configure various aspects of the plugin this.addSettingTab(new WaypointSettingsTab(this.app, this)); + + // Add in a hotkey to update the waypoint + this.addCommand({ + id: "update-waypoint", + name: "Update waypoint in current file", + hotkeys: [{ + modifiers: ["Ctrl"], + key: "w" + }], + callback: () => { this.updateWaypoint(this.app.workspace.getActiveFile()) } + }) } onunload() { From eceaa837176e8002b13c30a84fd505064ed10b17 Mon Sep 17 00:00:00 2001 From: nxe Date: Tue, 24 May 2022 13:21:22 +0100 Subject: [PATCH 4/6] formatting --- main.ts | 6 +++--- package-lock.json | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/main.ts b/main.ts index 8b1e38b..5ea1798 100644 --- a/main.ts +++ b/main.ts @@ -197,8 +197,8 @@ export default class Waypoint extends Plugin { }).filter(child => this.settings.showFolderNotes || child.name !== node.name + ".md"); if (children.length > 0) { text += "\n" + (await Promise.all(children.map(child => this.getFileTreeRepresentation(child, indentLevel + 1)))) - .filter(Boolean) - .join("\n"); + .filter(Boolean) + .join("\n"); } return text; } else { @@ -295,7 +295,7 @@ export default class Waypoint extends Plugin { log(message: string) { if (this.settings.debugLogging) { - console.log(message); + console.log(message); } } diff --git a/package-lock.json b/package-lock.json index 5801987..88445a8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "waypoint", - "version": "1.0.1", + "version": "1.1.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "waypoint", - "version": "1.0.1", + "version": "1.1.0", "license": "MIT", "devDependencies": { "@types/codemirror": "^5.60.5", From 7e141c5f1f905c1b92b4df9b873a83b8dc6a561d Mon Sep 17 00:00:00 2001 From: nxe Date: Tue, 24 May 2022 16:01:31 +0100 Subject: [PATCH 5/6] linting fix and removed left over `console.log` --- main.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/main.ts b/main.ts index 5ea1798..a2c8ba6 100644 --- a/main.ts +++ b/main.ts @@ -131,10 +131,9 @@ export default class Waypoint extends Plugin { this.log("Updating waypoint in " + file.path); let fileTree = await this.getFileTreeRepresentation(file.parent, 0, true); if (file.parent.isRoot()) { - let splitFileTree = fileTree.split("\n"); + const splitFileTree = fileTree.split("\n"); fileTree = `- **[[${file.basename}]]**\n${splitFileTree.slice(1).join("\n")}` } - console.log(fileTree); const waypoint = `${Waypoint.BEGIN_WAYPOINT}\n${fileTree}\n\n${Waypoint.END_WAYPOINT}`; const text = await this.app.vault.read(file); const lines: string[] = text.split("\n"); @@ -168,7 +167,7 @@ export default class Waypoint extends Plugin { async getFileTreeRepresentation(node: TAbstractFile, indentLevel: number, topLevel = false): Promise|null { const bullet = " ".repeat(indentLevel) + "-"; if (node instanceof TFile) { - // Chack for the parent being the root because otherwise the "root note" would be included in the tree + // Check for the parent being the root because otherwise the "root note" would be included in the tree if (node.path.endsWith(".md") && !node.parent.isRoot()) { return `${bullet} [[${node.basename}]]`; } @@ -363,14 +362,14 @@ class WaypointSettingsTab extends PluginSettingTab { .setPlaceholder(DEFAULT_SETTINGS.ignoredFolders.join(",")) .setValue(this.plugin.settings.ignoredFolders.join(", ")) .onChange(async (value) => { - let previous = this.plugin.settings.ignoredFolders; + const previous = this.plugin.settings.ignoredFolders; this.plugin.settings.ignoredFolders = value.split(/\s*,\s*/); await this.plugin.saveSettings(); // Get a list of all new and old folders that need updating - let allFolders = [...new Set([...previous, ...this.plugin.settings.ignoredFolders])] + const allFolders = [...new Set([...previous, ...this.plugin.settings.ignoredFolders])] for (let i = 0; i < allFolders.length; i++) { - let file = this.app.vault.getAbstractFileByPath(allFolders[i]); + const file = this.app.vault.getAbstractFileByPath(allFolders[i]); if (file === null) { continue } await this.plugin.locateParentWaypoint(file, false).then((file) => { if (file !== null) { this.plugin.updateWaypoint(file) } From cd5ffcc49bc4b66b2c66a4d21193a59a6f3b92d7 Mon Sep 17 00:00:00 2001 From: Mara Date: Thu, 4 Aug 2022 21:26:18 +0200 Subject: [PATCH 6/6] feat: support for outside folder note strategies see IdreesInc/Waypoint#19 --- main.ts | 29 ++++++++++++++++++++++++----- package-lock.json | 4 ++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/main.ts b/main.ts index 27d6c85..0b36bf6 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { App, debounce, Plugin, PluginSettingTab, Setting, TAbstractFile, TFile, TFolder } from 'obsidian'; +import {App, debounce, Plugin, PluginSettingTab, Setting, TAbstractFile, TFile, TFolder, Vault} from 'obsidian'; interface WaypointSettings { waypointFlag: string @@ -29,7 +29,8 @@ export default class Waypoint extends Plugin { // Register events after layout is built to avoid initial wave of 'create' events this.registerEvent(this.app.vault.on("create", (file) => { this.log("create " + file.name); - this.foldersWithChanges.add(file.parent); + const folderParent = this.getParentFolder(file.path); + this.foldersWithChanges.add(folderParent); this.scheduleUpdate(); })); this.registerEvent(this.app.vault.on("delete", (file) => { @@ -42,7 +43,8 @@ export default class Waypoint extends Plugin { })); this.registerEvent(this.app.vault.on("rename", (file, oldPath) => { this.log("rename " + file.name); - this.foldersWithChanges.add(file.parent); + const parentFolderOrigin = this.getParentFolder(file.path); + this.foldersWithChanges.add(parentFolderOrigin); const parentFolder = this.getParentFolder(oldPath); if (parentFolder !== null) { this.foldersWithChanges.add(parentFolder); @@ -74,7 +76,11 @@ export default class Waypoint extends Plugin { this.log("Found waypoint flag in folder note!"); await this.updateWaypoint(file); await this.updateParentWaypoint(file.parent, false); - return; + return; + } else if (this.outsideFolderPath(file)) { + await this.updateWaypoint(file); + await this.updateParentWaypoint(this.outsideFolderPath(file), false); + return; } else if (file.parent.isRoot()) { this.log("Found waypoint flag in root folder."); this.printWaypointError(file, `%% Error: Cannot create a waypoint in the root folder of your vault. For more information, check the instructions [here](https://github.com/IdreesInc/Waypoint) %%`); @@ -89,6 +95,14 @@ export default class Waypoint extends Plugin { this.log("No waypoint flags found."); } + outsideFolderPath(file:TFile) { + const folderNotePath = this.app.vault.getAbstractFileByPath(file.path.replace('.md', '')); + if (folderNotePath && folderNotePath instanceof TFolder) { + return folderNotePath; + } + return null; + } + async printWaypointError(file: TFile, error: string) { this.log("Creating waypoint error in " + file.path); const text = await this.app.vault.read(file); @@ -114,7 +128,8 @@ export default class Waypoint extends Plugin { */ async updateWaypoint(file: TFile) { this.log("Updating waypoint in " + file.path); - const fileTree = await this.getFileTreeRepresentation(file.parent, 0, true); + const parentFolder = this.getParentFolder(file.path); + const fileTree = await this.getFileTreeRepresentation(parentFolder, 0, true); const waypoint = `${Waypoint.BEGIN_WAYPOINT}\n${fileTree}\n\n${Waypoint.END_WAYPOINT}`; const text = await this.app.vault.read(file); const lines: string[] = text.split("\n"); @@ -236,6 +251,7 @@ export default class Waypoint extends Plugin { */ async locateParentWaypoint(node: TAbstractFile, includeCurrentNode: boolean): Promise { this.log("Locating parent waypoint of " + node.name); + console.log("Locating parent waypoint of " + node.name); let folder = includeCurrentNode ? node : node.parent; while (folder) { const folderNote = this.app.vault.getAbstractFileByPath(folder.path + "/" + folder.name + ".md"); @@ -260,8 +276,11 @@ export default class Waypoint extends Plugin { */ getParentFolder(path: string): TFolder { const abstractFile = this.app.vault.getAbstractFileByPath(path.split("/").slice(0, -1).join("/")); + const abstractOutsideFolder = this.app.vault.getAbstractFileByPath(path.replace('.md', '')); if (abstractFile instanceof TFolder) { return abstractFile; + } else if (abstractOutsideFolder instanceof TFolder) { + return abstractOutsideFolder; } else { return null; } diff --git a/package-lock.json b/package-lock.json index 88445a8..22cbe35 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "waypoint", - "version": "1.1.0", + "version": "1.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "waypoint", - "version": "1.1.0", + "version": "1.2.0", "license": "MIT", "devDependencies": { "@types/codemirror": "^5.60.5",