diff --git a/src/links-handler.ts b/src/links-handler.ts index 0addd44..2f20550 100644 --- a/src/links-handler.ts +++ b/src/links-handler.ts @@ -114,17 +114,19 @@ 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); } 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; } @@ -220,7 +222,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 +253,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 +322,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;