@@ -108,11 +108,41 @@ function rehypePreserveBareAnchorPlaceholders() {
108108 } ;
109109}
110110
111+ 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;
112+
113+ /**
114+ * The renderer resolves an `<img>`'s `src` (workspace files, host media) but never a `<source>`'s
115+ * `srcset`, which would load unresolved whenever its media matched. A color-scheme source moves
116+ * onto the `<img>` for the renderer to pick by theme; every source is then dropped.
117+ */
118+ function moveColorSchemeSourcesToImage ( picture : MarkdownImageHastNode ) {
119+ if ( ! picture . children ) return ;
120+ const image = picture . children . find ( ( child ) => child . tagName === "img" ) ;
121+ for ( const source of picture . children ) {
122+ if ( source . tagName !== "source" || ! image ) continue ;
123+ const media = source . properties ?. media ;
124+ const scheme = typeof media === "string" ? COLOR_SCHEME_MEDIA . exec ( media ) ?. [ 1 ] : undefined ;
125+ const srcSet = source . properties ?. srcSet ;
126+ const candidate = Array . isArray ( srcSet ) ? srcSet [ 0 ] : srcSet ;
127+ // A candidate's URL runs to the first space (a descriptor such as `2x` may follow).
128+ const url =
129+ typeof candidate === "string" ? candidate . trim ( ) . split ( / \s / , 1 ) [ 0 ] ?. replace ( / , + $ / , "" ) : "" ;
130+ if ( scheme && url ) {
131+ const key = scheme . toLowerCase ( ) === "dark" ? "dataDarkSrc" : "dataLightSrc" ;
132+ image . properties = { ...image . properties , [ key ] : url } ;
133+ }
134+ }
135+ picture . children = picture . children . filter ( ( child ) => child . tagName !== "source" ) ;
136+ }
137+
111138function rehypePreserveImageSourceMeta ( ) {
112139 return ( tree : MarkdownImageHastNode ) => {
113140 const visit = ( node : MarkdownImageHastNode ) => {
114141 const src = node . properties ?. src ;
115142 const title = node . properties ?. title ;
143+ if ( node . type === "element" && node . tagName === "picture" ) {
144+ moveColorSchemeSourcesToImage ( node ) ;
145+ }
116146 if ( node . type === "element" && node . tagName === "img" ) {
117147 node . properties = {
118148 ...node . properties ,
@@ -128,6 +158,8 @@ function rehypePreserveImageSourceMeta() {
128158 } ;
129159}
130160
161+ const IMAGE_SRC_PROTOCOLS = [ ...( defaultSchema . protocols ?. src ?? [ ] ) , "file" , "t3-context" ] ;
162+
131163const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
132164 ...defaultSchema ,
133165 attributes : {
@@ -144,6 +176,8 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
144176 a : [ ...( defaultSchema . attributes ?. a ?? [ ] ) , "dataPullRequestAutolink" ] ,
145177 img : [
146178 ...( defaultSchema . attributes ?. img ?? [ ] ) ,
179+ "dataDarkSrc" ,
180+ "dataLightSrc" ,
147181 "dataLocalSrc" ,
148182 "dataMarkdownTitle" ,
149183 "dataStandalone" ,
@@ -158,7 +192,9 @@ const CHAT_MARKDOWN_SANITIZE_SCHEMA = {
158192 "t3-context" ,
159193 THREAD_LINK_PROTOCOL ,
160194 ] ,
161- src : [ ...( defaultSchema . protocols ?. src ?? [ ] ) , "file" , "t3-context" ] ,
195+ src : IMAGE_SRC_PROTOCOLS ,
196+ dataDarkSrc : IMAGE_SRC_PROTOCOLS ,
197+ dataLightSrc : IMAGE_SRC_PROTOCOLS ,
162198 } ,
163199} satisfies Parameters < typeof rehypeSanitize > [ 0 ] ;
164200
@@ -316,7 +352,12 @@ const imageSourceProcessor = unified()
316352interface ImageSourceNode {
317353 readonly type : string ;
318354 readonly tagName ?: string ;
319- readonly properties ?: { readonly src ?: unknown ; readonly dataLocalSrc ?: unknown } ;
355+ readonly properties ?: {
356+ readonly src ?: unknown ;
357+ readonly dataLocalSrc ?: unknown ;
358+ readonly dataDarkSrc ?: unknown ;
359+ readonly dataLightSrc ?: unknown ;
360+ } ;
320361 readonly children ?: ReadonlyArray < ImageSourceNode > ;
321362}
322363
@@ -328,13 +369,17 @@ export function chatMarkdownImageSources(markdown: string): Array<string> {
328369 const sources : Array < string > = [ ] ;
329370 const visit = ( node : ImageSourceNode ) => {
330371 if ( node . type === "element" && node . tagName === "img" ) {
331- const { src, dataLocalSrc } = node . properties ?? { } ;
372+ const { src, dataLocalSrc, dataDarkSrc , dataLightSrc } = node . properties ?? { } ;
332373 // The sanitizer drops a Windows drive path from `src`; the renderer reads it from here.
333374 if ( typeof dataLocalSrc === "string" ) {
334375 sources . push ( normalizeMarkdownLinkDestination ( dataLocalSrc ) . replaceAll ( "\\" , "/" ) ) ;
335376 } else if ( typeof src === "string" ) {
336377 sources . push ( normalizeMarkdownLinkDestination ( src ) ) ;
337378 }
379+ // A `<picture>` shows one of these instead, by the viewer's theme.
380+ for ( const variant of [ dataDarkSrc , dataLightSrc ] ) {
381+ if ( typeof variant === "string" ) sources . push ( normalizeMarkdownLinkDestination ( variant ) ) ;
382+ }
338383 }
339384 node . children ?. forEach ( visit ) ;
340385 } ;
0 commit comments