diff --git a/src/globals.ts b/src/globals.ts index 10b6cd5..7d5531a 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -30,6 +30,7 @@ export type IndexedNote = { headings3: string doesNotExist?: boolean + parent?: string } export type SearchMatch = { diff --git a/src/main.ts b/src/main.ts index e967b5c..9fb4d13 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,5 @@ import { Plugin, TFile } from 'obsidian' import { - addNonExistingToIndex, addToIndex, initGlobalSearchIndex, removeFromIndex, @@ -40,8 +39,6 @@ export default class OmnisearchPlugin extends Plugin { this.registerEvent( this.app.vault.on('delete', file => { removeFromIndex(file.path) - // Re-index the note as non-existing file - addNonExistingToIndex(file.name) }), ) this.registerEvent( diff --git a/src/notes.ts b/src/notes.ts index 4d23364..e131ef5 100644 --- a/src/notes.ts +++ b/src/notes.ts @@ -15,6 +15,9 @@ export function resetNotesCache(): void { export function getNoteFromCache(key: string): IndexedNote | undefined { return notesCache[key] } +export function getNonExistingNotesFromCache(): IndexedNote[] { + return Object.values(notesCache).filter(note => note.doesNotExist) +} export function addNoteToCache(key: string, note: IndexedNote): void { notesCache[key] = note } @@ -73,10 +76,19 @@ export function getNonExistingNotes( ): string[] { return (metadata.links ?? []) .map(l => { - const path = l.link.split(/[\^#]+/)[0] // Remove anchors and headings + const path = removeAnchors(l.link) return app.metadataCache.getFirstLinkpathDest(path, file.path) ? '' : l.link }) .filter(l => !!l) } + +/** + * Removes anchors and headings + * @param name + * @returns + */ +export function removeAnchors(name: string): string { + return name.split(/[\^#]+/)[0] +} diff --git a/src/search.ts b/src/search.ts index 666761a..3a92836 100644 --- a/src/search.ts +++ b/src/search.ts @@ -22,6 +22,8 @@ import { getNonExistingNotes, resetNotesCache, addNoteToCache, + removeAnchors, + getNonExistingNotesFromCache, } from './notes' let minisearchInstance: MiniSearch @@ -237,7 +239,7 @@ export async function addToIndex(file: TAbstractFile): Promise { if (metadata) { const nonExisting = getNonExistingNotes(file, metadata) for (const name of nonExisting.filter(o => !getNoteFromCache(o))) { - addNonExistingToIndex(name) + addNonExistingToIndex(name, file.path) } } @@ -279,7 +281,10 @@ export async function addToIndex(file: TAbstractFile): Promise { * Useful to find internal links that lead (yet) to nowhere * @param name */ -export function addNonExistingToIndex(name: string): void { +export function addNonExistingToIndex(name: string, parent:string): void { + name = removeAnchors(name) + if (getNoteFromCache(name)) return + const filename = name + (name.endsWith('.md') ? '' : '.md') const note = { path: filename, @@ -289,7 +294,9 @@ export function addNonExistingToIndex(name: string): void { headings1: '', headings2: '', headings3: '', + doesNotExist: true, + parent, } as IndexedNote minisearchInstance.add(note) addNoteToCache(filename, note) @@ -306,9 +313,11 @@ export function removeFromIndex(path: string): void { } const note = getNoteFromCache(path) if (note) { - // Delete the original minisearchInstance.remove(note) removeNoteFromCache(path) + getNonExistingNotesFromCache().filter(n => n.parent === path).forEach(n => { + removeFromIndex(n.path) + }) } else { console.warn(`not not found under path ${path}`)