@@ -116,6 +116,12 @@ const isInlineCode = (node?: Element): boolean => {
116116} ;
117117
118118
119+ // Check if it is a large JSON
120+ const isLargeJson = ( language : string | undefined , content : string | undefined ) : boolean => {
121+ if ( ! content || language !== 'json' ) return false ;
122+ return content . length > 5000 ; // JSON larger than 5KB is considered large JSON
123+ } ;
124+
119125// Memoize the CodeHighlight component
120126const CodeHighlight = memo ( ( { className, children, node, renderAsDiagram = false , ...props } : CodeHighlightProps ) => {
121127 const { theme } = useTheme ( ) ;
@@ -126,6 +132,10 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
126132 const mermaidRef = useRef < HTMLDivElement > ( null ) ;
127133 const debounceTimerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ; // Use ReturnType for better typing
128134
135+ // Get the content string, check if it is a large JSON
136+ const contentStr = String ( children || '' ) . replace ( / \n $ / , '' ) ;
137+ const isLargeJsonBlock = isLargeJson ( language , contentStr ) ;
138+
129139 // Handle Mermaid rendering with debounce
130140 useEffect ( ( ) => {
131141 // Effect should run when renderAsDiagram becomes true or hasRendered changes.
@@ -247,11 +257,19 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
247257 }
248258 } ;
249259 // Dependencies: renderAsDiagram ensures effect runs when diagram should be shown.
250- // children, language, theme trigger re-render if code/context changes.
251260 // Dependencies include all values used inside the effect to satisfy exhaustive-deps.
252261 // The !hasRendered check prevents re-execution of render logic after success.
253262 } , [ renderAsDiagram , hasRendered , language , children , theme ] ) ; // Add children and theme back
254263
264+ // For large JSON, skip syntax highlighting completely and use a simple pre tag
265+ if ( isLargeJsonBlock ) {
266+ return (
267+ < pre className = "whitespace-pre-wrap break-words bg-muted p-4 rounded-md overflow-x-auto text-sm font-mono" >
268+ { contentStr }
269+ </ pre >
270+ ) ;
271+ }
272+
255273 // Render based on language type
256274 // If it's a mermaid language block and rendering as diagram is not requested (e.g., incomplete stream), display as plain text
257275 if ( language === 'mermaid' && ! renderAsDiagram ) {
@@ -262,7 +280,7 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
262280 language = "text" // Use text as language to avoid syntax highlighting errors
263281 { ...props }
264282 >
265- { String ( children ) . replace ( / \n $ / , '' ) }
283+ { contentStr }
266284 </ SyntaxHighlighter >
267285 ) ;
268286 }
@@ -273,6 +291,7 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
273291 return < div className = "mermaid-diagram-container my-4 overflow-x-auto" ref = { mermaidRef } > </ div > ;
274292 }
275293
294+
276295 // Handle non-Mermaid code blocks
277296 return ! inline ? (
278297 < SyntaxHighlighter
@@ -281,12 +300,12 @@ const CodeHighlight = memo(({ className, children, node, renderAsDiagram = false
281300 language = { language }
282301 { ...props }
283302 >
284- { String ( children ) . replace ( / \n $ / , '' ) }
303+ { contentStr }
285304 </ SyntaxHighlighter >
286305 ) : (
287306 // Handle inline code
288307 < code
289- className = { cn ( className , 'mx-1 rounded-sm bg-muted px-1 py-0.5 font-mono text-sm' ) } // 添加 font-mono 确保使用等宽字体
308+ className = { cn ( className , 'mx-1 rounded-sm bg-muted px-1 py-0.5 font-mono text-sm' ) } // Add font-mono to ensure monospaced font is used
290309 { ...props }
291310 >
292311 { children }
0 commit comments