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
doesNotExist?: boolean
parent?: string
}
export type SearchMatch = {

View File

@@ -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(

View File

@@ -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]
}

View File

@@ -22,6 +22,8 @@ import {
getNonExistingNotes,
resetNotesCache,
addNoteToCache,
removeAnchors,
getNonExistingNotesFromCache,
} from './notes'
let minisearchInstance: MiniSearch<IndexedNote>
@@ -237,7 +239,7 @@ export async function addToIndex(file: TAbstractFile): Promise<void> {
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<void> {
* 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}`)