From c58b60c1a931cf67c8c2952210e8bf8dcd8a2741 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Sat, 31 Dec 2022 16:43:38 -0600 Subject: [PATCH 1/2] Don't allow invalid links when check for vault consistency --- src/links-handler.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/links-handler.ts b/src/links-handler.ts index 0addd44..24ce360 100644 --- a/src/links-handler.ts +++ b/src/links-handler.ts @@ -114,9 +114,13 @@ export class LinksHandler { } - getFileByLink(link: string, owningNotePath: string): TFile { - link = link.replace(/#.+/, ''); - return this.app.metadataCache.getFirstLinkpathDest(link, owningNotePath); + getFileByLink(link: string, owningNotePath: string, allowInvalidLink: boolean = true): TFile { + link = this.splitLinkToPathAndSection(link).link; + if (allowInvalidLink) { + return this.app.metadataCache.getFirstLinkpathDest(link, owningNotePath); + } + let fullPath = this.getFullPathForLink(link, owningNotePath); + return this.getFileByPath(fullPath); } @@ -220,7 +224,7 @@ export class LinksHandler { if (this.checkIsCorrectWikiLink(link.original)) continue; - let file = this.getFileByLink(link.link, note.path); + let file = this.getFileByLink(link.link, note.path, false); if (!file) { if (!allLinks[note.path]) allLinks[note.path] = []; @@ -251,7 +255,7 @@ export class LinksHandler { if (this.checkIsCorrectWikiEmbed(embed.original)) continue; - let file = this.getFileByLink(embed.link, note.path); + let file = this.getFileByLink(embed.link, note.path, false); if (!file) { if (!allEmbeds[note.path]) allEmbeds[note.path] = []; @@ -320,7 +324,7 @@ export class LinksHandler { if (!li.hasSection) continue; - let file = this.getFileByLink(link.link, note.path); + let file = this.getFileByLink(link.link, note.path, false); if (file) { if (file.extension === "pdf" && li.section.startsWith("page=")) { continue; From d6443eff50fc54cc03bf2774bdf25cf61d5a0ad8 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Sat, 31 Dec 2022 16:44:23 -0600 Subject: [PATCH 2/2] Avoid inefficient file search loops --- src/links-handler.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/links-handler.ts b/src/links-handler.ts index 24ce360..2f20550 100644 --- a/src/links-handler.ts +++ b/src/links-handler.ts @@ -126,9 +126,7 @@ export class LinksHandler { getFileByPath(path: string): TFile { path = Utils.normalizePathForFile(path); - let files = this.app.vault.getFiles(); - let file = files.find(file => Utils.normalizePathForFile(file.path) === path); - return file; + return app.vault.getAbstractFileByPath(path) as TFile; }