@@ -15,6 +15,7 @@ function latexMath(flow: boolean): Construct {
1515 return {
1616 name : flow ? "latexMathFlow" : "latexMathText" ,
1717 ...( flow ? { concrete : true } : { } ) ,
18+ /** Tokenize a TeX-delimited expression while preserving Markdown source positions. */
1819 tokenize ( effects , ok , nok ) {
1920 const math = flow ? "mathFlow" : "mathText" ;
2021 const fence = flow ? "mathFlowFence" : "mathTextSequence" ;
@@ -23,6 +24,7 @@ function latexMath(flow: boolean): Construct {
2324 let inValue = false ;
2425 const closingDelimiter : Construct = {
2526 partial : true ,
27+ /** Probe for the matching closing delimiter without consuming a failed match. */
2628 tokenize ( checkEffects , yes , no ) {
2729 return ( code ) => {
2830 checkEffects . enter ( "mathTextSequence" ) ;
@@ -39,13 +41,15 @@ function latexMath(flow: boolean): Construct {
3941
4042 return start ;
4143
44+ /** Begin the math token at the opening backslash. */
4245 function start ( code : number | null ) : State | undefined {
4346 effects . enter ( math ) ;
4447 effects . enter ( fence ) ;
4548 effects . consume ( code ) ;
4649 return opening ;
4750 }
4851
52+ /** Select the matching delimiter, allowing only brackets in flow mode. */
4953 function opening ( code : number | null ) : State | undefined {
5054 if ( code !== 91 && ( flow || code !== 40 ) ) return nok ( code ) ;
5155 closing = code === 91 ? 93 : 41 ;
@@ -54,6 +58,7 @@ function latexMath(flow: boolean): Construct {
5458 return inside ;
5559 }
5660
61+ /** Consume formula content until a matching delimiter or an incomplete ending. */
5762 function inside ( code : number | null ) : State | undefined {
5863 if ( code === null ) return nok ( code ) ;
5964 if ( markdownLineEnding ( code ) ) {
@@ -73,6 +78,7 @@ function latexMath(flow: boolean): Construct {
7378 return inside ;
7479 }
7580
81+ /** Keep escaped characters inside the formula instead of treating them as delimiters. */
7682 function escape ( code : number | null ) : State | undefined {
7783 if ( ! inValue ) effects . enter ( value ) ;
7884 inValue = true ;
@@ -85,6 +91,7 @@ function latexMath(flow: boolean): Construct {
8591 } ;
8692 }
8793
94+ /** Finish the formula and check trailing content when parsing a display block. */
8895 function close ( code : number | null ) : State | undefined {
8996 if ( inValue ) effects . exit ( value ) ;
9097 effects . enter ( fence ) ;
@@ -97,6 +104,7 @@ function latexMath(flow: boolean): Construct {
97104 } ;
98105 }
99106
107+ /** Accept a display block only when the rest of its line is whitespace. */
100108 function after ( code : number | null ) : State | undefined {
101109 // A display formula with surrounding prose belongs to the paragraph.
102110 if ( code === null || markdownLineEnding ( code ) ) return ok ( code ) ;
@@ -107,6 +115,7 @@ function latexMath(flow: boolean): Construct {
107115 return nok ( code ) ;
108116 }
109117
118+ /** Consume trailing display-block whitespace before checking the line ending. */
110119 function trailingSpace ( code : number | null ) : State | undefined {
111120 if ( markdownSpace ( code ) ) {
112121 effects . consume ( code ) ;
@@ -129,6 +138,7 @@ export const remarkChatMath: Plugin<[], Root> = function () {
129138 if ( dollarSyntax && dollar && ! Array . isArray ( dollar ) ) {
130139 dollarSyntax [ 36 ] = {
131140 ...dollar ,
141+ /** Reject spaced single-dollar expressions so prices do not swallow later math. */
132142 tokenize ( effects , ok , nok ) {
133143 return dollar . tokenize . call (
134144 this ,
@@ -156,6 +166,7 @@ export const remarkChatMath: Plugin<[], Root> = function () {
156166 if ( ! parse ) return ;
157167 this . parser = ( source , file ) => {
158168 const tree = parse ( source , file ) as Root ;
169+ /** Attach rendering and copy metadata, removing container prefixes from copied formulas. */
159170 const visit = ( node : Root | RootContent , inContainer = false ) => {
160171 if ( node . type === "math" || node . type === "inlineMath" ) {
161172 const start = node . position ?. start . offset ;
0 commit comments