diff --git a/src/search.ts b/src/search.ts index 81f9bf2..a8e5aa5 100644 --- a/src/search.ts +++ b/src/search.ts @@ -1,18 +1,25 @@ import { Notice, TFile, type TAbstractFile } from 'obsidian' import MiniSearch, { type SearchResult } from 'minisearch' -import { SPACE_OR_PUNCTUATION, type IndexedNote, type ResultNote, type SearchMatch } from './globals' -import { indexedNotes, plugin } from './stores' +import { + SPACE_OR_PUNCTUATION, + type IndexedNote, + type ResultNote, + type SearchMatch, +} from './globals' +import { plugin } from './stores' import { get } from 'svelte/store' import { extractHeadingsFromCache, stringsToRegex, wait } from './utils' let minisearchInstance: MiniSearch +let indexedNotes: Record = {} + /** * Initializes the MiniSearch instance, * and adds all the notes to the index */ export async function initGlobalSearchIndex(): Promise { - indexedNotes.set({}) + indexedNotes = {} minisearchInstance = new MiniSearch({ tokenize: text => text.split(SPACE_OR_PUNCTUATION), idField: 'path', @@ -112,7 +119,7 @@ export function getSuggestions( // Map the raw results to get usable suggestions const suggestions = results.map(result => { - const note = indexedNotes.get(result.id) + const note = indexedNotes[result.id] if (!note) { throw new Error(`Note "${result.id}" not indexed`) } @@ -145,7 +152,7 @@ export async function addToIndex(file: TAbstractFile): Promise { const fileCache = app.metadataCache.getFileCache(file) // console.log(fileCache) - if (indexedNotes.get(file.path)) { + if (indexedNotes[file.path]) { throw new Error(`${file.basename} is already indexed`) } @@ -168,7 +175,7 @@ export async function addToIndex(file: TAbstractFile): Promise { : '', } minisearchInstance.add(note) - indexedNotes.add(note) + indexedNotes[note.path] = note } catch (e) { console.trace('Error while indexing ' + file.basename) @@ -193,9 +200,9 @@ export function removeFromIndex(file: TAbstractFile): void { * @param path */ export function removeFromIndexByPath(path: string): void { - const note = indexedNotes.get(path) + const note = indexedNotes[path] if (note) { minisearchInstance.remove(note) - indexedNotes.remove(path) + delete indexedNotes[path] } } diff --git a/src/stores.ts b/src/stores.ts index 2c34629..2d2e4e4 100644 --- a/src/stores.ts +++ b/src/stores.ts @@ -1,36 +1,7 @@ -import { get, writable } from 'svelte/store' -import type { IndexedNote } from './globals' +import { writable } from 'svelte/store' import type OmnisearchPlugin from './main' -function createIndexedNotes() { - const { subscribe, set, update } = writable>({}) - return { - subscribe, - set, - add(note: IndexedNote) { - update(notes => { - notes[note.path] = note - return notes - }) - }, - remove(path: string) { - update(notes => { - delete notes[path] - return notes - }) - }, - get(path: string): IndexedNote | undefined { - return get(indexedNotes)[path] - }, - } -} - /** * A reference to the plugin instance */ export const plugin = writable() - -/** - * The entire list of indexed notes, constantly kept up-to-date. - */ -export const indexedNotes = createIndexedNotes()