-
-
Notifications
You must be signed in to change notification settings - Fork 23.1k
feat: Add Mermaid renderer support in chat window (#5402) #5410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| import { IconClipboard, IconDownload } from '@tabler/icons-react' | ||
| import { memo, useState } from 'react' | ||
| import { memo, useState, useEffect, useRef } from 'react' | ||
| import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' | ||
| import { oneDark } from 'react-syntax-highlighter/dist/esm/styles/prism' | ||
| import PropTypes from 'prop-types' | ||
| import { Box, IconButton, Popover, Typography } from '@mui/material' | ||
| import { useTheme } from '@mui/material/styles' | ||
| import mermaid from 'mermaid' | ||
|
|
||
| const programmingLanguages = { | ||
| javascript: '.js', | ||
|
|
@@ -29,13 +30,41 @@ const programmingLanguages = { | |
| shell: '.sh', | ||
| sql: '.sql', | ||
| html: '.html', | ||
| css: '.css' | ||
| css: '.css', | ||
| mermaid: '.mmd' | ||
| } | ||
|
|
||
| export const CodeBlock = memo(({ language, chatflowid, isFullWidth, value }) => { | ||
| const theme = useTheme() | ||
| const [anchorEl, setAnchorEl] = useState(null) | ||
| const [mermaidSvg, setMermaidSvg] = useState(null) | ||
| const [mermaidError, setMermaidError] = useState(null) | ||
| const diagramIdRef = useRef(`mermaid-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`) | ||
| const openPopOver = Boolean(anchorEl) | ||
| const isMermaid = language?.toLowerCase() === 'mermaid' | ||
|
|
||
| useEffect(() => { | ||
| if (!isMermaid || !value) return | ||
|
|
||
| mermaid.initialize({ | ||
| startOnLoad: false, | ||
| theme: theme.palette.mode === 'dark' ? 'dark' : 'default', | ||
| securityLevel: 'strict' | ||
| }) | ||
|
|
||
| const renderMermaid = async () => { | ||
| try { | ||
| setMermaidError(null) | ||
| const { svg } = await mermaid.render(diagramIdRef.current, value) | ||
| setMermaidSvg(svg) | ||
| } catch (error) { | ||
| setMermaidError(error.message) | ||
| setMermaidSvg(null) | ||
| } | ||
| } | ||
|
|
||
| renderMermaid() | ||
| }, [isMermaid, value, theme.palette.mode]) | ||
|
Comment on lines
46
to
71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of the To optimize this, I recommend debouncing the rendering logic. This will ensure that the rendering only happens after the code has stopped changing for a brief period. useEffect(() => {
if (!isMermaid || !value) return
const timer = setTimeout(() => {
mermaid.initialize({
startOnLoad: false,
theme: theme.palette.mode === 'dark' ? 'dark' : 'default',
securityLevel: 'strict'
})
const renderMermaid = async () => {
try {
setMermaidError(null)
const { svg } = await mermaid.render(diagramIdRef.current, value)
setMermaidSvg(svg)
} catch (error) {
setMermaidError(error.message)
setMermaidSvg(null)
}
}
renderMermaid()
}, 300)
return () => clearTimeout(timer)
}, [isMermaid, value, theme.palette.mode]) |
||
|
|
||
| const handleClosePopOver = () => { | ||
| setAnchorEl(null) | ||
|
|
@@ -107,9 +136,30 @@ export const CodeBlock = memo(({ language, chatflowid, isFullWidth, value }) => | |
| </div> | ||
| </Box> | ||
|
|
||
| <SyntaxHighlighter language={language} style={oneDark} customStyle={{ margin: 0 }}> | ||
| {value} | ||
| </SyntaxHighlighter> | ||
| {isMermaid ? ( | ||
| <Box sx={{ background: theme.palette?.common.dark, p: 2, borderBottomLeftRadius: 10, borderBottomRightRadius: 10 }}> | ||
| {mermaidError ? ( | ||
| <Box> | ||
| <Typography variant='body2' color='error' sx={{ mb: 1 }}> | ||
| Mermaid rendering error: {mermaidError} | ||
| </Typography> | ||
| <SyntaxHighlighter language='text' style={oneDark} customStyle={{ margin: 0 }}> | ||
| {value} | ||
| </SyntaxHighlighter> | ||
| </Box> | ||
| ) : mermaidSvg ? ( | ||
| <div dangerouslySetInnerHTML={{ __html: mermaidSvg }} style={{ display: 'flex', justifyContent: 'center' }} /> | ||
| ) : ( | ||
| <Typography variant='body2' color='text.secondary'> | ||
| Rendering diagram... | ||
| </Typography> | ||
| )} | ||
| </Box> | ||
| ) : ( | ||
| <SyntaxHighlighter language={language} style={oneDark} customStyle={{ margin: 0 }}> | ||
| {value} | ||
| </SyntaxHighlighter> | ||
| )} | ||
| </div> | ||
| ) | ||
| }) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this would be an issue, because when streaming, value changes, this will cause multiple initialization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrapped the rendering logic in a setTimeout with a 300ms delay