@@ -248,7 +248,32 @@ CodeMirror.defineMode("markdown", function(cmCfg, modeCfg) {
248248 state . code = - 1
249249 return getType ( state ) ;
250250 } else if ( firstTokenOnLine && state . indentation <= maxNonCodeIndentation && ( match = stream . match ( fencedMathRE , true ) ) ) {
251- state . fencedEndRE = new RegExp ( match [ 1 ] . replace ( / \$ / g, '\\$' ) + "+ *$" ) ;
251+ // match[0] is the whole line
252+ // match[1] is the opening fence "$$"
253+ // match[2] is the optional language / mode name
254+ var wholeLine = match [ 0 ] ;
255+ var openingFence = match [ 1 ] ;
256+
257+ // Everything after the opening "$$"
258+ var rest = wholeLine . slice ( openingFence . length ) ;
259+
260+ // If there's another "$$" in the rest of the line, treat this as
261+ // a single-line math block: $$ ... $$.
262+ var closingIndex = rest . indexOf ( "$$" ) ;
263+
264+ if ( closingIndex !== - 1 ) {
265+ // Single-line $$ ... $$: highlight as math, but do NOT enter
266+ // multi-line fenced math mode.
267+
268+ state . formatting = "math" ;
269+ state . math = 0 ; // not in a multi-line math block
270+ // We've already consumed the whole line via stream.match(..., true)
271+ // so just return the math token type for this line.
272+ return tokenTypes . math ;
273+ }
274+
275+ // Otherwise fall back to the original behavior: multi-line fenced math
276+ state . fencedEndRE = new RegExp ( openingFence . replace ( / \$ / g, '\\$' ) + "+ *$" ) ;
252277 // try switching mode
253278 state . localMode = modeCfg . fencedCodeBlockHighlighting && getMode ( match [ 2 ] || modeCfg . fencedCodeBlockDefaultMode ) ;
254279 if ( state . localMode ) state . localState = CodeMirror . startState ( state . localMode ) ;
0 commit comments