From d6fe5d9a638cf80406d2f4adb9dd60e33fd0c99e Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Mon, 24 Jun 2024 15:04:25 +0530 Subject: [PATCH] Pass current component as arg to markdown renderer in chat view This doesn't work on search modal, but hopefully will get resolved once we migrate search into a view from a modal --- src/interface/obsidian/src/chat_view.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/interface/obsidian/src/chat_view.ts b/src/interface/obsidian/src/chat_view.ts index 87df8a91..e666e6be 100644 --- a/src/interface/obsidian/src/chat_view.ts +++ b/src/interface/obsidian/src/chat_view.ts @@ -1,4 +1,4 @@ -import { MarkdownRenderer, WorkspaceLeaf, request, requestUrl, setIcon } from 'obsidian'; +import { ItemView, MarkdownRenderer, WorkspaceLeaf, request, requestUrl, setIcon } from 'obsidian'; import * as DOMPurify from 'dompurify'; import { KhojSetting } from 'src/settings'; import { KhojPaneView } from 'src/pane_view'; @@ -304,7 +304,7 @@ export class KhojChatView extends KhojPaneView { // Convert the message to html, sanitize the message html and render it to the real DOM let chat_message_body_text_el = this.contentEl.createDiv(); chat_message_body_text_el.className = "chat-message-text-response"; - chat_message_body_text_el.innerHTML = this.markdownTextToSanitizedHtml(message); + chat_message_body_text_el.innerHTML = this.markdownTextToSanitizedHtml(message, this); // Add a copy button to each chat message, if it doesn't already exist if (willReplace === true) { @@ -314,13 +314,12 @@ export class KhojChatView extends KhojPaneView { return chat_message_body_text_el; } - markdownTextToSanitizedHtml(markdownText: string): string { + markdownTextToSanitizedHtml(markdownText: string, component: ItemView): string { // Render markdown to an unlinked DOM element let virtualChatMessageBodyTextEl = document.createElement("div"); // Convert the message to html - // @ts-ignore - MarkdownRenderer.renderMarkdown(markdownText, virtualChatMessageBodyTextEl, '', null); + MarkdownRenderer.renderMarkdown(markdownText, virtualChatMessageBodyTextEl, '', component); // Remove image HTML elements with any non whitelisted src prefix virtualChatMessageBodyTextEl.innerHTML = virtualChatMessageBodyTextEl.innerHTML.replace( @@ -407,7 +406,7 @@ export class KhojChatView extends KhojPaneView { chat_message_body_text_el.innerHTML = message; } else { // @ts-ignore - chat_message_body_text_el.innerHTML = this.markdownTextToSanitizedHtml(message); + chat_message_body_text_el.innerHTML = this.markdownTextToSanitizedHtml(message, this); } // Add action buttons to each chat message element @@ -453,7 +452,7 @@ export class KhojChatView extends KhojPaneView { // Sanitize the markdown to render this.result = DOMPurify.sanitize(this.result); // @ts-ignore - htmlElement.innerHTML = this.markdownTextToSanitizedHtml(this.result); + htmlElement.innerHTML = this.markdownTextToSanitizedHtml(this.result, this); // Render action buttons for the message this.renderActionButtons(this.result, htmlElement); // Scroll to bottom of modal, till the send message input box