@@ -33,6 +33,7 @@ type SplitMediaFromOutputOptions = {
3333 extractAudioDirectives ?: boolean ;
3434 extractMarkdownImages ?: boolean ;
3535 extractMediaDirectives ?: boolean ;
36+ markdownImageAllowlist ?: readonly string [ ] ;
3637} ;
3738
3839const FILE_URL_PREFIX_RE = / ^ f i l e : (?: \/ \/ ) ? / i;
@@ -243,6 +244,10 @@ function unwrapQuoted(value: string): string | undefined {
243244 return trimmed . slice ( 1 , - 1 ) . trim ( ) ;
244245}
245246
247+ function normalizeMarkdownImageDestination ( destination : string ) : string {
248+ return normalizeMediaSource ( cleanCandidate ( unwrapQuoted ( destination ) ?? destination ) ) ;
249+ }
250+
246251function mayContainFenceMarkers ( input : string ) : boolean {
247252 return input . includes ( "```" ) || input . includes ( "~~~" ) ;
248253}
@@ -448,7 +453,11 @@ function findMarkdownImageMatches(line: string): MarkdownImageMatch[] {
448453 return matches ;
449454}
450455
451- function collectMarkdownImageSegments ( params : { line : string ; media : string [ ] } ) : {
456+ function collectMarkdownImageSegments ( params : {
457+ line : string ;
458+ media : string [ ] ;
459+ allowlist ?: ReadonlyMap < string , string > ;
460+ } ) : {
452461 cleanedLine ?: string ;
453462 lineSegments : ParsedMediaOutputSegment [ ] ;
454463 foundMedia : boolean ;
@@ -469,17 +478,17 @@ function collectMarkdownImageSegments(params: { line: string; media: string[] })
469478 segmentPieces . push ( before ) ;
470479 visiblePieces . push ( before ) ;
471480
472- const target = normalizeMediaSource (
473- cleanCandidate ( unwrapQuoted ( match . destination ) ?? match . destination ) ,
474- ) ;
475- if ( isRemoteMarkdownImageMedia ( target ) ) {
481+ const target = normalizeMarkdownImageDestination ( match . destination ) ;
482+ const selectedTarget = params . allowlist ?. get ( target ) ;
483+ if ( selectedTarget || ( ! params . allowlist && isRemoteMarkdownImageMedia ( target ) ) ) {
476484 const beforeText = cleanLineText ( segmentPieces . join ( "" ) ) ;
477485 if ( beforeText ) {
478486 lineSegments . push ( { type : "text" , text : beforeText } ) ;
479487 }
480488 segmentPieces . length = 0 ;
481- params . media . push ( target ) ;
482- lineSegments . push ( { type : "media" , url : target } ) ;
489+ const mediaTarget = selectedTarget ?? target ;
490+ params . media . push ( mediaTarget ) ;
491+ lineSegments . push ( { type : "media" , url : mediaTarget } ) ;
483492 foundMedia = true ;
484493 } else {
485494 const original = params . line . slice ( match . start , match . end ) ;
@@ -522,7 +531,17 @@ export function splitMediaFromOutput(
522531 if ( ! trimmedRaw . trim ( ) ) {
523532 return { text : "" } ;
524533 }
525- const extractMarkdownImages = options . extractMarkdownImages === true ;
534+ const markdownImageAllowlist =
535+ options . markdownImageAllowlist === undefined
536+ ? undefined
537+ : new Map (
538+ options . markdownImageAllowlist . map ( ( source ) => [
539+ normalizeMarkdownImageDestination ( source ) ,
540+ source ,
541+ ] ) ,
542+ ) ;
543+ const extractMarkdownImages =
544+ markdownImageAllowlist !== undefined || options . extractMarkdownImages === true ;
526545 const extractMediaDirectives = options . extractMediaDirectives !== false ;
527546 const mayContainMediaToken = extractMediaDirectives && / m e d i a : / i. test ( trimmedRaw ) ;
528547 const mayContainMarkdownImage = extractMarkdownImages && / ! \[ [ ^ \] ] * ] \( / . test ( trimmedRaw ) ;
@@ -571,7 +590,7 @@ export function splitMediaFromOutput(
571590 const trimmedStart = line . trimStart ( ) ;
572591 if ( ! extractMediaDirectives || ! trimmedStart . toUpperCase ( ) . startsWith ( "MEDIA:" ) ) {
573592 const markdownImageResult = extractMarkdownImages
574- ? collectMarkdownImageSegments ( { line, media } )
593+ ? collectMarkdownImageSegments ( { line, media, allowlist : markdownImageAllowlist } )
575594 : { lineSegments : [ ] , foundMedia : false } ;
576595 if ( ! markdownImageResult . foundMedia ) {
577596 keptLines . push ( line ) ;
0 commit comments