From d690f50b6e39c844b27f7c600f393c31ed4f644c Mon Sep 17 00:00:00 2001 From: IdanL Date: Thu, 31 Oct 2024 21:37:04 +0200 Subject: [PATCH 1/2] test --- main.ts | 67 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/main.ts b/main.ts index a4416fa..b04a59d 100644 --- a/main.ts +++ b/main.ts @@ -26,21 +26,24 @@ export default class RedirectPlugin extends Plugin { private async createRedirectsFolder() { const currentRedirectsFolder = - this.app.vault.getAbstractFileByPath("/_forwards"); + this.app.vault.getAbstractFileByPath("_forwards"); - if (!currentRedirectsFolder) { - try { - this.redirectsFolder = await this.app.vault.createFolder( - "/_forwards" - ); - } catch (error) { - console.warn(error); - } - } else { - new Notice("_forwards folder found.", 2000); + console.log("currentRedirectsFolder: ", currentRedirectsFolder); + + // if (!currentRedirectsFolder) { + try { + this.redirectsFolder = await this.app.vault.createFolder( + "/_forwards" + ); + } catch (error) { + console.warn(error); } + // } else { + // new Notice("_forwards folder found.", 2000); + // } this.redirectsFolder = currentRedirectsFolder as TFolder; + console.log("this.redirectsFolder: ", this.redirectsFolder); } private async redirect() { @@ -49,12 +52,17 @@ export default class RedirectPlugin extends Plugin { if (!currentFile) { return; } + + console.log("currentFile", currentFile); + const currentFileContent = (await this.getCurrentFileContent( currentFile )) as string; const targetNoteFile = this.getTargetFile(currentFileContent); + console.log("targetNoteFile", targetNoteFile); + if (!targetNoteFile) { return; } @@ -113,6 +121,8 @@ export default class RedirectPlugin extends Plugin { private async moveRedirectNote( redirectingNote: TFile | null ): Promise { + let redirectingNoteInFolder = null; + if (!redirectingNote) { return; } @@ -121,23 +131,36 @@ export default class RedirectPlugin extends Plugin { await this.createRedirectsFolder(); } + if (redirectingNote.path === `_forwards/${redirectingNote.name}`) { + new Notice( + `${redirectingNote.name} is in the _forwards folder.`, + 2000 + ); + return; + } + try { await this.app.vault.copy( redirectingNote, `/_forwards/${redirectingNote.name}` ); + redirectingNoteInFolder = await this.deleteNote(redirectingNote); } catch (error) { + console.log("no need to move redirecting note"); + console.warn(error); } - const redirectingNoteInFolder = await this.deleteNote(redirectingNote); - if (!redirectingNoteInFolder) { return; } // Turn off event handler to avoid opening the target note twice this.app.workspace.off("file-open", this.redirectRef); + console.log( + "opening redirecting note in folder: ", + redirectingNoteInFolder + ); await this.app.workspace.openLinkText( redirectingNoteInFolder.name, @@ -152,15 +175,15 @@ export default class RedirectPlugin extends Plugin { private async deleteNote(orgRedirectingNote: TFile): Promise { let updatedRedirectingNote = orgRedirectingNote; - if ( - orgRedirectingNote.path === `_forwards/${orgRedirectingNote.name}` - ) { - new Notice( - `${orgRedirectingNote.name} is in the _forwards folder.`, - 2000 - ); - return; - } + // if ( + // orgRedirectingNote.path === `_forwards/${orgRedirectingNote.name}` + // ) { + // new Notice( + // `${orgRedirectingNote.name} is in the _forwards folder.`, + // 2000 + // ); + // return; + // } if (this.settings.openInNewTab) { updatedRedirectingNote = Object.create(orgRedirectingNote); From 8466069a6a76418f563ad5540d94312bccd4eca4 Mon Sep 17 00:00:00 2001 From: IdanL Date: Thu, 31 Oct 2024 22:53:08 +0200 Subject: [PATCH 2/2] refactor code, testing --- main.ts | 78 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/main.ts b/main.ts index b04a59d..e69d133 100644 --- a/main.ts +++ b/main.ts @@ -30,7 +30,6 @@ export default class RedirectPlugin extends Plugin { console.log("currentRedirectsFolder: ", currentRedirectsFolder); - // if (!currentRedirectsFolder) { try { this.redirectsFolder = await this.app.vault.createFolder( "/_forwards" @@ -38,9 +37,6 @@ export default class RedirectPlugin extends Plugin { } catch (error) { console.warn(error); } - // } else { - // new Notice("_forwards folder found.", 2000); - // } this.redirectsFolder = currentRedirectsFolder as TFolder; console.log("this.redirectsFolder: ", this.redirectsFolder); @@ -74,6 +70,11 @@ export default class RedirectPlugin extends Plugin { { active: this.settings.switchToNewTab } ); + if (currentFile.path === `_forwards/${currentFile.name}`) { + new Notice(`${currentFile.name} is in the _forwards folder.`, 2000); + return; + } + await this.moveRedirectNote(currentFile); } @@ -131,49 +132,50 @@ export default class RedirectPlugin extends Plugin { await this.createRedirectsFolder(); } - if (redirectingNote.path === `_forwards/${redirectingNote.name}`) { - new Notice( - `${redirectingNote.name} is in the _forwards folder.`, - 2000 - ); - return; - } + // if (redirectingNote.path === `_forwards/${redirectingNote.name}`) { + // new Notice( + // `${redirectingNote.name} is in the _forwards folder.`, + // 2000 + // ); + // return; + // } try { await this.app.vault.copy( redirectingNote, `/_forwards/${redirectingNote.name}` ); - redirectingNoteInFolder = await this.deleteNote(redirectingNote); - } catch (error) { - console.log("no need to move redirecting note"); + // Turn off event handler to avoid opening the target note twice + this.app.workspace.off("file-open", this.redirectRef); + if (this.settings.openInNewTab) { + redirectingNoteInFolder = Object.create(redirectingNote); + redirectingNoteInFolder.path = `_forwards/${redirectingNote.path}`; + + await this.app.workspace.openLinkText( + redirectingNoteInFolder.name, + redirectingNoteInFolder.path + ); + + this.app.workspace.on("file-open", this.redirectRef); + } + + await this.deleteNote(redirectingNote); + + // redirectingNoteInFolder = await this.deleteNote(redirectingNote); + } catch (error) { console.warn(error); } - if (!redirectingNoteInFolder) { - return; - } + // if (!redirectingNoteInFolder) { + // return; + // } - // Turn off event handler to avoid opening the target note twice - this.app.workspace.off("file-open", this.redirectRef); - console.log( - "opening redirecting note in folder: ", - redirectingNoteInFolder - ); - - await this.app.workspace.openLinkText( - redirectingNoteInFolder.name, - redirectingNoteInFolder.path - ); - - this.app.workspace.on("file-open", this.redirectRef); - - return redirectingNoteInFolder; + // return redirectingNoteInFolder; } private async deleteNote(orgRedirectingNote: TFile): Promise { - let updatedRedirectingNote = orgRedirectingNote; + // let updatedRedirectingNote = orgRedirectingNote; // if ( // orgRedirectingNote.path === `_forwards/${orgRedirectingNote.name}` @@ -185,10 +187,10 @@ export default class RedirectPlugin extends Plugin { // return; // } - if (this.settings.openInNewTab) { - updatedRedirectingNote = Object.create(orgRedirectingNote); - updatedRedirectingNote.path = `_forwards/${orgRedirectingNote.path}`; - } + // if (this.settings.openInNewTab) { + // updatedRedirectingNote = Object.create(orgRedirectingNote); + // updatedRedirectingNote.path = `_forwards/${orgRedirectingNote.path}`; + // } try { await this.app.vault.delete(orgRedirectingNote); @@ -200,7 +202,7 @@ export default class RedirectPlugin extends Plugin { console.error(error); } - return updatedRedirectingNote; + // return updatedRedirectingNote; } async onload() {