@@ -24,12 +24,16 @@ import {
2424 squashAtomCommandFailure ,
2525 type AtomCommandResult ,
2626} from "@t3tools/client-runtime/state/runtime" ;
27- import { classifyMarkdownImageSource } from "@t3tools/client-runtime/markdown-images" ;
27+ import {
28+ classifyMarkdownImageSource ,
29+ markdownImageSourceFragment ,
30+ } from "@t3tools/client-runtime/markdown-images" ;
2831import * as Cause from "effect/Cause" ;
2932import { AsyncResult } from "effect/unstable/reactivity" ;
3033import React , {
3134 Children ,
3235 Suspense ,
36+ type CSSProperties ,
3337 type ClipboardEvent as ReactClipboardEvent ,
3438 type MouseEvent as ReactMouseEvent ,
3539 isValidElement ,
@@ -90,6 +94,7 @@ import {
9094import { remarkNormalizeListItemIndentation } from "../markdown-list-indentation" ;
9195import {
9296 extractMarkdownLinkHrefs ,
97+ isWindowsDrivePathHref ,
9398 normalizeMarkdownLinkDestination ,
9499 resolveInlineCodeFileLinkMeta ,
95100 resolveMarkdownFileLinkMeta ,
@@ -149,6 +154,7 @@ interface ChatMarkdownProps {
149154 lineBreaks ?: boolean ;
150155 /** Parse sanitized raw HTML instead of displaying its source text. */
151156 parseRawHtml ?: boolean ;
157+ imageBaseDir ?: string | undefined ;
152158}
153159
154160export function canUseMarkdownFileShellActions (
@@ -237,27 +243,24 @@ export function orderedListGutterStyle(
237243 return { "--list-gutter" : `${ markerWidth + 1 } ch` } ;
238244}
239245
240- type MarkdownHtmlAstNode = {
246+ type MarkdownImageHastNode = {
241247 type ?: string ;
242248 tagName ?: string ;
243249 properties ?: Record < string , unknown > ;
244- children ?: MarkdownHtmlAstNode [ ] ;
250+ children ?: MarkdownImageHastNode [ ] ;
245251} ;
246252
247- /** Preserve Windows drive paths through the protocol allowlist in rehype-sanitize . */
248- function rehypeNormalizeWindowsImageSrc ( ) {
249- return ( tree : MarkdownHtmlAstNode ) => {
250- const visit = ( node : MarkdownHtmlAstNode ) => {
253+ /** Carries authored image source metadata through the sanitizer to the image renderer . */
254+ function rehypePreserveImageSourceMeta ( ) {
255+ return ( tree : MarkdownImageHastNode ) => {
256+ const visit = ( node : MarkdownImageHastNode ) => {
251257 const src = node . properties ?. src ;
252- if (
253- node . type === "element" &&
254- node . tagName === "img" &&
255- typeof src === "string" &&
256- WINDOWS_DRIVE_PATH_REGEX . test ( src )
257- ) {
258+ const title = node . properties ?. title ;
259+ if ( node . type === "element" && node . tagName === "img" ) {
258260 node . properties = {
259261 ...node . properties ,
260- src : `file:///${ src . replaceAll ( "\\" , "/" ) } ` ,
262+ ...( typeof src === "string" && isWindowsDrivePathHref ( src ) ? { dataLocalSrc : src } : { } ) ,
263+ ...( typeof title === "string" ? { dataMarkdownTitle : title } : { } ) ,
261264 } ;
262265 }
263266 node . children ?. forEach ( visit ) ;
@@ -274,6 +277,7 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
274277 "*" : ( defaultSchema . attributes ?. [ "*" ] ?? [ ] ) . filter ( ( attribute ) => attribute !== "title" ) ,
275278 code : [ ...( defaultSchema . attributes ?. code ?? [ ] ) , "dataCodeMeta" , "dataInlineCode" ] ,
276279 blockquote : [ ...( defaultSchema . attributes ?. blockquote ?? [ ] ) , "dataAlert" ] ,
280+ img : [ ...( defaultSchema . attributes ?. img ?? [ ] ) , "dataLocalSrc" , "dataMarkdownTitle" ] ,
277281 } ,
278282 protocols : {
279283 ...defaultSchema . protocols ,
@@ -301,7 +305,7 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
301305
302306const CHAT_MARKDOWN_REHYPE_PLUGINS = [
303307 rehypeRaw ,
304- rehypeNormalizeWindowsImageSrc ,
308+ rehypePreserveImageSourceMeta ,
305309 [ rehypeSanitize , CHAT_MARKDOWN_SANITIZE_SCHEMA ] ,
306310] satisfies NonNullable < ReactMarkdownOptions [ "rehypePlugins" ] > ;
307311
@@ -1045,21 +1049,63 @@ const MarkdownLinkFavicon = memo(function MarkdownLinkFavicon({ host }: { host:
10451049 ) ;
10461050} ) ;
10471051
1048- const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME =
1049- "h-auto w-auto max-h-[30rem] max-w-[min(100%,30rem)] object-contain" ;
1052+ const CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME = "max-h-[30rem] max-w-[min(100%,30rem)]" ;
1053+ const CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME = cn (
1054+ "h-auto w-auto object-contain" ,
1055+ CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME ,
1056+ ) ;
1057+
1058+ function markdownImageCopy ( alt : string , src : string , title : string | undefined ) : string {
1059+ const escapedAlt = alt . replaceAll ( "\\" , "\\\\" ) . replaceAll ( "[" , "\\[" ) . replaceAll ( "]" , "\\]" ) ;
1060+ const titleSuffix =
1061+ title === undefined ? "" : ` "${ title . replaceAll ( "\\" , "\\\\" ) . replaceAll ( '"' , '\\"' ) } "` ;
1062+ return `` ;
1063+ }
1064+
1065+ function authoredImageSizeStyle (
1066+ width : string | number | undefined ,
1067+ height : string | number | undefined ,
1068+ ) : CSSProperties | undefined {
1069+ const parsedWidth = Number ( width ) ;
1070+ const parsedHeight = Number ( height ) ;
1071+ const hasWidth = Number . isFinite ( parsedWidth ) && parsedWidth > 0 ;
1072+ const hasHeight = Number . isFinite ( parsedHeight ) && parsedHeight > 0 ;
1073+ if ( hasWidth && hasHeight ) {
1074+ return {
1075+ width : parsedWidth ,
1076+ height : "auto" ,
1077+ aspectRatio : `${ parsedWidth } / ${ parsedHeight } ` ,
1078+ maxWidth : `min(100%, 30rem, ${ ( 30 * parsedWidth ) / parsedHeight } rem)` ,
1079+ } ;
1080+ }
1081+ if ( hasWidth ) return { maxWidth : `min(100%, 30rem, ${ parsedWidth } px)` } ;
1082+ if ( hasHeight ) return { maxHeight : `min(30rem, ${ parsedHeight } px)` } ;
1083+ return undefined ;
1084+ }
10501085
1051- // block! outranks the unlayered `.chat-markdown img { display: inline-block }`
1052- // rule, keeping workspace images on the same block layout as their placeholder.
1086+ const CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME = "inline-block!" ;
10531087const CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME = cn (
10541088 CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME ,
1055- "my-1 block! rounded-lg border border-border/40" ,
1089+ CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME ,
1090+ "rounded-lg border border-border/40" ,
10561091) ;
10571092
1058- function ChatMarkdownImageFallback ( props : { readonly alt : string } ) {
1093+ function ChatMarkdownImageFallback ( props : {
1094+ readonly alt : string ;
1095+ readonly copyMarkdown ?: string | undefined ;
1096+ } ) {
10591097 return (
1060- < span className = "my-1 inline-flex items-center gap-1.5 rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground" >
1061- < TriangleAlertIcon aria-hidden className = "size-3.5 shrink-0" />
1062- { props . alt . length > 0 ? `Image unavailable · ${ props . alt } ` : "Image unavailable" }
1098+ < span
1099+ data-markdown-copy = { props . copyMarkdown }
1100+ className = { cn (
1101+ CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME ,
1102+ "rounded-md border border-border/40 bg-muted/40 px-2 py-1 text-xs text-muted-foreground" ,
1103+ ) }
1104+ >
1105+ < span className = "inline-flex items-center gap-1.5" >
1106+ < TriangleAlertIcon aria-hidden className = "size-3.5 shrink-0" />
1107+ { props . alt . length > 0 ? `Image unavailable · ${ props . alt } ` : "Image unavailable" }
1108+ </ span >
10631109 </ span >
10641110 ) ;
10651111}
@@ -1069,6 +1115,9 @@ const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(prop
10691115 readonly threadRef : ScopedThreadRef ;
10701116 readonly path : string ;
10711117 readonly alt : string ;
1118+ readonly copyMarkdown : string ;
1119+ readonly srcFragment : string ;
1120+ readonly style ?: CSSProperties | undefined ;
10721121} ) {
10731122 const assetUrl = useAssetUrlState ( props . threadRef . environmentId , {
10741123 _tag : "workspace-file" ,
@@ -1078,24 +1127,32 @@ const ChatMarkdownWorkspaceImage = memo(function ChatMarkdownWorkspaceImage(prop
10781127 const [ failedUrl , setFailedUrl ] = useState < string | null > ( null ) ;
10791128
10801129 if ( assetUrl . _tag === "Failure" || ( assetUrl . _tag === "Success" && failedUrl === assetUrl . url ) ) {
1081- return < ChatMarkdownImageFallback alt = { props . alt } /> ;
1130+ return < ChatMarkdownImageFallback alt = { props . alt } copyMarkdown = { props . copyMarkdown } /> ;
10821131 }
10831132 if ( assetUrl . _tag !== "Success" ) {
10841133 return (
10851134 < span
1135+ data-markdown-copy = { props . copyMarkdown }
10861136 role = "status"
10871137 aria-label = "Loading image"
1088- className = "my-1 block aspect-video w-full max-w-[30rem] rounded-lg bg-muted/60"
1138+ className = { cn (
1139+ CHAT_MARKDOWN_WORKSPACE_IMAGE_LAYOUT_CLASS_NAME ,
1140+ "aspect-video w-64 max-w-full rounded-lg bg-muted/60" ,
1141+ CHAT_MARKDOWN_IMAGE_BOUNDS_CLASS_NAME ,
1142+ ) }
1143+ style = { props . style }
10891144 />
10901145 ) ;
10911146 }
10921147 return (
10931148 < img
1094- src = { assetUrl . url }
1149+ src = { assetUrl . url + props . srcFragment }
10951150 alt = { props . alt }
1151+ data-markdown-copy = { props . copyMarkdown }
10961152 loading = "lazy"
10971153 draggable = { false }
10981154 className = { CHAT_MARKDOWN_WORKSPACE_IMAGE_CLASS_NAME }
1155+ style = { props . style }
10991156 onError = { ( ) => setFailedUrl ( assetUrl . url ) }
11001157 />
11011158 ) ;
@@ -1636,6 +1693,7 @@ function ChatMarkdown({
16361693 className,
16371694 lineBreaks = false ,
16381695 parseRawHtml = true ,
1696+ imageBaseDir,
16391697} : ChatMarkdownProps ) {
16401698 const { resolvedTheme } = useTheme ( ) ;
16411699 const createAssetUrl = useAtomQueryRunner ( assetEnvironment . createUrl , {
@@ -1729,6 +1787,7 @@ function ChatMarkdown({
17291787 return buildFileLinkParentSuffixByPath ( filePaths ) ;
17301788 } , [ inlineCodeFileLinkMetaByText , markdownFileLinkMetaByHref ] ) ;
17311789 const markdownUrlTransform = useCallback ( ( href : string ) => {
1790+ if ( isWindowsDrivePathHref ( href ) ) return href ;
17321791 return rewriteMarkdownFileUriHref ( href ) ?? defaultUrlTransform ( href ) ;
17331792 } , [ ] ) ;
17341793 // Re-emit highlighted content as markdown so copying out of the rendered
@@ -2146,10 +2205,19 @@ function ChatMarkdown({
21462205 </ code >
21472206 ) ;
21482207 } ,
2149- img ( { node : _node , title : _title , src, alt, ...props } ) {
2150- const srcString = typeof src === "string" ? normalizeMarkdownLinkDestination ( src ) : "" ;
2208+ img ( { node, title, src, alt, ...props } ) {
2209+ const localSrc = node ?. properties ?. dataLocalSrc ;
2210+ const markdownTitle = node ?. properties ?. dataMarkdownTitle ;
2211+ const authoredSrc = typeof localSrc === "string" ? localSrc : src ;
2212+ const authoredTitle = typeof markdownTitle === "string" ? markdownTitle : title ;
2213+ const srcString =
2214+ typeof authoredSrc === "string" ? normalizeMarkdownLinkDestination ( authoredSrc ) : "" ;
2215+ const classifiedSrc =
2216+ typeof localSrc === "string" ? srcString . replaceAll ( "\\" , "/" ) : srcString ;
21512217 const altText = alt ?? "" ;
2152- const imageSource = classifyMarkdownImageSource ( srcString , cwd ) ;
2218+ const copyMarkdown = markdownImageCopy ( altText , srcString , authoredTitle ) ;
2219+ const authoredSizeStyle = authoredImageSizeStyle ( props . width , props . height ) ;
2220+ const imageSource = classifyMarkdownImageSource ( classifiedSrc , imageBaseDir ?? cwd ) ;
21532221 if ( imageSource . _tag === "Direct" ) {
21542222 return (
21552223 < img
@@ -2158,6 +2226,7 @@ function ChatMarkdown({
21582226 alt = { altText }
21592227 loading = "lazy"
21602228 className = { cn ( props . className , CHAT_MARKDOWN_IMAGE_SIZE_CLASS_NAME ) }
2229+ style = { authoredSizeStyle }
21612230 />
21622231 ) ;
21632232 }
@@ -2167,10 +2236,13 @@ function ChatMarkdown({
21672236 threadRef = { threadRef }
21682237 path = { imageSource . path }
21692238 alt = { altText }
2239+ copyMarkdown = { copyMarkdown }
2240+ srcFragment = { markdownImageSourceFragment ( classifiedSrc ) }
2241+ style = { authoredSizeStyle }
21702242 />
21712243 ) ;
21722244 }
2173- return < ChatMarkdownImageFallback alt = { altText } /> ;
2245+ return < ChatMarkdownImageFallback alt = { altText } copyMarkdown = { copyMarkdown } /> ;
21742246 } ,
21752247 table ( { node : _node , ...props } ) {
21762248 return < MarkdownTable { ...props } /> ;
@@ -2213,6 +2285,7 @@ function ChatMarkdown({
22132285 diffThemeName ,
22142286 fileLinkParentSuffixByPath ,
22152287 inlineCodeFileLinkMetaByText ,
2288+ imageBaseDir ,
22162289 isStreaming ,
22172290 markdownFileLinkMetaByHref ,
22182291 onTaskListChange ,
0 commit comments