@@ -17,6 +17,12 @@ function stripQuote(line: string): string {
1717 return line . replace ( QUOTE_RE , "" ) ;
1818}
1919
20+ function quoteDepth ( line : string ) : number {
21+ const match = QUOTE_RE . exec ( line ) ;
22+ if ( ! match ) return 0 ;
23+ return match [ 0 ] . split ( ">" ) . length - 1 ;
24+ }
25+
2026function fenceLanguage ( info : string ) : string {
2127 return info . trim ( ) . split ( / \s + / , 1 ) [ 0 ] ?. toLowerCase ( ) ?? "" ;
2228}
@@ -29,11 +35,19 @@ function fenceHash(source: string): string {
2935 return `m${ ( hash >>> 0 ) . toString ( 36 ) } ` ;
3036}
3137
32- function closerIndex ( lines : readonly string [ ] , from : number , fence : string ) : number {
38+ function closerIndex (
39+ lines : readonly string [ ] ,
40+ depths : readonly number [ ] ,
41+ from : number ,
42+ fence : string ,
43+ minDepth : number ,
44+ ) : number {
3345 const char = fence [ 0 ] === "`" ? "`" : "~" ;
3446 const closeRe = new RegExp ( `^ {0,3}${ char } {${ fence . length } ,}[ \\t]*$` ) ;
3547 for ( let scan = from ; scan < lines . length ; scan += 1 ) {
36- if ( closeRe . test ( lines [ scan ] ?? "" ) ) return scan ;
48+ // A fenced block ends when its blockquote ends, so only a fence at the
49+ // opener's quote depth or deeper can close it.
50+ if ( ( depths [ scan ] ?? 0 ) >= minDepth && closeRe . test ( lines [ scan ] ?? "" ) ) return scan ;
3751 }
3852 return - 1 ;
3953}
@@ -42,12 +56,13 @@ function closerIndex(lines: readonly string[], from: number, fence: string): num
4256function collectDefinitions ( markdown : string ) : string [ ] {
4357 const found : string [ ] = [ ] ;
4458 const lines = markdown . split ( "\n" ) . map ( stripQuote ) ;
59+ const depths = markdown . split ( "\n" ) . map ( quoteDepth ) ;
4560 let index = 0 ;
4661 while ( index < lines . length ) {
4762 const line = lines [ index ] ?? "" ;
4863 const open = FENCE_RE . exec ( line ) ;
4964 if ( open ) {
50- const close = closerIndex ( lines , index + 1 , open [ 1 ] ?? "```" ) ;
65+ const close = closerIndex ( lines , depths , index + 1 , open [ 1 ] ?? "```" , depths [ index ] ?? 0 ) ;
5166 index = close === - 1 ? lines . length : close + 1 ;
5267 continue ;
5368 }
@@ -69,6 +84,7 @@ export function splitMarkdownWithMermaid(markdown: string): MermaidMarkdownSegme
6984 > = [ ] ;
7085 const lines = markdown . split ( "\n" ) ;
7186 const text = lines . map ( stripQuote ) ;
87+ const depths = lines . map ( quoteDepth ) ;
7288 let markdownStart = 0 ;
7389 let index = 0 ;
7490
@@ -89,7 +105,8 @@ export function splitMarkdownWithMermaid(markdown: string): MermaidMarkdownSegme
89105 continue ;
90106 }
91107 const fence = open [ 1 ] ?? "```" ;
92- const close = closerIndex ( text , index + 1 , fence ) ;
108+ const depth = depths [ index ] ?? 0 ;
109+ const close = closerIndex ( text , depths , index + 1 , fence , depth ) ;
93110 if ( close === - 1 ) {
94111 index += 1 ;
95112 continue ;
0 commit comments