@@ -585,6 +585,21 @@ function extractPreCodeMeta(node: unknown): string | undefined {
585585 return typeof meta === "string" && meta . trim ( ) . length > 0 ? meta . trim ( ) : undefined ;
586586}
587587
588+ function isClosedCodeFence ( node : ReactMarkdownExtraProps [ "node" ] , text : string ) : boolean {
589+ const start = node ?. position ?. start . offset ;
590+ const end = node ?. position ?. end . offset ;
591+ if ( start === undefined || end === undefined ) return false ;
592+ const source = text . slice ( start , end ) ;
593+ const opening = / ^ (?: ` { 3 , } | ~ { 3 , } ) / . exec ( source ) ?. [ 0 ] ;
594+ const closing = / (?: ^ | \n ) (?: [ \t ] * > [ \t ] * ) * [ \t ] * ( ` { 3 , } | ~ { 3 , } ) [ \t \r ] * $ / . exec ( source ) ?. [ 1 ] ;
595+ return (
596+ opening !== undefined &&
597+ closing !== undefined &&
598+ opening [ 0 ] === closing [ 0 ] &&
599+ closing . length >= opening . length
600+ ) ;
601+ }
602+
588603type MarkdownAstNode = {
589604 type ?: string ;
590605 meta ?: unknown ;
@@ -3273,7 +3288,7 @@ const CHAT_MARKDOWN_COMPONENTS = {
32733288 return < MarkdownDetails open = { detailsOpen } > { children } </ MarkdownDetails > ;
32743289 } ,
32753290 pre : function MarkdownPre ( { node, children, ...props } ) {
3276- const { resolvedTheme, diffThemeName, isStreaming, onRunShellCommand } = use (
3291+ const { resolvedTheme, diffThemeName, isStreaming, onRunShellCommand, text } = use (
32773292 ChatMarkdownRendererContext ,
32783293 ) ;
32793294 const codeBlock = extractCodeBlock ( children ) ;
@@ -3289,7 +3304,11 @@ const CHAT_MARKDOWN_COMPONENTS = {
32893304 language = { language }
32903305 fenceTitle = { fenceTitle }
32913306 theme = { resolvedTheme }
3292- onRunShellCommand = { onRunShellCommand }
3307+ onRunShellCommand = {
3308+ onRunShellCommand && ! isStreaming && isClosedCodeFence ( node , text )
3309+ ? onRunShellCommand
3310+ : undefined
3311+ }
32933312 isStreaming = { isStreaming }
32943313 >
32953314 < RenderErrorBoundary
0 commit comments