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,9 @@ function MarkdownCodeBlock({
975978 theme,
976979 onRunShellCommand,
977980 isStreaming,
981+ leadingActions,
982+ canWrap = true ,
983+ diagram = false ,
978984 children,
979985} : {
980986 code : string ;
@@ -983,6 +989,10 @@ function MarkdownCodeBlock({
983989 theme : "light" | "dark" ;
984990 onRunShellCommand ?: ( ( command : string ) => void ) | undefined ;
985991 isStreaming : boolean ;
992+ leadingActions ?: ReactNode ;
993+ canWrap ?: boolean ;
994+ /** Renders content instead of code, with actions below it like tables. */
995+ diagram ?: boolean ;
986996 children : ReactNode ;
987997} ) {
988998 const [ copied , setCopied ] = useState ( false ) ;
@@ -1040,6 +1050,37 @@ function MarkdownCodeBlock({
10401050 [ ] ,
10411051 ) ;
10421052
1053+ const copyButton = (
1054+ < Tooltip >
1055+ < TooltipTrigger
1056+ render = {
1057+ < Button
1058+ type = "button"
1059+ variant = "ghost-muted"
1060+ size = "icon-xs"
1061+ onClick = { handleCopy }
1062+ aria-label = { copyLabel }
1063+ />
1064+ }
1065+ >
1066+ { copied ? < CheckIcon className = "size-3" /> : < CopyIcon className = "size-3" /> }
1067+ </ TooltipTrigger >
1068+ < TooltipPopup side = "top" > { copyLabel } </ TooltipPopup >
1069+ </ Tooltip >
1070+ ) ;
1071+
1072+ if ( diagram ) {
1073+ return (
1074+ < div className = "my-[0.65rem]" data-language = { language } >
1075+ { children }
1076+ < div className = "mt-0.5 flex items-center justify-between select-none" >
1077+ { leadingActions }
1078+ { copyButton }
1079+ </ div >
1080+ </ div >
1081+ ) ;
1082+ }
1083+
10431084 return (
10441085 < div
10451086 className = "chat-markdown-codeblock my-[0.65rem] overflow-hidden rounded-lg border border-border/70 bg-secondary leading-snug dark:border-transparent dark:bg-input/32"
@@ -1055,23 +1096,26 @@ function MarkdownCodeBlock({
10551096 />
10561097 </ span >
10571098 < 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 >
1099+ { leadingActions }
1100+ { canWrap ? (
1101+ < Tooltip >
1102+ < TooltipTrigger
1103+ render = {
1104+ < Button
1105+ type = "button"
1106+ variant = { wrapped ? "secondary" : "ghost-muted" }
1107+ size = "icon-xs"
1108+ aria-pressed = { wrapped }
1109+ onClick = { ( ) => setWrapped ( ( value ) => ! value ) }
1110+ aria-label = { wrapLabel }
1111+ />
1112+ }
1113+ >
1114+ < WrapTextIcon className = "size-3" />
1115+ </ TooltipTrigger >
1116+ < TooltipPopup side = "top" > { wrapLabel } </ TooltipPopup >
1117+ </ Tooltip >
1118+ ) : null }
10751119 { canRun ? (
10761120 < Tooltip >
10771121 < TooltipTrigger
@@ -1090,26 +1134,82 @@ function MarkdownCodeBlock({
10901134 < TooltipPopup side = "top" > Run in terminal</ TooltipPopup >
10911135 </ Tooltip >
10921136 ) : null }
1137+ { copyButton }
1138+ </ span >
1139+ </ div >
1140+ { children }
1141+ </ div >
1142+ ) ;
1143+ }
1144+
1145+ /**
1146+ * Mermaid fences render as a diagram once the response settles; streaming and
1147+ * the code toggle keep the highlighted source.
1148+ */
1149+ function MarkdownMermaidCodeBlock ( {
1150+ code,
1151+ fenceTitle,
1152+ theme,
1153+ isStreaming,
1154+ onExpand,
1155+ children,
1156+ } : {
1157+ code : string ;
1158+ fenceTitle : string | null ;
1159+ theme : "light" | "dark" ;
1160+ isStreaming : boolean ;
1161+ onExpand : ( imageUrl : string ) => void ;
1162+ children : ReactNode ;
1163+ } ) {
1164+ const [ showCode , setShowCode ] = useState ( false ) ;
1165+ const showDiagram = ! showCode && ! isStreaming && code . trim ( ) . length > 0 ;
1166+ const toggleLabel = showCode ? "Show diagram" : "Show code" ;
1167+ return (
1168+ < MarkdownCodeBlock
1169+ code = { code }
1170+ language = "mermaid"
1171+ fenceTitle = { fenceTitle }
1172+ theme = { theme }
1173+ isStreaming = { isStreaming }
1174+ canWrap = { ! showDiagram }
1175+ diagram = { showDiagram }
1176+ leadingActions = {
1177+ isStreaming ? null : (
10931178 < Tooltip >
10941179 < TooltipTrigger
10951180 render = {
10961181 < Button
10971182 type = "button"
10981183 variant = "ghost-muted"
10991184 size = "icon-xs"
1100- onClick = { handleCopy }
1101- aria-label = { copyLabel }
1185+ onClick = { ( ) => setShowCode ( ( value ) => ! value ) }
1186+ aria-label = { toggleLabel }
11021187 />
11031188 }
11041189 >
1105- { copied ? < CheckIcon className = "size-3" /> : < CopyIcon className = "size-3" /> }
1190+ { showCode ? < WorkflowIcon className = "size-3" /> : < CodeIcon className = "size-3" /> }
11061191 </ TooltipTrigger >
1107- < TooltipPopup side = "top" > { copyLabel } </ TooltipPopup >
1192+ < TooltipPopup side = "top" > { toggleLabel } </ TooltipPopup >
11081193 </ Tooltip >
1109- </ span >
1110- </ div >
1111- { children }
1112- </ div >
1194+ )
1195+ }
1196+ >
1197+ { showDiagram ? (
1198+ < RenderErrorBoundary resetKeys = { [ code , theme ] } fallback = { children } >
1199+ < Suspense
1200+ fallback = {
1201+ < div className = "flex min-h-36 items-center justify-center text-xs text-muted-foreground" >
1202+ Rendering diagram
1203+ </ div >
1204+ }
1205+ >
1206+ < MermaidDiagram source = { code } theme = { theme } onExpand = { onExpand } />
1207+ </ Suspense >
1208+ </ RenderErrorBoundary >
1209+ ) : (
1210+ children
1211+ ) }
1212+ </ MarkdownCodeBlock >
11131213 ) ;
11141214}
11151215
@@ -3300,7 +3400,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
33003400 return < MarkdownDetails open = { detailsOpen } > { children } </ MarkdownDetails > ;
33013401 } ,
33023402 pre : function MarkdownPre ( { node, children, ...props } ) {
3303- const { resolvedTheme, diffThemeName, isStreaming, onRunShellCommand, text } = use (
3403+ const { resolvedTheme, diffThemeName, expandMedia , isStreaming, onRunShellCommand, text } = use (
33043404 ChatMarkdownRendererContext ,
33053405 ) ;
33063406 const codeBlock = extractCodeBlock ( children ) ;
@@ -3310,6 +3410,42 @@ const CHAT_MARKDOWN_COMPONENTS = {
33103410
33113411 const language = extractFenceLanguage ( codeBlock . className ) ;
33123412 const fenceTitle = extractFenceTitle ( extractPreCodeMeta ( node ) ) ;
3413+ const highlightedCode = (
3414+ < RenderErrorBoundary
3415+ resetKeys = { [ codeBlock . code , language , diffThemeName , isStreaming ] }
3416+ fallback = { < pre { ...props } > { children } </ pre > }
3417+ >
3418+ { /* Reserve the block's height but stay hidden until Shiki has colored
3419+ it, so plain text never flashes before the highlighted version. */ }
3420+ < Suspense
3421+ fallback = {
3422+ < pre { ...props } className = "invisible" aria-hidden >
3423+ { children }
3424+ </ pre >
3425+ }
3426+ >
3427+ < SuspenseShikiCodeBlock
3428+ className = { codeBlock . className }
3429+ code = { codeBlock . code }
3430+ themeName = { diffThemeName }
3431+ isStreaming = { isStreaming }
3432+ />
3433+ </ Suspense >
3434+ </ RenderErrorBoundary >
3435+ ) ;
3436+ if ( language === "mermaid" ) {
3437+ return (
3438+ < MarkdownMermaidCodeBlock
3439+ code = { codeBlock . code }
3440+ fenceTitle = { fenceTitle }
3441+ theme = { resolvedTheme }
3442+ isStreaming = { isStreaming }
3443+ onExpand = { ( src ) => expandMedia ( { images : [ { src, name : "Mermaid diagram" } ] , index : 0 } ) }
3444+ >
3445+ { highlightedCode }
3446+ </ MarkdownMermaidCodeBlock >
3447+ ) ;
3448+ }
33133449 return (
33143450 < MarkdownCodeBlock
33153451 code = { codeBlock . code }
@@ -3323,27 +3459,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
33233459 }
33243460 isStreaming = { isStreaming }
33253461 >
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 >
3462+ { highlightedCode }
33473463 </ MarkdownCodeBlock >
33483464 ) ;
33493465 } ,
0 commit comments