Merge pull request #23 from IdanLib/code-review-1

code review 1
This commit is contained in:
Idan Liberman 2025-07-27 00:01:10 +03:00 committed by GitHub
commit 3eeee6f8fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 27 deletions

70
main.ts
View file

@ -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;
}
redirectRef = async (file: TFile) => {
await this.redirect();
};
async changeSettings(newTab: boolean, switchToTab: boolean): Promise<void> {
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,19 +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 in the _forwards folder.`, 2000);
return;
}
await this.moveRedirectNote(currentFile);
}
private async getCurrentFileContent(
@ -74,15 +84,14 @@ 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;
}
}
private getTargetFile(currentFileContent: string): TFile | null {
const linkTextRegex = /::>\[\[(.*[\w\s]*)\]\]/i;
const linkTextRegex = /::>\[\[(.*?)\]\]/i;
const targetNoteName = currentFileContent.match(linkTextRegex)?.at(1);
if (!targetNoteName) {
@ -119,28 +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.redirectRef);
await this.app.workspace
.getLeaf()
.openFile(redirectingNoteInFolder, {
await this.app.workspace.openLinkText(
redirectingNoteInFolder.name,
redirectingNoteInFolder.path,
this.settings.openInNewTab,
{
active: this.settings.switchToNewTab,
});
this.app.workspace.on("file-open", this.redirectRef);
}
);
}
// 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);
}
}
@ -167,7 +178,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",
@ -184,10 +195,19 @@ 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() {
this.app.workspace.off("file-open", this.redirectRef);
this.app.workspace.off("file-open", this.handleFileOpen);
}
async loadSettings() {

View file

@ -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",

View file

@ -1,3 +1,3 @@
{
"1.0.0": "0.15.0"
"1.1.0": "0.15.0"
}