@@ -449,8 +449,12 @@ const PARAGRAPH_END = /(?:\r\n|\r(?!\n)|\n)(?: {0,3}>)*[ \t]*(?![^\r\n])/g;
449449const LINE_END = / [ \r \n ] / g;
450450const PAREN_CLOSE = / (?< ! \\ ) (?: \\ \\ ) * \\ \) / g;
451451const BRACKET_CLOSE = / (?< ! \\ ) (?: \\ \\ ) * \\ \] / g;
452+ const TEX_FENCE_OPEN_REST = / [ \t ] * (? ! [ ^ \r \n ] ) / y;
452453const TEX_FENCE_CLOSE = / \\ \] [ \t ] * (? ! [ ^ \r \n ] ) / g;
453454const DOLLAR_FENCE_CLOSE = / (?< ! \\ ) \$ \$ / g;
455+ // An inline `\(` or `\[` fails at the first backtick, so its read stops there.
456+ const BACKTICK = / ` / g;
457+ const UNESCAPED_DOLLAR = / (?< ! \\ ) (?: \\ \\ ) * \$ / g;
454458
455459function mathParseFits ( markdown : string ) : boolean {
456460 const stops = new Map < RegExp , number > ( ) ;
@@ -467,9 +471,11 @@ function mathParseFits(markdown: string): boolean {
467471 let work = markdown . length * CHARACTER_COST ;
468472 for ( const _ of markdown . matchAll ( / (?: \r \n ? | \n ) (? = [ ^ \r \n ] ) / g) ) work += LINE_COST ;
469473 let lineStart = 0 ;
470- for ( const match of markdown . matchAll ( / \\ [ ( [ ] ( [ \t ] * (? ! [ ^ \r \n ] ) ) ? | \$ + / g) ) {
471- const [ delimiter , texFence ] = match ;
474+ for ( const match of markdown . matchAll ( / \\ [ ( [ ] | \$ + / g) ) {
475+ const [ delimiter ] = match ;
472476 const index = match . index ;
477+ TEX_FENCE_OPEN_REST . lastIndex = index + 2 ;
478+ const texFence = delimiter === "\\[" && TEX_FENCE_OPEN_REST . test ( markdown ) ;
473479 if ( index - lineStart > until ( LINE_END , lineStart ) ) {
474480 lineStart = index ;
475481 while ( lineStart > 0 && ! / [ \r \n ] / . test ( markdown [ lineStart - 1 ] ! ) ) lineStart -= 1 ;
@@ -478,8 +484,11 @@ function mathParseFits(markdown: string): boolean {
478484 const paragraph = until ( PARAGRAPH_END , index ) ;
479485 const close = delimiter === "\\(" ? PAREN_CLOSE : delimiter [ 0 ] === "\\" ? BRACKET_CLOSE : null ;
480486 // A closed formula consumes the openers inside it; only an unclosed one rereads its paragraph.
481- if ( close ? until ( close , index + 2 ) + 2 >= paragraph : delimiter . length > 1 ) work += paragraph ;
482- if ( texFence !== undefined ) work += until ( TEX_FENCE_CLOSE , index + 2 ) ;
487+ if ( close ) {
488+ const read = Math . min ( paragraph , until ( BACKTICK , index ) ) ;
489+ if ( until ( close , index + 2 ) + 2 >= read ) work += read ;
490+ } else if ( delimiter . length > 1 ) work += paragraph ;
491+ if ( texFence ) work += until ( TEX_FENCE_CLOSE , index + 2 ) ;
483492 else if ( delimiter . length > 2 && delimiter [ 0 ] === "$" ) work += markdown . length - index ;
484493 else if ( delimiter === "$$" ) work += until ( DOLLAR_FENCE_CLOSE , index + 2 ) ;
485494 if ( work > MATH_PARSE_BUDGET ) return false ;
@@ -491,7 +500,21 @@ function mathParseFits(markdown: string): boolean {
491500export function markdownWithDollarMath ( markdown : string ) : string {
492501 if ( ! markdown . includes ( "\\(" ) && ! markdown . includes ( "\\[" ) ) return markdown ;
493502 if ( ! mathParseFits ( markdown ) ) return markdown ;
494- const ranges = markdownMathRanges ( markdown ) ;
503+ let ranges : ReturnType < typeof markdownMathRanges > ;
504+ try {
505+ ranges = markdownMathRanges ( markdown ) ;
506+ } catch {
507+ // Deeply nested Markdown overflows the recursive range walker.
508+ return markdown ;
509+ }
510+ // md4c would pair a literal `$` (a price, `$HOME`) with an inserted delimiter.
511+ let covering = 0 ;
512+ for ( const match of markdown . matchAll ( UNESCAPED_DOLLAR ) ) {
513+ const dollar = match . index + match [ 0 ] . length - 1 ;
514+ while ( covering < ranges . length && ranges [ covering ] ! . end <= dollar ) covering += 1 ;
515+ const range = ranges [ covering ] ;
516+ if ( ! range || range . start > dollar || range . source . startsWith ( "\\" ) ) return markdown ;
517+ }
495518 const edits : MarkdownEdit [ ] = [ ] ;
496519 for ( const [ index , { start, end, source } ] of ranges . entries ( ) ) {
497520 const dollars = source . startsWith ( "\\(" ) ? "$" : source . startsWith ( "\\[" ) ? "$$" : null ;
0 commit comments