77import {
88 CheckIcon ,
99 ChevronRightIcon ,
10+ CodeIcon ,
1011 CopyIcon ,
1112 FileSpreadsheetIcon ,
1213 FileTextIcon ,
@@ -24,6 +25,7 @@ import {
2425 PresentationIcon ,
2526 SparklesIcon ,
2627 TriangleAlertIcon ,
28+ WorkflowIcon ,
2729 WrapTextIcon ,
2830 type LucideIcon ,
2931} from "lucide-react" ;
@@ -141,6 +143,7 @@ import { GitHubIcon } from "./Icons";
141143import { createIncrementalHighlightedDocument } from "../lib/incrementalHighlighting" ;
142144import { HighlightedCodeLines } from "./chat/HighlightedCodeLines" ;
143145import { RenderErrorBoundary } from "./RenderErrorBoundary" ;
146+ import { MermaidDiagram } from "./chat/MermaidDiagram" ;
144147import { useTheme } from "../hooks/useTheme" ;
145148import { getClientSettings , useClientSettings } from "../hooks/useSettings" ;
146149import {
@@ -975,6 +978,8 @@ function MarkdownCodeBlock({
975978 theme,
976979 onRunShellCommand,
977980 isStreaming,
981+ leadingActions,
982+ canWrap = true ,
978983 children,
979984} : {
980985 code : string ;
@@ -983,6 +988,8 @@ function MarkdownCodeBlock({
983988 theme : "light" | "dark" ;
984989 onRunShellCommand ?: ( ( command : string ) => void ) | undefined ;
985990 isStreaming : boolean ;
991+ leadingActions ?: ReactNode ;
992+ canWrap ?: boolean ;
986993 children : ReactNode ;
987994} ) {
988995 const [ copied , setCopied ] = useState ( false ) ;
@@ -1055,23 +1062,26 @@ function MarkdownCodeBlock({
10551062 />
10561063 </ span >
10571064 < span className = "flex items-center gap-0.5" role = "toolbar" aria-label = "Code block actions" >
1058- < Tooltip >
1059- < TooltipTrigger
1060- render = {
1061- < Button
1062- type = "button"
1063- variant = { wrapped ? "secondary" : "ghost-muted" }
1064- size = "icon-xs"
1065- aria-pressed = { wrapped }
1066- onClick = { ( ) => setWrapped ( ( value ) => ! value ) }
1067- aria-label = { wrapLabel }
1068- />
1069- }
1070- >
1071- < WrapTextIcon className = "size-3" />
1072- </ TooltipTrigger >
1073- < TooltipPopup side = "top" > { wrapLabel } </ TooltipPopup >
1074- </ Tooltip >
1065+ { leadingActions }
1066+ { canWrap ? (
1067+ < Tooltip >
1068+ < TooltipTrigger
1069+ render = {
1070+ < Button
1071+ type = "button"
1072+ variant = { wrapped ? "secondary" : "ghost-muted" }
1073+ size = "icon-xs"
1074+ aria-pressed = { wrapped }
1075+ onClick = { ( ) => setWrapped ( ( value ) => ! value ) }
1076+ aria-label = { wrapLabel }
1077+ />
1078+ }
1079+ >
1080+ < WrapTextIcon className = "size-3" />
1081+ </ TooltipTrigger >
1082+ < TooltipPopup side = "top" > { wrapLabel } </ TooltipPopup >
1083+ </ Tooltip >
1084+ ) : null }
10751085 { canRun ? (
10761086 < Tooltip >
10771087 < TooltipTrigger
@@ -1113,6 +1123,76 @@ function MarkdownCodeBlock({
11131123 ) ;
11141124}
11151125
1126+ /**
1127+ * Mermaid fences render as a diagram once the response settles; streaming and
1128+ * the code toggle keep the highlighted source.
1129+ */
1130+ function MarkdownMermaidCodeBlock ( {
1131+ code,
1132+ fenceTitle,
1133+ theme,
1134+ isStreaming,
1135+ onExpand,
1136+ children,
1137+ } : {
1138+ code : string ;
1139+ fenceTitle : string | null ;
1140+ theme : "light" | "dark" ;
1141+ isStreaming : boolean ;
1142+ onExpand : ( imageUrl : string ) => void ;
1143+ children : ReactNode ;
1144+ } ) {
1145+ const [ showCode , setShowCode ] = useState ( false ) ;
1146+ const showDiagram = ! showCode && ! isStreaming && code . trim ( ) . length > 0 ;
1147+ const toggleLabel = showCode ? "Show diagram" : "Show code" ;
1148+ return (
1149+ < MarkdownCodeBlock
1150+ code = { code }
1151+ language = "mermaid"
1152+ fenceTitle = { fenceTitle }
1153+ theme = { theme }
1154+ isStreaming = { isStreaming }
1155+ canWrap = { ! showDiagram }
1156+ leadingActions = {
1157+ isStreaming ? null : (
1158+ < Tooltip >
1159+ < TooltipTrigger
1160+ render = {
1161+ < Button
1162+ type = "button"
1163+ variant = "ghost-muted"
1164+ size = "icon-xs"
1165+ onClick = { ( ) => setShowCode ( ( value ) => ! value ) }
1166+ aria-label = { toggleLabel }
1167+ />
1168+ }
1169+ >
1170+ { showCode ? < WorkflowIcon className = "size-3" /> : < CodeIcon className = "size-3" /> }
1171+ </ TooltipTrigger >
1172+ < TooltipPopup side = "top" > { toggleLabel } </ TooltipPopup >
1173+ </ Tooltip >
1174+ )
1175+ }
1176+ >
1177+ { showDiagram ? (
1178+ < RenderErrorBoundary resetKeys = { [ code , theme ] } fallback = { children } >
1179+ < Suspense
1180+ fallback = {
1181+ < div className = "flex min-h-36 items-center justify-center text-xs text-muted-foreground" >
1182+ Rendering diagram
1183+ </ div >
1184+ }
1185+ >
1186+ < MermaidDiagram source = { code } theme = { theme } onExpand = { onExpand } />
1187+ </ Suspense >
1188+ </ RenderErrorBoundary >
1189+ ) : (
1190+ children
1191+ ) }
1192+ </ MarkdownCodeBlock >
1193+ ) ;
1194+ }
1195+
11161196interface SuspenseShikiCodeBlockProps {
11171197 className : string | undefined ;
11181198 code : string ;
@@ -3300,7 +3380,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
33003380 return < MarkdownDetails open = { detailsOpen } > { children } </ MarkdownDetails > ;
33013381 } ,
33023382 pre : function MarkdownPre ( { node, children, ...props } ) {
3303- const { resolvedTheme, diffThemeName, isStreaming, onRunShellCommand, text } = use (
3383+ const { resolvedTheme, diffThemeName, expandMedia , isStreaming, onRunShellCommand, text } = use (
33043384 ChatMarkdownRendererContext ,
33053385 ) ;
33063386 const codeBlock = extractCodeBlock ( children ) ;
@@ -3310,6 +3390,42 @@ const CHAT_MARKDOWN_COMPONENTS = {
33103390
33113391 const language = extractFenceLanguage ( codeBlock . className ) ;
33123392 const fenceTitle = extractFenceTitle ( extractPreCodeMeta ( node ) ) ;
3393+ const highlightedCode = (
3394+ < RenderErrorBoundary
3395+ resetKeys = { [ codeBlock . code , language , diffThemeName , isStreaming ] }
3396+ fallback = { < pre { ...props } > { children } </ pre > }
3397+ >
3398+ { /* Reserve the block's height but stay hidden until Shiki has colored
3399+ it, so plain text never flashes before the highlighted version. */ }
3400+ < Suspense
3401+ fallback = {
3402+ < pre { ...props } className = "invisible" aria-hidden >
3403+ { children }
3404+ </ pre >
3405+ }
3406+ >
3407+ < SuspenseShikiCodeBlock
3408+ className = { codeBlock . className }
3409+ code = { codeBlock . code }
3410+ themeName = { diffThemeName }
3411+ isStreaming = { isStreaming }
3412+ />
3413+ </ Suspense >
3414+ </ RenderErrorBoundary >
3415+ ) ;
3416+ if ( language === "mermaid" ) {
3417+ return (
3418+ < MarkdownMermaidCodeBlock
3419+ code = { codeBlock . code }
3420+ fenceTitle = { fenceTitle }
3421+ theme = { resolvedTheme }
3422+ isStreaming = { isStreaming }
3423+ onExpand = { ( src ) => expandMedia ( { images : [ { src, name : "Mermaid diagram" } ] , index : 0 } ) }
3424+ >
3425+ { highlightedCode }
3426+ </ MarkdownMermaidCodeBlock >
3427+ ) ;
3428+ }
33133429 return (
33143430 < MarkdownCodeBlock
33153431 code = { codeBlock . code }
@@ -3323,27 +3439,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
33233439 }
33243440 isStreaming = { isStreaming }
33253441 >
3326- < RenderErrorBoundary
3327- resetKeys = { [ codeBlock . code , language , diffThemeName , isStreaming ] }
3328- fallback = { < pre { ...props } > { children } </ pre > }
3329- >
3330- { /* Reserve the block's height but stay hidden until Shiki has colored
3331- it, so plain text never flashes before the highlighted version. */ }
3332- < Suspense
3333- fallback = {
3334- < pre { ...props } className = "invisible" aria-hidden >
3335- { children }
3336- </ pre >
3337- }
3338- >
3339- < SuspenseShikiCodeBlock
3340- className = { codeBlock . className }
3341- code = { codeBlock . code }
3342- themeName = { diffThemeName }
3343- isStreaming = { isStreaming }
3344- />
3345- </ Suspense >
3346- </ RenderErrorBoundary >
3442+ { highlightedCode }
33473443 </ MarkdownCodeBlock >
33483444 ) ;
33493445 } ,
0 commit comments