From e5fe53026c06ba884653431109f4774fd856c14b Mon Sep 17 00:00:00 2001 From: idanlib Date: Sat, 26 Jul 2025 13:57:41 +0300 Subject: [PATCH 1/2] code review 1 --- main.ts | 18 +++++++++++------- manifest.json | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/main.ts b/main.ts index e161d1a..4becbd9 100644 --- a/main.ts +++ b/main.ts @@ -15,7 +15,7 @@ export default class RedirectPlugin extends Plugin { settings: RedirectSettings; redirectsFolder: TFolder | null = null; - redirectRef = async (file: TFile) => { + handleFileOpen = async (file: TFile) => { await this.redirect(); }; @@ -60,7 +60,10 @@ export default class RedirectPlugin extends Plugin { ); if (currentFile.path === `_forwards/${currentFile.name}`) { - new Notice(`${currentFile.name} is in the _forwards folder.`, 2000); + new Notice( + `${currentFile.name} is already in the _forwards folder.`, + 2000 + ); return; } @@ -82,7 +85,7 @@ export default class RedirectPlugin extends Plugin { } private getTargetFile(currentFileContent: string): TFile | null { - const linkTextRegex = /::>\[\[(.*[\w\s]*)\]\]/i; + const linkTextRegex = /::>\[\[(.*?)\]\]/i; const targetNoteName = currentFileContent.match(linkTextRegex)?.at(1); if (!targetNoteName) { @@ -126,7 +129,7 @@ export default class RedirectPlugin extends Plugin { if (this.settings.openInNewTab) { // Turn off event handler to avoid opening the target note twice - this.app.workspace.off("file-open", this.redirectRef); + this.app.workspace.off("file-open", this.handleFileOpen); await this.app.workspace .getLeaf() @@ -134,7 +137,8 @@ export default class RedirectPlugin extends Plugin { active: this.settings.switchToNewTab, }); - this.app.workspace.on("file-open", this.redirectRef); + // Turn event handler on again + this.app.workspace.on("file-open", this.handleFileOpen); } setTimeout(async () => { @@ -167,7 +171,7 @@ export default class RedirectPlugin extends Plugin { await this.redirect(); }); - this.app.workspace.on("file-open", this.redirectRef); + this.app.workspace.on("file-open", this.handleFileOpen); this.addCommand({ id: "paste-redirect-syntax", @@ -187,7 +191,7 @@ export default class RedirectPlugin extends Plugin { } onunload() { - this.app.workspace.off("file-open", this.redirectRef); + this.app.workspace.off("file-open", this.handleFileOpen); } async loadSettings() { diff --git a/manifest.json b/manifest.json index a48a26b..785cc5d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "fast-forward-link", "name": "FastForwardLink", - "version": "1.0.0", + "version": "1.0.1", "minAppVersion": "0.15.0", "description": "Fast-forward multiple links to a single target note. Create custom link shorthands (like `ps` > `photoshop`) to create synonyms, streamline navigation, and keep your vault organized.", "author": "Idan Liberman", From 6b00b25ad8f05ad2d403155c297cee8394feaac6 Mon Sep 17 00:00:00 2001 From: idanlib Date: Sat, 26 Jul 2025 23:45:24 +0300 Subject: [PATCH 2/2] code review 2 --- main.ts | 66 ++++++++++++++++++++++++++++++++------------------- versions.json | 2 +- 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/main.ts b/main.ts index 4becbd9..bedf515 100644 --- a/main.ts +++ b/main.ts @@ -14,11 +14,19 @@ const DEFAULT_SETTINGS: RedirectSettings = { export default class RedirectPlugin extends Plugin { settings: RedirectSettings; redirectsFolder: TFolder | null = null; + isMovingNote = false; + isBypassRedirect = false; handleFileOpen = async (file: TFile) => { + if (this.isMovingNote) return; + + if (this.isBypassRedirect) { + this.isBypassRedirect = false; + return; + } + await this.redirect(); }; - async changeSettings(newTab: boolean, switchToTab: boolean): Promise { this.settings.openInNewTab = newTab; this.settings.switchToNewTab = switchToTab; @@ -31,7 +39,8 @@ export default class RedirectPlugin extends Plugin { "/_forwards" ); } catch (error) { - console.warn(error); + new Notice("Failed to create the `_forwards` folder."); + console.error(error); } } @@ -52,22 +61,20 @@ export default class RedirectPlugin extends Plugin { return; } + if (currentFile.path !== `_forwards/${currentFile.name}`) { + new Notice( + `Moving ${currentFile.name} to the _forwards folder.`, + 2000 + ); + await this.moveRedirectNote(currentFile); + } + await this.app.workspace.openLinkText( targetNoteFile.name, targetNoteFile.path, this.settings.openInNewTab, { active: this.settings.switchToNewTab } ); - - if (currentFile.path === `_forwards/${currentFile.name}`) { - new Notice( - `${currentFile.name} is already in the _forwards folder.`, - 2000 - ); - return; - } - - await this.moveRedirectNote(currentFile); } private async getCurrentFileContent( @@ -77,9 +84,8 @@ export default class RedirectPlugin extends Plugin { return await this.app.vault.read(currentFile); } catch (error) { new Notice(`Cannot read ${currentFile}.`); - console.error( - console.error(`Failed to read ${currentFile} content: `, error) - ); + console.error(`Failed to read ${currentFile} content: `, error); + return null; } } @@ -122,29 +128,30 @@ export default class RedirectPlugin extends Plugin { } try { + this.isMovingNote = true; const redirectingNoteInFolder = await this.app.vault.copy( redirectingNote, `/_forwards/${redirectingNote.name}` ); if (this.settings.openInNewTab) { - // Turn off event handler to avoid opening the target note twice - this.app.workspace.off("file-open", this.handleFileOpen); - - await this.app.workspace - .getLeaf() - .openFile(redirectingNoteInFolder, { + await this.app.workspace.openLinkText( + redirectingNoteInFolder.name, + redirectingNoteInFolder.path, + this.settings.openInNewTab, + { active: this.settings.switchToNewTab, - }); - - // Turn event handler on again - this.app.workspace.on("file-open", this.handleFileOpen); + } + ); } + // Delay deletion to avoid race condition where the original note is deleted before the new note is fully opened in the UI. setTimeout(async () => { await this.deleteNote(redirectingNote); + this.isMovingNote = false; }, 500); } catch (error) { + this.isMovingNote = false; console.error(error); } } @@ -188,6 +195,15 @@ export default class RedirectPlugin extends Plugin { } }, }); + + this.addCommand({ + id: "bypass-redirect", + name: "Bypass redirect to target note", + callback: () => { + this.isBypassRedirect = true; + new Notice("Bypassing redirects."); + }, + }); } onunload() { diff --git a/versions.json b/versions.json index 26382a1..df3d877 100644 --- a/versions.json +++ b/versions.json @@ -1,3 +1,3 @@ { - "1.0.0": "0.15.0" + "1.1.0": "0.15.0" }