diff --git a/src/main.ts b/src/main.ts index d51eff5..dbaeb9e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,11 @@ -import { App, Notice, Platform, Plugin, type PluginManifest } from 'obsidian' +import { + App, + Notice, + Platform, + Plugin, + type PluginManifest, + TFile, +} from 'obsidian' import { OmnisearchInFileModal, OmnisearchVaultModal, @@ -109,7 +116,10 @@ export default class OmnisearchPlugin extends Plugin { // Listeners to keep the search index up-to-date this.registerEvent( this.app.vault.on('create', file => { - if (this.notesIndexer.isFileIndexable(file.path)) { + if ( + file instanceof TFile && + this.notesIndexer.isFileIndexable(file.path) + ) { logDebug('Indexing new file', file.path) searchEngine.addFromPaths([file.path]) this.embedsRepository.refreshEmbedsForNote(file.path) diff --git a/src/repositories/documents-repository.ts b/src/repositories/documents-repository.ts index ba702de..d064697 100644 --- a/src/repositories/documents-repository.ts +++ b/src/repositories/documents-repository.ts @@ -97,17 +97,18 @@ export class DocumentsRepository { // ** Canvas ** // Extract the text fields from the json else if (isFileCanvas(path)) { - const canvas = JSON.parse(await app.vault.cachedRead(file)) as CanvasData + const fileContents = await app.vault.cachedRead(file) + const canvas: CanvasData = fileContents ? JSON.parse(fileContents) : {} let texts: string[] = [] // Concatenate text from the canvas fields - for (const node of canvas.nodes) { + for (const node of canvas.nodes ?? []) { if (node.type === 'text') { texts.push(node.text) } else if (node.type === 'file') { texts.push(node.file) } } - for (const edge of canvas.edges.filter(e => !!e.label)) { + for (const edge of (canvas.edges ?? []).filter(e => !!e.label)) { texts.push(edge.label!) } content = texts.join('\r\n')