diff --git a/src/apps/chat/components/message/RenderCode.tsx b/src/apps/chat/components/message/RenderCode.tsx index 7ce2924da..e7c720eec 100644 --- a/src/apps/chat/components/message/RenderCode.tsx +++ b/src/apps/chat/components/message/RenderCode.tsx @@ -28,19 +28,35 @@ function RenderCodeImpl(props: { highlightCode: (inferredCodeLanguage: string | null, blockCode: string) => string, inferCodeLanguage: (blockTitle: string, code: string) => string | null, }) { + // state const [showHTML, setShowHTML] = React.useState(false); - const [showSVG, setShowSVG] = React.useState(true); + const [showMermaid, setShowMermaid] = React.useState(true); const [showPlantUML, setShowPlantUML] = React.useState(true); + const [showSVG, setShowSVG] = React.useState(true); // derived props - const { codeBlock: { blockTitle, blockCode }, highlightCode, inferCodeLanguage } = props; + const { + codeBlock: { blockTitle, blockCode, complete: blockComplete }, + highlightCode, inferCodeLanguage, + } = props; + + // heuristic for language, and syntax highlight + const { highlightedCode, inferredCodeLanguage } = React.useMemo( + () => { + const inferredCodeLanguage = inferCodeLanguage(blockTitle, blockCode); + const highlightedCode = highlightCode(inferredCodeLanguage, blockCode); + return { highlightedCode, inferredCodeLanguage }; + }, [inferCodeLanguage, blockTitle, blockCode, highlightCode]); + + + // heuristics for specialized rendering const isHTML = heuristicIsHtml(blockCode); const renderHTML = isHTML && showHTML; - const isSVG = blockCode.startsWith(''); - const renderSVG = isSVG && showSVG; + const isMermaid = blockTitle === 'mermaid' && blockComplete; + const renderMermaid = isMermaid && showMermaid; const isPlantUML = (blockCode.startsWith('@startuml') && blockCode.endsWith('@enduml')) @@ -86,13 +102,8 @@ function RenderCodeImpl(props: { }); renderPlantUML = renderPlantUML && (!!plantUmlHtmlData || !!plantUmlError); - // heuristic for language, and syntax highlight - const { highlightedCode, inferredCodeLanguage } = React.useMemo( - () => { - const inferredCodeLanguage = inferCodeLanguage(blockTitle, blockCode); - const highlightedCode = highlightCode(inferredCodeLanguage, blockCode); - return { highlightedCode, inferredCodeLanguage }; - }, [inferCodeLanguage, blockTitle, blockCode, highlightCode]); + const isSVG = blockCode.startsWith(''); + const renderSVG = isSVG && showSVG; const languagesCodepen = ['html', 'css', 'javascript', 'json', 'typescript']; @@ -131,30 +142,27 @@ function RenderCodeImpl(props: { )} {/* Renders HTML, or inline SVG, inline plantUML rendered, or highlighted code */} - {renderHTML ? - : } + {renderHTML + ? + : renderMermaid + ?
{blockCode}
+ : } {/* Code Buttons */} - {isSVG && ( - - setShowSVG(!showSVG)}> - - - - )} {isHTML && ( setShowHTML(!showHTML)}> @@ -162,6 +170,13 @@ function RenderCodeImpl(props: { )} + {isMermaid && ( + + setShowMermaid(!showMermaid)}> + + + + )} {isPlantUML && ( setShowPlantUML(!showPlantUML)}> @@ -169,6 +184,13 @@ function RenderCodeImpl(props: { )} + {isSVG && ( + + setShowSVG(!showSVG)}> + + + + )} {canCodepen && } {canReplit && }