@@ -109,11 +109,44 @@ function rehypePreserveBareAnchorPlaceholders() {
109109 } ;
110110}
111111
112+ const COLOR_SCHEME_MEDIA = / ^ \( \s * p r e f e r s - c o l o r - s c h e m e \s * : \s * ( d a r k | l i g h t ) \s * \) $ / i;
113+
114+ /**
115+ * The renderer resolves an `<img>`'s `src` (workspace files, host media) but never a `<source>`'s
116+ * `srcset`, which would load unresolved whenever its media matched. A color-scheme source moves
117+ * onto the `<img>` for the renderer to pick by theme; every source is then dropped.
118+ */
119+ function moveColorSchemeSourcesToImage ( picture : MarkdownImageHastNode ) {
120+ if ( ! picture . children ) return ;
121+ const image = picture . children . find ( ( child ) => child . tagName === "img" ) ;
122+ for ( const source of picture . children ) {
123+ if ( source . tagName !== "source" || ! image ) continue ;
124+ const media = source . properties ?. media ;
125+ const scheme = typeof media === "string" ? COLOR_SCHEME_MEDIA . exec ( media ) ?. [ 1 ] : undefined ;
126+ const srcSet = source . properties ?. srcSet ;
127+ const candidate = Array . isArray ( srcSet ) ? srcSet [ 0 ] : srcSet ;
128+ // A candidate's URL runs to the first space (a descriptor such as `2x` may follow).
129+ const url =
130+ typeof candidate === "string" ? candidate . trim ( ) . split ( / \s / , 1 ) [ 0 ] ?. replace ( / , + $ / , "" ) : "" ;
131+ const key = scheme ?. toLowerCase ( ) === "dark" ? "dataDarkSrc" : "dataLightSrc" ;
132+ // As in a browser, the first source that matches a scheme is the one shown.
133+ if ( scheme && url && image . properties ?. [ key ] === undefined ) {
134+ // The sanitizer reads a drive letter as a protocol; as a file URL it resolves to the same path.
135+ const value = isWindowsDrivePathHref ( url ) ? `file:///${ url . replaceAll ( "\\" , "/" ) } ` : url ;
136+ image . properties = { ...image . properties , [ key ] : value } ;
137+ }
138+ }
139+ picture . children = picture . children . filter ( ( child ) => child . tagName !== "source" ) ;
140+ }
141+
112142function rehypePreserveImageSourceMeta ( ) {
113143 return ( tree : MarkdownImageHastNode ) => {
114144 const visit = ( node : MarkdownImageHastNode ) => {
115145 const src = node . properties ?. src ;
116146 const title = node . properties ?. title ;
147+ if ( node . type === "element" && node . tagName === "picture" ) {
148+ moveColorSchemeSourcesToImage ( node ) ;
149+ }
117150 if ( node . type === "element" && node . tagName === "img" ) {
118151 node . properties = {
119152 ...node . properties ,
@@ -129,6 +162,8 @@ function rehypePreserveImageSourceMeta() {
129162 } ;
130163}
131164
165+ const IMAGE_SRC_PROTOCOLS = [ ...( defaultSchema . protocols ?. src ?? [ ] ) , "file" , "t3-context" ] ;
166+
132167const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
133168 ...defaultSchema ,
134169 attributes : {
@@ -145,6 +180,8 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
145180 a : [ ...( defaultSchema . attributes ?. a ?? [ ] ) , "dataPullRequestAutolink" ] ,
146181 img : [
147182 ...( defaultSchema . attributes ?. img ?? [ ] ) ,
183+ "dataDarkSrc" ,
184+ "dataLightSrc" ,
148185 "dataLocalSrc" ,
149186 "dataMarkdownTitle" ,
150187 "dataStandalone" ,
@@ -159,7 +196,9 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
159196 "t3-context" ,
160197 THREAD_LINK_PROTOCOL ,
161198 ] ,
162- src : [ ...( defaultSchema . protocols ?. src ?? [ ] ) , "file" , "t3-context" ] ,
199+ src : IMAGE_SRC_PROTOCOLS ,
200+ dataDarkSrc : IMAGE_SRC_PROTOCOLS ,
201+ dataLightSrc : IMAGE_SRC_PROTOCOLS ,
163202 } ,
164203} satisfies Parameters < typeof rehypeSanitize > [ 0 ] ;
165204
@@ -388,7 +427,12 @@ const imageSourceProcessor = unified()
388427interface ImageSourceNode {
389428 readonly type : string ;
390429 readonly tagName ?: string ;
391- readonly properties ?: { readonly src ?: unknown ; readonly dataLocalSrc ?: unknown } ;
430+ readonly properties ?: {
431+ readonly src ?: unknown ;
432+ readonly dataLocalSrc ?: unknown ;
433+ readonly dataDarkSrc ?: unknown ;
434+ readonly dataLightSrc ?: unknown ;
435+ } ;
392436 readonly children ?: ReadonlyArray < ImageSourceNode > ;
393437}
394438
@@ -400,13 +444,17 @@ export function chatMarkdownImageSources(markdown: string): Array<string> {
400444 const sources : Array < string > = [ ] ;
401445 const visit = ( node : ImageSourceNode ) => {
402446 if ( node . type === "element" && node . tagName === "img" ) {
403- const { src, dataLocalSrc } = node . properties ?? { } ;
447+ const { src, dataLocalSrc, dataDarkSrc , dataLightSrc } = node . properties ?? { } ;
404448 // The sanitizer drops a Windows drive path from `src`; the renderer reads it from here.
405449 if ( typeof dataLocalSrc === "string" ) {
406450 sources . push ( normalizeMarkdownLinkDestination ( dataLocalSrc ) . replaceAll ( "\\" , "/" ) ) ;
407451 } else if ( typeof src === "string" ) {
408452 sources . push ( normalizeMarkdownLinkDestination ( src ) ) ;
409453 }
454+ // A `<picture>` shows one of these instead, by the viewer's theme.
455+ for ( const variant of [ dataDarkSrc , dataLightSrc ] ) {
456+ if ( typeof variant === "string" ) sources . push ( normalizeMarkdownLinkDestination ( variant ) ) ;
457+ }
410458 }
411459 node . children ?. forEach ( visit ) ;
412460 } ;
0 commit comments