@@ -20,6 +20,7 @@ import {
2020 MessageSquareWarningIcon ,
2121 Minimize2Icon ,
2222 OctagonAlertIcon ,
23+ PlayIcon ,
2324 PresentationIcon ,
2425 SparklesIcon ,
2526 TriangleAlertIcon ,
@@ -215,6 +216,7 @@ interface ChatMarkdownProps {
215216 parseRawHtml ?: boolean ;
216217 /** Append a prompt that invokes a newly created artifact-template skill. */
217218 onUseArtifactTemplate ?: ( ( template : CodexArtifactTemplate ) => void ) | undefined ;
219+ onRunShellCommand ?: ( ( command : string ) => void ) | undefined ;
218220 /** Directory that anchors relative links and images; defaults to `cwd`. Set
219221 to the file's own directory when rendering a markdown file. */
220222 imageBaseDir ?: string | undefined ;
@@ -585,6 +587,23 @@ function extractPreCodeMeta(node: unknown): string | undefined {
585587 return typeof meta === "string" && meta . trim ( ) . length > 0 ? meta . trim ( ) : undefined ;
586588}
587589
590+ function isClosedCodeFence ( node : ReactMarkdownExtraProps [ "node" ] , text : string ) : boolean {
591+ const start = node ?. position ?. start . offset ;
592+ const end = node ?. position ?. end . offset ;
593+ if ( start === undefined || end === undefined ) return false ;
594+ const source = text . slice ( start , end ) ;
595+ const opening = / ^ (?: ` { 3 , } | ~ { 3 , } ) / . exec ( source ) ?. [ 0 ] ;
596+ // One class for the blockquote prefix: nested quantifiers here backtrack
597+ // exponentially on code lines that start with many `> ` markers.
598+ const closing = / (?: ^ | \n ) [ \t > ] * ( ` { 3 , } | ~ { 3 , } ) [ \t \r ] * $ / . exec ( source ) ?. [ 1 ] ;
599+ return (
600+ opening !== undefined &&
601+ closing !== undefined &&
602+ opening [ 0 ] === closing [ 0 ] &&
603+ closing . length >= opening . length
604+ ) ;
605+ }
606+
588607type MarkdownAstNode = {
589608 type ?: string ;
590609 meta ?: unknown ;
@@ -920,19 +939,34 @@ function MarkdownCodeBlock({
920939 language,
921940 fenceTitle,
922941 theme,
942+ onRunShellCommand,
943+ isStreaming,
923944 children,
924945} : {
925946 code : string ;
926947 language : string ;
927948 fenceTitle : string | null ;
928949 theme : "light" | "dark" ;
950+ onRunShellCommand ?: ( ( command : string ) => void ) | undefined ;
951+ isStreaming : boolean ;
929952 children : ReactNode ;
930953} ) {
931954 const [ copied , setCopied ] = useState ( false ) ;
932955 const [ wrapped , setWrapped ] = useState ( readInitialWordWrapSetting ) ;
933956 const copiedTimerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
934957 const wrapLabel = wrapped ? "Disable line wrap" : "Wrap lines" ;
935958 const copyLabel = copied ? "Copied" : "Copy code" ;
959+ const command = code . trim ( ) ;
960+ const canRun =
961+ onRunShellCommand !== undefined &&
962+ ! isStreaming &&
963+ / ^ (?: s h | b a s h | z s h | f i s h | s h e l l | p o w e r s h e l l | p w s h ) $ / . test ( language ) &&
964+ code . endsWith ( "\n" ) &&
965+ command . length > 0 &&
966+ ! command . endsWith ( "\\" ) &&
967+ // Control and invisible format characters (bidi overrides, zero-width) can
968+ // make the rendered command differ from what the terminal would receive.
969+ ! / [ \p{ Cc} \p{ Cf} ] / u. test ( code . slice ( 0 , - 1 ) ) ;
936970
937971 const handleCopy = useCallback ( ( ) => {
938972 if ( typeof navigator === "undefined" || navigator . clipboard == null ) {
@@ -1004,6 +1038,24 @@ function MarkdownCodeBlock({
10041038 </ TooltipTrigger >
10051039 < TooltipPopup side = "top" > { wrapLabel } </ TooltipPopup >
10061040 </ Tooltip >
1041+ { canRun ? (
1042+ < Tooltip >
1043+ < TooltipTrigger
1044+ render = {
1045+ < Button
1046+ type = "button"
1047+ variant = "ghost-muted"
1048+ size = "icon-xs"
1049+ onClick = { ( ) => onRunShellCommand ( command ) }
1050+ aria-label = "Run in terminal"
1051+ />
1052+ }
1053+ >
1054+ < PlayIcon className = "size-3" />
1055+ </ TooltipTrigger >
1056+ < TooltipPopup side = "top" > Run in terminal</ TooltipPopup >
1057+ </ Tooltip >
1058+ ) : null }
10071059 < Tooltip >
10081060 < TooltipTrigger
10091061 render = {
@@ -2244,6 +2296,7 @@ function useChatMarkdownState({
22442296 isStreaming = false ,
22452297 skills = EMPTY_MARKDOWN_SKILLS ,
22462298 onUseArtifactTemplate,
2299+ onRunShellCommand,
22472300 imageBaseDir,
22482301 onImageExpand,
22492302 renderContextReference,
@@ -2650,6 +2703,7 @@ function useChatMarkdownState({
26502703 markdownFileLinkMetaByHref,
26512704 onTaskListChange,
26522705 onUseArtifactTemplate,
2706+ onRunShellCommand,
26532707 openChangeRequestLink,
26542708 openDeferredMarkdownLink,
26552709 openExternalLinkInPreview,
@@ -2680,6 +2734,7 @@ function useChatMarkdownState({
26802734 markdownFileLinkMetaByHref ,
26812735 onTaskListChange ,
26822736 onUseArtifactTemplate ,
2737+ onRunShellCommand ,
26832738 openChangeRequestLink ,
26842739 openDeferredMarkdownLink ,
26852740 openExternalLinkInPreview ,
@@ -3211,7 +3266,9 @@ const CHAT_MARKDOWN_COMPONENTS = {
32113266 return < MarkdownDetails open = { detailsOpen } > { children } </ MarkdownDetails > ;
32123267 } ,
32133268 pre : function MarkdownPre ( { node, children, ...props } ) {
3214- const { resolvedTheme, diffThemeName, isStreaming } = use ( ChatMarkdownRendererContext ) ;
3269+ const { resolvedTheme, diffThemeName, isStreaming, onRunShellCommand, text } = use (
3270+ ChatMarkdownRendererContext ,
3271+ ) ;
32153272 const codeBlock = extractCodeBlock ( children ) ;
32163273 if ( ! codeBlock ) {
32173274 return < pre { ...props } > { children } </ pre > ;
@@ -3225,6 +3282,12 @@ const CHAT_MARKDOWN_COMPONENTS = {
32253282 language = { language }
32263283 fenceTitle = { fenceTitle }
32273284 theme = { resolvedTheme }
3285+ onRunShellCommand = {
3286+ onRunShellCommand && ! isStreaming && isClosedCodeFence ( node , text )
3287+ ? onRunShellCommand
3288+ : undefined
3289+ }
3290+ isStreaming = { isStreaming }
32283291 >
32293292 < RenderErrorBoundary
32303293 resetKeys = { [ codeBlock . code , language , diffThemeName , isStreaming ] }
0 commit comments