Save/load index to files, improve logging
This commit is contained in:
31
src/notes.ts
31
src/notes.ts
@@ -14,9 +14,29 @@ import { stringsToRegex } from './utils'
|
||||
*/
|
||||
export let notesCache: Record<string, IndexedNote> = {}
|
||||
|
||||
const notesCacheFilePath = `${app.vault.configDir}/plugins/omnisearch/notesCache.json`
|
||||
|
||||
export function resetNotesCache(): void {
|
||||
notesCache = {}
|
||||
}
|
||||
|
||||
export async function loadNotesCache(): Promise<void> {
|
||||
if (await app.vault.adapter.exists(notesCacheFilePath)) {
|
||||
try {
|
||||
const json = await app.vault.adapter.read(notesCacheFilePath)
|
||||
notesCache = JSON.parse(json)
|
||||
console.log("Notes cache loaded from the file")
|
||||
}
|
||||
catch(e) {
|
||||
console.trace("Could not load Notes cache from the file")
|
||||
console.error(e)
|
||||
}
|
||||
}
|
||||
|
||||
if (!notesCache) {
|
||||
notesCache = {}
|
||||
}
|
||||
}
|
||||
export function getNoteFromCache(key: string): IndexedNote | undefined {
|
||||
return notesCache[key]
|
||||
}
|
||||
@@ -117,3 +137,14 @@ export function getNonExistingNotes(
|
||||
export function removeAnchors(name: string): string {
|
||||
return name.split(/[\^#]+/)[0]
|
||||
}
|
||||
|
||||
export async function saveNotesCacheToFile(): Promise<void> {
|
||||
const json = JSON.stringify(notesCache)
|
||||
await app.vault.adapter.write(notesCacheFilePath, json)
|
||||
console.log("Notes cache saved to the file")
|
||||
}
|
||||
|
||||
export function isCacheOutdated(file: TFile): boolean {
|
||||
const indexedNote = getNoteFromCache(file.path)
|
||||
return !indexedNote || indexedNote.mtime !== file.stat.mtime
|
||||
}
|
||||
Reference in New Issue
Block a user