From b0a78f2b30b61d197c6672d7693534d40bc4b164 Mon Sep 17 00:00:00 2001 From: Marrrrrrrrrrtin <90629140+MartinRepo@users.noreply.github.com> Date: Wed, 20 Mar 2024 15:08:22 +0000 Subject: [PATCH] feat: improve messages render performance (#777) --- web/src/ChatBox.js | 74 +---------------------------- web/src/ChatEditPage.js | 4 ++ web/src/ChatListPage.js | 4 ++ web/src/ChatMessageRender.js | 91 ++++++++++++++++++++++++++++++++++++ web/src/ChatPage.js | 10 ++++ 5 files changed, 110 insertions(+), 73 deletions(-) create mode 100644 web/src/ChatMessageRender.js diff --git a/web/src/ChatBox.js b/web/src/ChatBox.js index d0a0f3072..6c9d01eef 100644 --- a/web/src/ChatBox.js +++ b/web/src/ChatBox.js @@ -15,27 +15,10 @@ import React from "react"; import {Avatar, ChatContainer, ConversationHeader, MainContainer, Message, MessageInput, MessageList} from "@chatscope/chat-ui-kit-react"; import "@chatscope/chat-ui-kit-styles/dist/default/styles.min.css"; -import {marked} from "marked"; -import DOMPurify from "dompurify"; -import katex from "katex"; -import "katex/dist/katex.min.css"; -import hljs from "highlight.js"; -import "highlight.js/styles/atom-one-dark-reasonable.css"; import * as Conf from "./Conf"; import * as Setting from "./Setting"; import i18next from "i18next"; -marked.setOptions({ - renderer: new marked.Renderer(), - gfm: true, - tables: true, - breaks: true, - pedantic: false, - sanitize: false, - smartLists: true, - smartypants: true, -}); - class ChatBox extends React.Component { constructor(props) { super(props); @@ -117,61 +100,6 @@ class ChatBox extends React.Component { reader.readAsDataURL(file); }; - renderMarkdown(text) { - if (text === "") { - text = this.state.dots; - } - const rawHtml = marked(text); - let cleanHtml = DOMPurify.sanitize(rawHtml); - /* replace

with
, reduce paragraph spacing. */ - cleanHtml = cleanHtml.replace(/

/g, "

").replace(/<\/p>/g, "
"); - /* h2 is larger than h1, h2 is the largest, so replace h1 with h2, and set margin as 20px. */ - cleanHtml = cleanHtml.replace(/

/g, "

").replace(/<(h[1-6])>/g, "<$1 style='margin-top: 20px; margin-bottom: 20px'>"); - /* adjust margin and internal gap for unordered list and ordered list. */ - cleanHtml = cleanHtml.replace(/<(ul)>/g, "