1
- /*! markdown-it-math 2 .0.1 https://github.com/runarberg/markdown-it-math @license MIT */
1
+ /*! markdown-it-math 3 .0.0 https://github.com/runarberg/markdown-it-math @license MIT */
2
2
( function ( f ) { if ( typeof exports === "object" && typeof module !== "undefined" ) { module . exports = f ( ) } else if ( typeof define === "function" && define . amd ) { define ( [ ] , f ) } else { var g ; if ( typeof window !== "undefined" ) { g = window } else if ( typeof global !== "undefined" ) { g = global } else if ( typeof self !== "undefined" ) { g = self } else { g = this } g . markdownitMath = f ( ) } } ) ( function ( ) { var define , module , exports ; return ( function e ( t , n , r ) { function s ( o , u ) { if ( ! n [ o ] ) { if ( ! t [ o ] ) { var a = typeof require == "function" && require ; if ( ! u && a ) return a ( o , ! 0 ) ; if ( i ) return i ( o , ! 0 ) ; var f = new Error ( "Cannot find module '" + o + "'" ) ; throw f . code = "MODULE_NOT_FOUND" , f } var l = n [ o ] = { exports :{ } } ; t [ o ] [ 0 ] . call ( l . exports , function ( e ) { var n = t [ o ] [ 1 ] [ e ] ; return s ( n ?n :e ) } , l , l . exports , e , t , n , r ) } return n [ o ] . exports } var i = typeof require == "function" && require ; for ( var o = 0 ; o < r . length ; o ++ ) s ( r [ o ] ) ; return s } ) ( { 1 :[ function ( require , module , exports ) {
3
3
'use strict' ;
4
4
@@ -5692,42 +5692,56 @@ var ascii2mathml = require('ascii2mathml');
5692
5692
require ( './lib/polyfills' ) ;
5693
5693
5694
5694
5695
- function scanDelims ( state , start ) {
5696
- var pos = state . pos , lastChar , nextChar , count ,
5695
+ function scanDelims ( state , start , delimLength ) {
5696
+ var pos = start , lastChar , nextChar , count , can_open , can_close ,
5697
5697
isLastWhiteSpace , isLastPunctChar ,
5698
5698
isNextWhiteSpace , isNextPunctChar ,
5699
- can_open = true ,
5700
- can_close = true ,
5699
+ left_flanking = true ,
5700
+ right_flanking = true ,
5701
5701
max = state . posMax ,
5702
5702
isWhiteSpace = state . md . utils . isWhiteSpace ,
5703
5703
isPunctChar = state . md . utils . isPunctChar ,
5704
5704
isMdAsciiPunct = state . md . utils . isMdAsciiPunct ;
5705
+
5705
5706
// treat beginning of the line as a whitespace
5706
5707
lastChar = start > 0 ? state . src . charCodeAt ( start - 1 ) : 0x20 ;
5708
+
5707
5709
if ( pos >= max ) {
5708
5710
can_open = false ;
5709
5711
}
5712
+
5713
+ pos += delimLength ;
5714
+
5710
5715
count = pos - start ;
5716
+
5711
5717
// treat end of the line as a whitespace
5712
5718
nextChar = pos < max ? state . src . charCodeAt ( pos ) : 0x20 ;
5719
+
5713
5720
isLastPunctChar = isMdAsciiPunct ( lastChar ) || isPunctChar ( String . fromCharCode ( lastChar ) ) ;
5714
5721
isNextPunctChar = isMdAsciiPunct ( nextChar ) || isPunctChar ( String . fromCharCode ( nextChar ) ) ;
5722
+
5715
5723
isLastWhiteSpace = isWhiteSpace ( lastChar ) ;
5716
5724
isNextWhiteSpace = isWhiteSpace ( nextChar ) ;
5725
+
5717
5726
if ( isNextWhiteSpace ) {
5718
- can_open = false ;
5727
+ left_flanking = false ;
5719
5728
} else if ( isNextPunctChar ) {
5720
5729
if ( ! ( isLastWhiteSpace || isLastPunctChar ) ) {
5721
- can_open = false ;
5730
+ left_flanking = false ;
5722
5731
}
5723
5732
}
5733
+
5724
5734
if ( isLastWhiteSpace ) {
5725
- can_close = false ;
5735
+ right_flanking = false ;
5726
5736
} else if ( isLastPunctChar ) {
5727
5737
if ( ! ( isNextWhiteSpace || isNextPunctChar ) ) {
5728
- can_close = false ;
5738
+ right_flanking = false ;
5729
5739
}
5730
5740
}
5741
+
5742
+ can_open = left_flanking ;
5743
+ can_close = right_flanking ;
5744
+
5731
5745
return {
5732
5746
can_open : can_open ,
5733
5747
can_close : can_close ,
@@ -5750,7 +5764,7 @@ function makeMath_inline(open, close) {
5750
5764
if ( openDelim !== open ) { return false ; }
5751
5765
if ( silent ) { return false ; } // Don’t run any pairs in validation mode
5752
5766
5753
- res = scanDelims ( state , start + open . length ) ;
5767
+ res = scanDelims ( state , start , openDelim . length ) ;
5754
5768
startCount = res . delims ;
5755
5769
5756
5770
if ( ! res . can_open ) {
@@ -5765,7 +5779,7 @@ function makeMath_inline(open, close) {
5765
5779
while ( state . pos < max ) {
5766
5780
closeDelim = state . src . slice ( state . pos , state . pos + close . length ) ;
5767
5781
if ( closeDelim === close ) {
5768
- res = scanDelims ( state , state . pos + close . length ) ;
5782
+ res = scanDelims ( state , state . pos , close . length ) ;
5769
5783
if ( res . can_close ) {
5770
5784
found = true ;
5771
5785
break ;
@@ -5799,7 +5813,7 @@ function makeMath_inline(open, close) {
5799
5813
5800
5814
function makeMath_block ( open , close ) {
5801
5815
return function math_block ( state , startLine , endLine , silent ) {
5802
- var openDelim , len , params , nextLine , token ,
5816
+ var openDelim , len , params , nextLine , token , firstLine , lastLine , lastLinePos ,
5803
5817
haveEndMarker = false ,
5804
5818
pos = state . bMarks [ startLine ] + state . tShift [ startLine ] ,
5805
5819
max = state . eMarks [ startLine ] ;
@@ -5810,14 +5824,26 @@ function makeMath_block(open, close) {
5810
5824
5811
5825
if ( openDelim !== open ) { return false ; }
5812
5826
5827
+ pos += open . length ;
5828
+ firstLine = state . src . slice ( pos , max ) ;
5829
+
5813
5830
// Since start is found, we can report success here in validation mode
5814
5831
if ( silent ) { return true ; }
5815
5832
5833
+ if ( firstLine . trim ( ) . slice ( - close . length ) === close ) {
5834
+ // Single line expression
5835
+ firstLine = firstLine . trim ( ) . slice ( 0 , - close . length ) ;
5836
+ haveEndMarker = true ;
5837
+ }
5838
+
5816
5839
// search end of block
5817
5840
nextLine = startLine ;
5818
5841
5819
5842
for ( ; ; ) {
5843
+ if ( haveEndMarker ) { break ; }
5844
+
5820
5845
nextLine ++ ;
5846
+
5821
5847
if ( nextLine >= endLine ) {
5822
5848
// unclosed block should be autoclosed by end of document.
5823
5849
// also block seems to be autoclosed by end of parent
@@ -5832,23 +5858,27 @@ function makeMath_block(open, close) {
5832
5858
break ;
5833
5859
}
5834
5860
5835
- if ( state . src . slice ( pos , pos + close . length ) !== close ) { continue ; }
5861
+ if ( state . src . slice ( pos , max ) . trim ( ) . slice ( - close . length ) !== close ) {
5862
+ continue ;
5863
+ }
5836
5864
5837
5865
if ( state . tShift [ nextLine ] - state . blkIndent >= 4 ) {
5838
5866
// closing block math should be indented less then 4 spaces
5839
5867
continue ;
5840
5868
}
5841
5869
5842
- pos += close . length ;
5870
+ lastLinePos = state . src . slice ( 0 , max ) . lastIndexOf ( close ) ;
5871
+ lastLine = state . src . slice ( pos , lastLinePos ) ;
5872
+
5873
+ pos += lastLine . length + close . length ;
5843
5874
5844
5875
// make sure tail has spaces only
5845
5876
pos = state . skipSpaces ( pos ) ;
5846
5877
5847
5878
if ( pos < max ) { continue ; }
5848
5879
5849
- haveEndMarker = true ;
5850
5880
// found!
5851
- break ;
5881
+ haveEndMarker = true ;
5852
5882
}
5853
5883
5854
5884
// If math block has heading spaces, they should be removed from its inner block
@@ -5858,7 +5888,9 @@ function makeMath_block(open, close) {
5858
5888
5859
5889
token = state . push ( 'math_block' , 'math' , 0 ) ;
5860
5890
token . block = true ;
5861
- token . content = state . getLines ( startLine + 1 , nextLine , len , true ) ;
5891
+ token . content = ( firstLine && firstLine . trim ( ) ? firstLine + '\n' : '' ) +
5892
+ state . getLines ( startLine + 1 , nextLine , len , true ) +
5893
+ ( lastLine && lastLine . trim ( ) ? lastLine : '' ) ;
5862
5894
token . info = params ;
5863
5895
token . map = [ startLine , state . line ] ;
5864
5896
token . markup = open ;
0 commit comments