@@ -9,7 +9,8 @@ export type MermaidMarkdownSegment =
99 readonly key : string ;
1010 } ;
1111
12- const FENCE_RE = / ^ { 0 , 3 } ( ` ` ` | ~ ~ ~ ) [ \t ] * ( [ ^ \n ] * ) \n ? / ;
12+ const FENCE_RE = / ^ { 0 , 3 } ( ` { 3 , } | ~ { 3 , } ) [ \t ] * ( [ ^ \n ] * ) \n ? / ;
13+ const DEFINITION_RE = / ^ { 0 , 3 } \[ [ ^ \] \n ] + \] : [ \t ] * \S .* $ / ;
1314
1415function fenceLanguage ( info : string ) : string {
1516 return info . trim ( ) . split ( / \s + / , 1 ) [ 0 ] ?. toLowerCase ( ) ?? "" ;
@@ -23,55 +24,93 @@ function fenceHash(source: string): string {
2324 return `m${ ( hash >>> 0 ) . toString ( 36 ) } ` ;
2425}
2526
27+ function closerIndex ( lines : readonly string [ ] , from : number , fence : string ) : number {
28+ const char = fence [ 0 ] === "`" ? "`" : "~" ;
29+ const closeRe = new RegExp ( `^ {0,3}${ char } {${ fence . length } ,}[ \\t]*$` ) ;
30+ for ( let scan = from ; scan < lines . length ; scan += 1 ) {
31+ if ( closeRe . test ( lines [ scan ] ?? "" ) ) return scan ;
32+ }
33+ return - 1 ;
34+ }
35+
2636export function splitMarkdownWithMermaid ( markdown : string ) : MermaidMarkdownSegment [ ] {
27- const segments : MermaidMarkdownSegment [ ] = [ ] ;
37+ const raw : Array <
38+ | { readonly kind : "markdown" ; readonly markdown : string ; readonly start : number }
39+ | {
40+ readonly kind : "mermaid" ;
41+ readonly source : string ;
42+ readonly id : string ;
43+ readonly start : number ;
44+ }
45+ > = [ ] ;
2846 const lines = markdown . split ( "\n" ) ;
2947 let markdownStart = 0 ;
3048 let index = 0 ;
3149
3250 const pushMarkdown = ( end : number ) => {
3351 if ( end > markdownStart ) {
34- const markdown = lines . slice ( markdownStart , end ) . join ( "\n" ) ;
35- segments . push ( {
52+ raw . push ( {
3653 kind : "markdown" ,
37- markdown,
38- key : `md- ${ markdownStart } - ${ fenceHash ( markdown ) } ` ,
54+ markdown : lines . slice ( markdownStart , end ) . join ( "\n" ) ,
55+ start : markdownStart ,
3956 } ) ;
4057 }
4158 } ;
4259
4360 while ( index < lines . length ) {
4461 const line = lines [ index ] ?? "" ;
4562 const open = FENCE_RE . exec ( line ) ;
46- if ( ! open || fenceLanguage ( open [ 2 ] ?? "" ) !== "mermaid" ) {
63+ if ( ! open ) {
4764 index += 1 ;
4865 continue ;
4966 }
5067 const fence = open [ 1 ] ?? "```" ;
51- const closeRe = new RegExp ( `^ {0,3}${ fence === "```" ? "```" : "~~~" } [ \\t]*$` ) ;
52- let close = - 1 ;
53- for ( let scan = index + 1 ; scan < lines . length ; scan += 1 ) {
54- if ( closeRe . test ( lines [ scan ] ?? "" ) ) {
55- close = scan ;
56- break ;
57- }
58- }
59- // Unclosed fence: leave it as Markdown (streaming or authored that way).
68+ const close = closerIndex ( lines , index + 1 , fence ) ;
6069 if ( close === - 1 ) {
6170 index += 1 ;
6271 continue ;
6372 }
73+ // A non-mermaid fence may document a mermaid example; keep it whole.
74+ if ( fenceLanguage ( open [ 2 ] ?? "" ) !== "mermaid" ) {
75+ index = close + 1 ;
76+ continue ;
77+ }
6478 pushMarkdown ( index ) ;
6579 const source = lines . slice ( index + 1 , close ) . join ( "\n" ) ;
66- segments . push ( {
67- kind : "mermaid" ,
68- source,
69- id : fenceHash ( source ) ,
70- key : `mmd-${ index } -${ fenceHash ( source ) } ` ,
71- } ) ;
80+ raw . push ( { kind : "mermaid" , source, id : fenceHash ( source ) , start : index } ) ;
7281 index = close + 1 ;
7382 markdownStart = index ;
7483 }
7584 pushMarkdown ( lines . length ) ;
76- return segments . filter ( ( segment ) => segment . kind === "mermaid" || segment . markdown . length > 0 ) ;
85+
86+ // Link reference definitions resolve per parser instance, so share them
87+ // with every Markdown segment instead of stranding uses after a fence.
88+ const definitions = [
89+ ...new Set (
90+ raw . flatMap ( ( segment ) =>
91+ segment . kind === "markdown"
92+ ? segment . markdown . split ( "\n" ) . filter ( ( entry ) => DEFINITION_RE . test ( entry ) )
93+ : [ ] ,
94+ ) ,
95+ ) ,
96+ ] ;
97+ const prefix = definitions . length > 0 ? `${ definitions . join ( "\n" ) } \n` : "" ;
98+
99+ return raw
100+ . map ( ( segment ) : MermaidMarkdownSegment => {
101+ if ( segment . kind === "mermaid" ) {
102+ return {
103+ kind : "mermaid" ,
104+ source : segment . source ,
105+ id : segment . id ,
106+ key : `mmd-${ segment . start } ` ,
107+ } ;
108+ }
109+ const needsPrefix =
110+ prefix . length > 0 &&
111+ ! definitions . every ( ( definition ) => segment . markdown . includes ( definition ) ) ;
112+ const text = needsPrefix ? `${ prefix } ${ segment . markdown } ` : segment . markdown ;
113+ return { kind : "markdown" , markdown : text , key : `md-${ segment . start } ` } ;
114+ } )
115+ . filter ( ( segment ) => segment . kind === "mermaid" || segment . markdown . length > 0 ) ;
77116}
0 commit comments