From bbcdb8413d7980bb9d5b1cff01d1d3619c3f7cbe Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 3 Jun 2024 18:03:11 +0530 Subject: [PATCH] Add null checks, fix build errors in Khoj plugin on newer Obsidian --- src/interface/obsidian/src/chat_view.ts | 8 ++++---- src/interface/obsidian/src/main.ts | 4 ++-- src/interface/obsidian/src/pane_view.ts | 4 ++-- src/interface/obsidian/src/utils.ts | 7 +++---- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/interface/obsidian/src/chat_view.ts b/src/interface/obsidian/src/chat_view.ts index f56fd7fb..c4b92026 100644 --- a/src/interface/obsidian/src/chat_view.ts +++ b/src/interface/obsidian/src/chat_view.ts @@ -243,13 +243,12 @@ export class KhojChatView extends KhojPaneView { if (referenceFile) { // Find vault file associated with current reference - let linkToEntry = getLinkToEntry(mdFiles.concat(pdfFiles), referenceFile, reference); + const linkToEntry = getLinkToEntry(mdFiles.concat(pdfFiles), referenceFile, reference); - let linkElement: Element; - linkElement = referenceButton.createEl('span'); + const linkElement: Element = referenceButton.createEl('span'); linkElement.setAttribute('title', escaped_ref); linkElement.textContent = referenceFile; - if (linkToEntry && linkToEntry) { + if (linkElement && linkToEntry) { linkElement.classList.add("reference-link"); linkElement.addEventListener('click', (event) => { event.stopPropagation(); @@ -293,6 +292,7 @@ export class KhojChatView extends KhojPaneView { // Render markdow to HTML DOM element let chat_message_body_text_el = this.contentEl.createDiv(); chat_message_body_text_el.className = "chat-message-text-response"; + // @ts-ignore MarkdownRenderer.renderMarkdown(message, chat_message_body_text_el, '', null); // Replace placeholders with LaTeX delimiters diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index 83ac17b8..a97c1466 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -85,10 +85,10 @@ export default class Khoj extends Plugin { // Our view could not be found in the workspace, create a new leaf // in the right sidebar for it leaf = workspace.getRightLeaf(false); - await leaf.setViewState({ type: viewType, active: true }); + await leaf?.setViewState({ type: viewType, active: true }); } // "Reveal" the leaf in case it is in a collapsed sidebar - workspace.revealLeaf(leaf); + if (leaf) workspace.revealLeaf(leaf); } } diff --git a/src/interface/obsidian/src/pane_view.ts b/src/interface/obsidian/src/pane_view.ts index 40659572..3912ce4d 100644 --- a/src/interface/obsidian/src/pane_view.ts +++ b/src/interface/obsidian/src/pane_view.ts @@ -44,10 +44,10 @@ export abstract class KhojPaneView extends ItemView { // Our view could not be found in the workspace, create a new leaf // in the right sidebar for it leaf = workspace.getRightLeaf(false); - await leaf.setViewState({ type: viewType, active: true }); + await leaf?.setViewState({ type: viewType, active: true }); } // "Reveal" the leaf in case it is in a collapsed sidebar - workspace.revealLeaf(leaf); + if (leaf) workspace.revealLeaf(leaf); } } diff --git a/src/interface/obsidian/src/utils.ts b/src/interface/obsidian/src/utils.ts index cfdbc431..6ecf0bf6 100644 --- a/src/interface/obsidian/src/utils.ts +++ b/src/interface/obsidian/src/utils.ts @@ -139,10 +139,9 @@ export async function updateContentIndex(vault: Vault, setting: KhojSetting, las export async function createNote(name: string, newLeaf = false): Promise { try { let pathPrefix: string - // @ts-ignore - switch (app.vault.getConfig('newFileLocation')) { + switch (this.app.vault.getConfig('newFileLocation')) { case 'current': - pathPrefix = (app.workspace.getActiveFile()?.parent.path ?? '') + '/' + pathPrefix = (this.app.workspace.getActiveFile()?.parent.path ?? '') + '/' break case 'folder': pathPrefix = this.app.vault.getConfig('newFileFolderPath') + '/' @@ -151,7 +150,7 @@ export async function createNote(name: string, newLeaf = false): Promise { pathPrefix = '' break } - await app.workspace.openLinkText(`${pathPrefix}${name}.md`, '', newLeaf) + await this.app.workspace.openLinkText(`${pathPrefix}${name}.md`, '', newLeaf) } catch (e) { console.error('Khoj: Could not create note.\n' + (e as any).message); throw e