@@ -58,6 +58,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
5858 var tokenTypes = {
5959 header : "header" ,
6060 code : "comment" ,
61+ math : "math" ,
6162 quote : "quote" ,
6263 list1 : "variable-2" ,
6364 list2 : "variable-3" ,
@@ -87,9 +88,10 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
8788 , taskListRE = / ^ \[ ( x | ) \] (? = \s ) / // Must follow listRE
8889 , atxHeaderRE = modeCfg . allowAtxHeaderWithoutSpace ? / ^ ( # + ) / : / ^ ( # + ) (?: | $ ) /
8990 , setextHeaderRE = / ^ * (?: \= { 1 , } | - { 1 , } ) \s * $ /
90- , textRE = / ^ [ ^ # ! \[ \] * _ \\ < > ` " ' ( ~ ] + /
91+ , textRE = / ^ [ ^ # ! \[ \] * _ \\ < > \$ ` " ' ( ~ ] + /
9192 , fencedCodeRE = new RegExp ( "^(" + ( modeCfg . fencedCodeBlocks === true ? "~~~+|```+" : modeCfg . fencedCodeBlocks ) +
92- ")[ \\t]*([\\w+#\-]*)" ) ;
93+ ")[ \\t]*([\\w+#\-]*)" )
94+ , fencedMathRE = new RegExp ( "^(\$\$)[ \\t]*([\\w+#\-]*)" ) ;
9395
9496 function switchInline ( stream , state , f ) {
9597 state . f = state . inline = f ;
@@ -218,6 +220,15 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
218220 if ( modeCfg . highlightFormatting ) state . formatting = "code-block" ;
219221 state . code = - 1
220222 return getType ( state ) ;
223+ } else if ( match = stream . match ( fencedCodeRE , true ) ) {
224+ state . fencedChars = match [ 1 ]
225+ // try switching mode
226+ state . localMode = getMode ( match [ 2 ] ) ;
227+ if ( state . localMode ) state . localState = CodeMirror . startState ( state . localMode ) ;
228+ state . f = state . block = local ;
229+ state . formatting = "math" ;
230+ state . math = - 1
231+ return getType ( state ) ;
221232 }
222233
223234 return switchInline ( stream , state , state . inline ) ;
@@ -247,6 +258,9 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
247258 return state . localMode . token ( stream , state . localState ) ;
248259 } else {
249260 stream . skipToEnd ( ) ;
261+ if ( state . math === - 1 ) {
262+ return tokenTypes . math ;
263+ }
250264 return tokenTypes . code ;
251265 }
252266 }
@@ -256,6 +270,13 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
256270 state . block = blockNormal ;
257271 state . f = inlineNormal ;
258272 state . fencedChars = null ;
273+ if ( state . math === - 1 ) {
274+ state . formatting = "math" ;
275+ state . math = 1
276+ var returnType = getType ( state ) ;
277+ state . math = 0
278+ return returnType ;
279+ }
259280 if ( modeCfg . highlightFormatting ) state . formatting = "code-block" ;
260281 state . code = 1
261282 var returnType = getType ( state ) ;
@@ -308,6 +329,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
308329 if ( state . strikethrough ) { styles . push ( tokenTypes . strikethrough ) ; }
309330 if ( state . linkText ) { styles . push ( tokenTypes . linkText ) ; }
310331 if ( state . code ) { styles . push ( tokenTypes . code ) ; }
332+ if ( state . math ) { styles . push ( tokenTypes . math ) ; }
311333 if ( state . image ) { styles . push ( tokenTypes . image ) ; }
312334 if ( state . imageAltText ) { styles . push ( tokenTypes . imageAltText , "link" ) ; }
313335 if ( state . imageMarker ) { styles . push ( tokenTypes . imageMarker ) ; }
@@ -420,6 +442,27 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
420442 return getType ( state ) ;
421443 }
422444
445+ // display math correctly
446+ if ( ch === '$' ) {
447+ var previousFormatting = state . formatting ;
448+ state . formatting = "math" ;
449+ stream . eatWhile ( '$' ) ;
450+ var count = stream . current ( ) . length
451+ if ( state . math == 0 ) {
452+ state . math = count
453+ return getType ( state )
454+ } else if ( count == state . math ) { // Must be exact
455+ var t = getType ( state )
456+ state . math = 0
457+ return t
458+ } else {
459+ state . formatting = previousFormatting
460+ return getType ( state )
461+ }
462+ } else if ( state . math ) {
463+ return getType ( state ) ;
464+ }
465+
423466 if ( ch === '\\' ) {
424467 stream . next ( ) ;
425468 if ( modeCfg . highlightFormatting ) {
@@ -704,6 +747,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
704747 linkHref : false ,
705748 linkTitle : false ,
706749 code : 0 ,
750+ math : 0 ,
707751 em : false ,
708752 strong : false ,
709753 header : 0 ,
@@ -738,6 +782,7 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
738782 formatting : false ,
739783 linkTitle : s . linkTitle ,
740784 code : s . code ,
785+ math : s . math ,
741786 em : s . em ,
742787 strong : s . strong ,
743788 strikethrough : s . strikethrough ,
0 commit comments