@@ -140,13 +140,13 @@ const MARKDOWN_LINK_DEFINITION_PATTERN =
140140const ANGLE_AUTOLINK_PATTERN = / < ( [ ^ < > \n ] + ) > / g;
141141const PROTECTED_MARKDOWN_SPAN_PATTERN = / ! ? \[ [ ^ \] ] * ] \( [ ^ ) ] * \) | ! ? \[ [ ^ \] ] * ] \[ [ ^ \] ] * ] | < [ ^ > \n ] + > / g;
142142const BARE_WINDOWS_PATH_PATTERN =
143- / (?< ! [ A - Z a - z 0 - 9 _ / ] ) (?: [ A - Z a - z ] : [ \\ / ] [ ^ \s < > [ \] ( ) ` ] + | \\ \\ [ ^ \s < > [ \] ( ) ` \\ / ] + (?: [ \\ / ] [ ^ \s < > [ \] ( ) ` ] + ) + ) / g;
143+ / (?< ! [ A - Z a - z 0 - 9 _ / ] ) (?: [ A - Z a - z ] : [ \\ / ] [ ^ \s < > [ \] ( ) ` * ] + | \\ \\ [ ^ \s < > [ \] ( ) ` * \\ / ] + (?: [ \\ / ] [ ^ \s < > [ \] ( ) ` * ] + ) + ) / g;
144144const AUTOLINK_TRAILING_PUNCTUATION_PATTERN = / [ . , ; : ! ? ' ) \] } " ] + $ / ;
145145
146146/**
147147 * Rewrites Windows drive/UNC markdown destinations and bare paths to `file://`
148148 * URLs so rehype-sanitize does not treat `D:` as an unknown protocol.
149- * Leaves fenced code, inline code, and non-file links alone.
149+ * Leaves fenced code, indented code, inline code, and non-file links alone.
150150 */
151151export function normalizeWindowsMarkdownFileLinks ( markdown : string ) : string {
152152 if ( markdown . length === 0 || ! HAS_WINDOWS_FILESYSTEM_PATH_PATTERN . test ( markdown ) ) {
@@ -258,8 +258,9 @@ function rewriteWindowsMarkdownInText(text: string): string {
258258 return autolinkBareWindowsPaths ( rewriteWindowsAngleAutolinks ( withDestinations ) ) ;
259259}
260260
261- const FENCED_CODE_SEGMENT_PATTERN = / ( ` ` ` [ \s \S ] * ?(?: ` ` ` | $ ) | ~ ~ ~ [ \s \S ] * ?(?: ~ ~ ~ | $ ) ) / ;
262261const INLINE_CODE_SEGMENT_PATTERN = / ( ` [ ^ ` \n ] + ` ) / ;
262+ const OPEN_FENCE_PATTERN = / ^ ( { 0 , 3 } ) ( ` { 3 , } | ~ { 3 , } ) ( .* ) $ / ;
263+ const CLOSE_FENCE_PATTERN = / ^ ( { 0 , 3 } ) ( ` { 3 , } | ~ { 3 , } ) [ \t ] * $ / ;
263264
264265function mapOutsideInlineCode ( text : string , transform : ( chunk : string ) => string ) : string {
265266 return text
@@ -268,11 +269,70 @@ function mapOutsideInlineCode(text: string, transform: (chunk: string) => string
268269 . join ( "" ) ;
269270}
270271
272+ function lineWithoutCarriageReturn ( line : string ) : string {
273+ return line . endsWith ( "\r" ) ? line . slice ( 0 , - 1 ) : line ;
274+ }
275+
276+ function openingFenceMarker ( line : string ) : string | null {
277+ const match = OPEN_FENCE_PATTERN . exec ( line ) ;
278+ if ( ! match ?. [ 2 ] ) return null ;
279+ const marker = match [ 2 ] ;
280+ const info = match [ 3 ] ?? "" ;
281+ if ( marker . startsWith ( "`" ) && info . includes ( "`" ) ) return null ;
282+ return marker ;
283+ }
284+
285+ function isClosingFenceLine ( line : string , openMarker : string ) : boolean {
286+ const match = CLOSE_FENCE_PATTERN . exec ( line ) ;
287+ if ( ! match ?. [ 2 ] ) return false ;
288+ const marker = match [ 2 ] ;
289+ return marker [ 0 ] === openMarker [ 0 ] && marker . length >= openMarker . length ;
290+ }
291+
292+ function isIndentedCodeLine ( line : string ) : boolean {
293+ return line . startsWith ( " " ) || line . startsWith ( "\t" ) ;
294+ }
295+
271296function mapMarkdownOutsideCode ( markdown : string , transform : ( text : string ) => string ) : string {
272- return markdown
273- . split ( FENCED_CODE_SEGMENT_PATTERN )
274- . map ( ( segment , index ) => ( index % 2 === 1 ? segment : mapOutsideInlineCode ( segment , transform ) ) )
275- . join ( "" ) ;
297+ let output = "" ;
298+ let index = 0 ;
299+ let openFence : string | null = null ;
300+
301+ while ( index < markdown . length ) {
302+ const lineEnd = markdown . indexOf ( "\n" , index ) ;
303+ const lineLimit = lineEnd === - 1 ? markdown . length : lineEnd ;
304+ const line = markdown . slice ( index , lineLimit ) ;
305+ const newline = lineEnd === - 1 ? "" : "\n" ;
306+ const matchLine = lineWithoutCarriageReturn ( line ) ;
307+
308+ if ( openFence !== null ) {
309+ output += line + newline ;
310+ if ( isClosingFenceLine ( matchLine , openFence ) ) {
311+ openFence = null ;
312+ }
313+ index = lineLimit + newline . length ;
314+ continue ;
315+ }
316+
317+ const fence = openingFenceMarker ( matchLine ) ;
318+ if ( fence !== null ) {
319+ openFence = fence ;
320+ output += line + newline ;
321+ index = lineLimit + newline . length ;
322+ continue ;
323+ }
324+
325+ if ( isIndentedCodeLine ( matchLine ) ) {
326+ output += line + newline ;
327+ index = lineLimit + newline . length ;
328+ continue ;
329+ }
330+
331+ output += mapOutsideInlineCode ( line , transform ) + newline ;
332+ index = lineLimit + newline . length ;
333+ }
334+
335+ return output ;
276336}
277337
278338function looksLikePosixFilesystemPath ( path : string ) : boolean {
0 commit comments