Squashed some bugs related to non-existent notes indexing

This commit is contained in:
Simon Cambier
2022-05-18 17:51:32 +02:00
parent 3faf6b53a0
commit 1e3a9d001f
4 changed files with 26 additions and 7 deletions

View File

@@ -30,6 +30,7 @@ export type IndexedNote = {
headings3: string headings3: string
doesNotExist?: boolean doesNotExist?: boolean
parent?: string
} }
export type SearchMatch = { export type SearchMatch = {

View File

@@ -1,6 +1,5 @@
import { Plugin, TFile } from 'obsidian' import { Plugin, TFile } from 'obsidian'
import { import {
addNonExistingToIndex,
addToIndex, addToIndex,
initGlobalSearchIndex, initGlobalSearchIndex,
removeFromIndex, removeFromIndex,
@@ -40,8 +39,6 @@ export default class OmnisearchPlugin extends Plugin {
this.registerEvent( this.registerEvent(
this.app.vault.on('delete', file => { this.app.vault.on('delete', file => {
removeFromIndex(file.path) removeFromIndex(file.path)
// Re-index the note as non-existing file
addNonExistingToIndex(file.name)
}), }),
) )
this.registerEvent( this.registerEvent(

View File

@@ -15,6 +15,9 @@ export function resetNotesCache(): void {
export function getNoteFromCache(key: string): IndexedNote | undefined { export function getNoteFromCache(key: string): IndexedNote | undefined {
return notesCache[key] return notesCache[key]
} }
export function getNonExistingNotesFromCache(): IndexedNote[] {
return Object.values(notesCache).filter(note => note.doesNotExist)
}
export function addNoteToCache(key: string, note: IndexedNote): void { export function addNoteToCache(key: string, note: IndexedNote): void {
notesCache[key] = note notesCache[key] = note
} }
@@ -73,10 +76,19 @@ export function getNonExistingNotes(
): string[] { ): string[] {
return (metadata.links ?? []) return (metadata.links ?? [])
.map(l => { .map(l => {
const path = l.link.split(/[\^#]+/)[0] // Remove anchors and headings const path = removeAnchors(l.link)
return app.metadataCache.getFirstLinkpathDest(path, file.path) return app.metadataCache.getFirstLinkpathDest(path, file.path)
? '' ? ''
: l.link : l.link
}) })
.filter(l => !!l) .filter(l => !!l)
} }
/**
* Removes anchors and headings
* @param name
* @returns
*/
export function removeAnchors(name: string): string {
return name.split(/[\^#]+/)[0]
}

View File

@@ -22,6 +22,8 @@ import {
getNonExistingNotes, getNonExistingNotes,
resetNotesCache, resetNotesCache,
addNoteToCache, addNoteToCache,
removeAnchors,
getNonExistingNotesFromCache,
} from './notes' } from './notes'
let minisearchInstance: MiniSearch<IndexedNote> let minisearchInstance: MiniSearch<IndexedNote>
@@ -237,7 +239,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
if (metadata) { if (metadata) {
const nonExisting = getNonExistingNotes(file, metadata) const nonExisting = getNonExistingNotes(file, metadata)
for (const name of nonExisting.filter(o => !getNoteFromCache(o))) { 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<void> {
* Useful to find internal links that lead (yet) to nowhere * Useful to find internal links that lead (yet) to nowhere
* @param name * @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 filename = name + (name.endsWith('.md') ? '' : '.md')
const note = { const note = {
path: filename, path: filename,
@@ -289,7 +294,9 @@ export function addNonExistingToIndex(name: string): void {
headings1: '', headings1: '',
headings2: '', headings2: '',
headings3: '', headings3: '',
doesNotExist: true, doesNotExist: true,
parent,
} as IndexedNote } as IndexedNote
minisearchInstance.add(note) minisearchInstance.add(note)
addNoteToCache(filename, note) addNoteToCache(filename, note)
@@ -306,9 +313,11 @@ export function removeFromIndex(path: string): void {
} }
const note = getNoteFromCache(path) const note = getNoteFromCache(path)
if (note) { if (note) {
// Delete the original
minisearchInstance.remove(note) minisearchInstance.remove(note)
removeNoteFromCache(path) removeNoteFromCache(path)
getNonExistingNotesFromCache().filter(n => n.parent === path).forEach(n => {
removeFromIndex(n.path)
})
} }
else { else {
console.warn(`not not found under path ${path}`) console.warn(`not not found under path ${path}`)