@@ -382,33 +382,41 @@ function convertBackslashBlockDelimiters(value: string) {
382382 const output : string [ ] = [ ] ;
383383 let collectingBlock = false ;
384384 let blockLines : string [ ] = [ ] ;
385- let activeFencePrefix = "" ;
385+ let activeStartFencePrefix = "" ;
386+ let activeContinuationFencePrefix = "" ;
386387 let activeQuoteDepth = 0 ;
387388 let activeOpenLine = "" ;
388389
389- const toFencePrefix = ( prefix : string ) =>
390+ const toContinuationFencePrefix = ( prefix : string ) =>
390391 prefix . replace ( LIST_MARKER_PREFIX_PATTERN , ( marker ) => " " . repeat ( marker . length ) ) ;
391392
392393 for ( const line of lines ) {
393394 const { prefix, content, quoteDepth } = parseLinePrefix ( line ) ;
395+ const normalizedContent = content . trimStart ( ) ;
394396
395397 if ( ! collectingBlock ) {
396- const singleLineMatch = content . match ( BLOCK_LATEX_SINGLE_LINE_PATTERN ) ;
398+ const singleLineMatch = normalizedContent . match ( BLOCK_LATEX_SINGLE_LINE_PATTERN ) ;
397399 if ( singleLineMatch ) {
398400 const body = ( singleLineMatch [ 1 ] ?? "" ) . trim ( ) ;
399401 if ( ! body ) {
400402 output . push ( line ) ;
401403 continue ;
402404 }
403- const fencePrefix = toFencePrefix ( prefix ) ;
404- output . push ( `${ fencePrefix } $$` , `${ fencePrefix } ${ body } ` , `${ fencePrefix } $$` ) ;
405+ const startFencePrefix = prefix ;
406+ const continuationFencePrefix = toContinuationFencePrefix ( prefix ) ;
407+ output . push (
408+ `${ startFencePrefix } $$` ,
409+ `${ continuationFencePrefix } ${ body } ` ,
410+ `${ continuationFencePrefix } $$` ,
411+ ) ;
405412 continue ;
406413 }
407414
408- if ( BLOCK_LATEX_OPEN_PATTERN . test ( content ) ) {
415+ if ( BLOCK_LATEX_OPEN_PATTERN . test ( normalizedContent ) ) {
409416 collectingBlock = true ;
410417 blockLines = [ ] ;
411- activeFencePrefix = toFencePrefix ( prefix ) ;
418+ activeStartFencePrefix = prefix ;
419+ activeContinuationFencePrefix = toContinuationFencePrefix ( prefix ) ;
412420 activeQuoteDepth = quoteDepth ;
413421 activeOpenLine = line ;
414422 continue ;
@@ -418,15 +426,20 @@ function convertBackslashBlockDelimiters(value: string) {
418426 continue ;
419427 }
420428
421- if ( BLOCK_LATEX_CLOSE_PATTERN . test ( content ) && quoteDepth === activeQuoteDepth ) {
429+ if ( BLOCK_LATEX_CLOSE_PATTERN . test ( normalizedContent ) && quoteDepth === activeQuoteDepth ) {
422430 if ( blockLines . some ( ( bodyLine ) => bodyLine . trim ( ) . length > 0 ) ) {
423- output . push ( `${ activeFencePrefix } $$` , ...blockLines , `${ activeFencePrefix } $$` ) ;
431+ output . push (
432+ `${ activeStartFencePrefix } $$` ,
433+ ...blockLines ,
434+ `${ activeContinuationFencePrefix } $$` ,
435+ ) ;
424436 } else {
425437 output . push ( activeOpenLine , ...blockLines , line ) ;
426438 }
427439 collectingBlock = false ;
428440 blockLines = [ ] ;
429- activeFencePrefix = "" ;
441+ activeStartFencePrefix = "" ;
442+ activeContinuationFencePrefix = "" ;
430443 activeQuoteDepth = 0 ;
431444 activeOpenLine = "" ;
432445 continue ;
0 commit comments