diff --git a/cspell.json b/cspell.json index 8664e21..e35c014 100644 --- a/cspell.json +++ b/cspell.json @@ -78,6 +78,7 @@ "tsdoc", "tsdocs", "Unescapes", + "unregisters", "vararg", "varargs", "vectorize", diff --git a/src/obsidian/MetadataCache.ts b/src/obsidian/MetadataCache.ts index 161678f..f3bcf51 100644 --- a/src/obsidian/MetadataCache.ts +++ b/src/obsidian/MetadataCache.ts @@ -9,6 +9,8 @@ import type { LinkCache, MarkdownView, ReferenceCache, + TAbstractFile, + Vault, } from "obsidian"; import { retryWithTimeout, @@ -119,7 +121,12 @@ export async function getBacklinksForFileSafe(app: App, pathOrFile: PathOrFile, let backlinks: CustomArrayDict | null = null; await retryWithTimeout(async () => { const file = getFile(app, pathOrFile); - backlinks = app.metadataCache.getBacklinksForFile(file); + const unregister = registerFileInVault(app.vault, file); + try { + backlinks = app.metadataCache.getBacklinksForFile(file); + } finally { + unregister(); + } for (const notePath of backlinks.keys()) { const note = app.vault.getFileByPath(notePath); if (!note) { @@ -178,3 +185,32 @@ export async function getFrontMatterSafe(app: App, const cache = await getCacheSafe(app, pathOrFile); return (cache?.frontmatter ?? {}) as CombinedFrontMatter; } + +/** + * Registers the specified file in the vault. + * + * @param vault - The vault instance. + * @param file - The file to register. + * @returns A function that unregisters the file. + */ +export function registerFileInVault(vault: Vault, file: TAbstractFile): () => void { + if (!file.deleted) { + return () => { }; + } + + const deletedPaths: string[] = []; + + let deletedFile: TAbstractFile = file; + + while (deletedFile.deleted) { + deletedPaths.push(deletedFile.path); + vault.fileMap[deletedFile.path] = deletedFile; + deletedFile = deletedFile.parent!; + } + + return () => { + for (const path of deletedPaths) { + delete vault.fileMap[path]; + } + }; +}