@@ -326,7 +326,7 @@ export const plugin: Plugin<[FlexibleContainerOptions?], Root> = (options) => {
326326 * and the remaining content.
327327 *
328328 */
329- function parseFence (
329+ function parseFenceTypeTitle (
330330 initialValue : string ,
331331 fence : string ,
332332 ) : {
@@ -399,7 +399,7 @@ export const plugin: Plugin<[FlexibleContainerOptions?], Root> = (options) => {
399399 function analyzeChild ( node : Paragraph , fence : string ) : AnalyzeResult {
400400 const textElement = node . children [ 0 ] as Text ; // it is guarenteed in "getOpeningFence"
401401
402- let { type, title, rest } = parseFence ( textElement . value , fence ) ;
402+ const { type, title, rest } = parseFenceTypeTitle ( textElement . value , fence ) ;
403403
404404 if ( ! rest ) {
405405 // It is regular container
@@ -436,46 +436,13 @@ export const plugin: Plugin<[FlexibleContainerOptions?], Root> = (options) => {
436436 const firstElement = node . children [ 0 ] as Text ; // it is guarenteed in "getOpeningFence"
437437
438438 let flag : AnalyzeFlag = "mutated" ; // it has more children means it can not be "regular"
439- let type : string | undefined = undefined ;
440- let title : string | undefined = undefined ;
441- let nIndex : number = - 1 ; // for newline "\n" character
442439 const paragraphChildren : PhrasingContent [ ] = [ ] ;
443440
444- if ( ! firstElement . value . includes ( "\n" ) ) {
445- // means there is a Phrase other than Text Phrase after
446- const match = firstElement . value . match ( REGEX_START ) ;
441+ const { type, title, rest } = parseFenceTypeTitle ( firstElement . value , fence ) ;
447442
448- type = match ! [ 2 ] ;
449- title = match ! [ 3 ] ;
450- } else {
451- let value = firstElement . value
452- . replace ( new RegExp ( `^${ fence } ` ) , "" )
453- // remove (space, tab) but exclude \r and \n in the beginning
454- . replace ( / ^ [ ^ \S \r \n ] + / , "" ) ;
455-
456- nIndex = value . indexOf ( "\n" ) ;
457-
458- if ( nIndex === 0 ) {
459- // means that there is no "type" and "title"
460-
461- // remove the newline "\n" in the beginning, and get the rest of the value
462- value = value . slice ( 1 ) ;
463- } else {
464- // means that there is a "type" and/or a "title"
465-
466- // get the type and the title
467- const params = value . substring ( 0 , nIndex ) ;
468- const match = params . match ( / ( [ \w - ] + ) \s * ( .* [ ^ \n ] ) ? / u) ; // two matching groups: the first word and the rest
469-
470- type = match ! [ 1 ] ;
471- title = match ! [ 2 ] ;
472-
473- // remove upto newline "\n" in the beginning, get the rest of the value
474- value = value . slice ( nIndex + 1 ) ;
475- }
476-
477- // mutate the first element value after extracting type and title
478- firstElement . value = value ;
443+ if ( rest ) {
444+ // mutate the first Phrase
445+ firstElement . value = rest ;
479446
480447 paragraphChildren . push ( firstElement ) ;
481448 }
@@ -487,22 +454,18 @@ export const plugin: Plugin<[FlexibleContainerOptions?], Root> = (options) => {
487454
488455 const lastElement = node . children [ node . children . length - 1 ] ;
489456
490- // check weather has closing marker or not (check completeness)
491- if ( lastElement . type === "text" ) {
492- if ( lastElement . value . endsWith ( "\n" + fence ) ) {
493- flag = "complete" ;
494-
495- // mutate the last Phrase
496- lastElement . value = lastElement . value . slice ( 0 , - ( fence . length + 1 ) ) ;
497- }
457+ // check weather the paragraph has closing marker or not (check completeness)
458+ if ( lastElement . type === "text" && lastElement . value . endsWith ( "\n" + fence ) ) {
459+ // the container ends within the same paragraph
460+ flag = "complete" ;
498461
499- paragraphChildren . push ( lastElement ) ;
500- } else if ( lastElement ) {
501- paragraphChildren . push ( lastElement ) ;
462+ // mutate the last Phrase
463+ lastElement . value = lastElement . value . slice ( 0 , - ( fence . length + 1 ) ) ;
502464 }
503465
504- // mutate the current paragraph children
505- node . children = paragraphChildren ;
466+ paragraphChildren . push ( lastElement ) ;
467+
468+ node . children = paragraphChildren ; // mutation
506469
507470 return { flag, type, rawtitle : title } ;
508471 }
0 commit comments