#14 - non-existing notes are indexed

This commit is contained in:
Simon Cambier
2022-05-17 22:17:43 +02:00
parent 8c99450d3b
commit 5c2033cd06
8 changed files with 124 additions and 32 deletions

View File

@@ -1,7 +1,27 @@
import { MarkdownView } from 'obsidian'
import type { ResultNote } from './globals'
import { MarkdownView, TFile, type CachedMetadata } from 'obsidian'
import type { IndexedNote, ResultNote } from './globals'
import { stringsToRegex } from './utils'
/**
* This is an in-memory cache of the notes, with all their computed fields
* used by the search engine.
* This cache allows us to quickly de-index notes when they are deleted or updated.
*/
export let notesCache: Record<string, IndexedNote> = {}
export function resetNotesCache(): void {
notesCache = {}
}
export function getNoteFromCache(key: string): IndexedNote | undefined {
return notesCache[key]
}
export function addNoteToCache(key: string, note: IndexedNote): void {
notesCache[key] = note
}
export function removeNoteFromCache(key: string): void {
delete notesCache[key]
}
export async function openNote(
item: ResultNote,
newPane = false,
@@ -40,3 +60,23 @@ export async function createNote(name: string): Promise<void> {
console.error(e)
}
}
/**
* For a given file, returns a list of links leading to notes that don't exist
* @param file
* @param metadata
* @returns
*/
export function getNonExistingNotes(
file: TFile,
metadata: CachedMetadata,
): string[] {
return (metadata.links ?? [])
.map(l => {
const path = l.link.split(/[\^#]+/)[0] // Remove anchors and headings
return app.metadataCache.getFirstLinkpathDest(path, file.path)
? ''
: l.link
})
.filter(l => !!l)
}