From 7e7ce276b28e3b4e0293382e878be390265ee4a7 Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Fri, 2 May 2025 14:12:24 +0530 Subject: [PATCH] (fix:mermaid/flicker) separated from markdown --- .../src/conversation/ConversationBubble.tsx | 242 ++++++++++-------- 1 file changed, 140 insertions(+), 102 deletions(-) diff --git a/frontend/src/conversation/ConversationBubble.tsx b/frontend/src/conversation/ConversationBubble.tsx index 75cf2633..0a0d0780 100644 --- a/frontend/src/conversation/ConversationBubble.tsx +++ b/frontend/src/conversation/ConversationBubble.tsx @@ -1,6 +1,6 @@ import 'katex/dist/katex.min.css'; -import { forwardRef, useRef, useState } from 'react'; +import { forwardRef, Fragment, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; import ReactMarkdown from 'react-markdown'; import { useSelector } from 'react-redux'; @@ -193,6 +193,33 @@ const ConversationBubble = forwardRef< return inlineProcessedContent; }; + const processMarkdownContent = (content: string) => { + const processedContent = preprocessLaTeX(content); + + const contentSegments: Array<{type: 'text' | 'mermaid', content: string}> = []; + + let lastIndex = 0; + const regex = /```mermaid\n([\s\S]*?)```/g; + let match; + + while ((match = regex.exec(processedContent)) !== null) { + const textBefore = processedContent.substring(lastIndex, match.index); + if (textBefore) { + contentSegments.push({ type: 'text', content: textBefore }); + } + + contentSegments.push({ type: 'mermaid', content: match[1].trim() }); + + lastIndex = match.index + match[0].length; + } + + const textAfter = processedContent.substring(lastIndex); + if (textAfter) { + contentSegments.push({ type: 'text', content: textAfter }); + } + + return contentSegments; + }; bubble = (
- - ); - } - - return match ? ( -
-
- - {language} - - -
- - {String(children).replace(/\n$/, '')} - -
- ) : ( - - {children} - - ); - }, - ul({ children }) { - return ( -
    - {children} -
- ); - }, - ol({ children }) { - return ( -
    - {children} -
- ); - }, - table({ children }) { - return ( -
- - {children} -
-
- ); - }, - thead({ children }) { - return ( - - {children} - - ); - }, - tr({ children }) { - return ( - - {children} - - ); - }, - th({ children }) { - return {children}; - }, - td({ children }) { - return {children}; - }, - }} - > - {preprocessLaTeX(message)} -
+ {(() => { + const contentSegments = processMarkdownContent(message); + return ( + <> + {contentSegments.map((segment, index) => ( + + {segment.type === 'text' ? ( + +
+ + {language} + + +
+ + {String(children).replace(/\n$/, '')} + +
+ ) : ( + + {children} + + ); + }, + ul({ children }) { + return ( + + ); + }, + ol({ children }) { + return ( +
    + {children} +
+ ); + }, + table({ children }) { + return ( +
+ + {children} +
+
+ ); + }, + thead({ children }) { + return ( + + {children} + + ); + }, + tr({ children }) { + return ( + + {children} + + ); + }, + th({ children }) { + return {children}; + }, + td({ children }) { + return {children}; + }, + }} + > + {segment.content} + + ) : ( +
+ +
+ )} + + ))} + + ); + })()} )}