@@ -16,10 +16,17 @@ declare module "micromark-util-types" {
1616 texMathTextData : "texMathTextData" ;
1717 texMathFlow : "texMathFlow" ;
1818 texMathFlowFence : "texMathFlowFence" ;
19+ texMathFlowClosingFence : "texMathFlowClosingFence" ;
1920 texMathFlowValue : "texMathFlowValue" ;
2021 }
2122}
2223
24+ declare module "mdast-util-from-markdown" {
25+ interface CompileData {
26+ texMathFlowClosed ?: boolean | undefined ;
27+ }
28+ }
29+
2330// \(…\) inline, and \[ / \] or $$ on their own lines for display. These run before
2431// CommonMark's escape construct, which would otherwise consume \( and \[. A
2532// one-line \[x\] stays prose so \[1\] citations do.
@@ -122,28 +129,30 @@ const texMathText: Construct = {
122129type Marker = readonly [ first : number , second : number ] ;
123130
124131/**
125- * A line holding only the `open` marker, up to one holding only the `close`
126- * marker. Like a code fence , it must close; an unfinished formula (a reply still
127- * streaming) stays prose .
132+ * A line holding only the `open` marker, then lines up to one holding only the
133+ * `close` marker. Like a fenced code block , it runs to its closing line or the
134+ * end of its container; an unclosed block is shown as source .
128135 */
129136const tokenizeTexMathFlow = ( open : Marker , close : Marker ) : Tokenizer =>
130137 function ( effects , ok , nok ) {
138+ const interrupting = this . interrupt ;
131139 const nonLazyLine : Construct = {
132140 partial : true ,
133141 tokenize ( this : TokenizeContext , effects , ok , nok ) {
134142 return ( code ) => {
143+ if ( code === null ) return nok ( code ) ;
135144 effects . enter ( "lineEnding" ) ;
136145 effects . consume ( code ) ;
137146 effects . exit ( "lineEnding" ) ;
138147 return ( next ) => ( this . parser . lazy [ this . now ( ) . line ] ? nok ( next ) : ok ( next ) ) ;
139148 } ;
140149 } ,
141150 } ;
142- const fenceLine = ( marker : Marker ) : Construct => ( {
151+ const closingFence : Construct = {
143152 partial : true ,
144153 tokenize ( effects , ok , nok ) {
145154 return function start ( code ) {
146- effects . enter ( "texMathFlowFence " ) ;
155+ effects . enter ( "texMathFlowClosingFence " ) ;
147156 return indent ( code ) ;
148157 } ;
149158
@@ -152,10 +161,10 @@ const tokenizeTexMathFlow = (open: Marker, close: Marker): Tokenizer =>
152161 effects . consume ( code ) ;
153162 return indent ;
154163 }
155- if ( code !== marker [ 0 ] ) return nok ( code ) ;
164+ if ( code !== close [ 0 ] ) return nok ( code ) ;
156165 effects . consume ( code ) ;
157166 return function second ( code ) {
158- if ( code !== marker [ 1 ] ) return nok ( code ) ;
167+ if ( code !== close [ 1 ] ) return nok ( code ) ;
159168 effects . consume ( code ) ;
160169 return fenceEnd ;
161170 } ;
@@ -167,13 +176,11 @@ const tokenizeTexMathFlow = (open: Marker, close: Marker): Tokenizer =>
167176 return fenceEnd ;
168177 }
169178 if ( code !== null && ! isLineEnding ( code ) ) return nok ( code ) ;
170- effects . exit ( "texMathFlowFence " ) ;
179+ effects . exit ( "texMathFlowClosingFence " ) ;
171180 return ok ( code ) ;
172181 }
173182 } ,
174- } ) ;
175- const closingFence = fenceLine ( close ) ;
176- const openingFence = fenceLine ( open ) ;
183+ } ;
177184
178185 return function start ( code ) {
179186 effects . enter ( "texMathFlow" ) ;
@@ -186,37 +193,36 @@ const tokenizeTexMathFlow = (open: Marker, close: Marker): Tokenizer =>
186193 } ;
187194 } ;
188195
196+ // Only the opening line can fail. Past it the block ends in `ok`, closed or
197+ // not. Checking whether the line interrupts a paragraph stops there too.
189198 function openEnd ( code : Code ) : State | undefined {
190199 if ( isSpace ( code ) ) {
191200 effects . consume ( code ) ;
192201 return openEnd ;
193202 }
194- if ( ! isLineEnding ( code ) ) return nok ( code ) ;
203+ if ( code !== null && ! isLineEnding ( code ) ) return nok ( code ) ;
195204 effects . exit ( "texMathFlowFence" ) ;
196- return effects . attempt ( nonLazyLine , lineStart , nok ) ( code ) ;
205+ return interrupting ? ok ( code ) : lineEnd ( code ) ;
197206 }
198207
199- function lineStart ( code : Code ) : State | undefined {
200- return effects . attempt ( closingFence , finish , notOpening ) ( code ) ;
208+ function lineEnd ( code : Code ) : State | undefined {
209+ return effects . attempt ( nonLazyLine , lineStart , finish ) ( code ) ;
201210 }
202211
203- // Another opener cannot be inside a formula. Giving up at it keeps a run of
204- // unclosed openers linear.
205- function notOpening ( code : Code ) : State | undefined {
206- return effects . attempt ( openingFence , nok , content ) ( code ) ;
212+ function lineStart ( code : Code ) : State | undefined {
213+ return effects . attempt ( closingFence , finish , content ) ( code ) ;
207214 }
208215
209216 function content ( code : Code ) : State | undefined {
210- if ( code === null ) return nok ( code ) ;
211- if ( isLineEnding ( code ) ) return effects . attempt ( nonLazyLine , lineStart , nok ) ( code ) ;
217+ if ( code === null || isLineEnding ( code ) ) return lineEnd ( code ) ;
212218 effects . enter ( "texMathFlowValue" ) ;
213219 return value ( code ) ;
214220 }
215221
216222 function value ( code : Code ) : State | undefined {
217223 if ( code === null || isLineEnding ( code ) ) {
218224 effects . exit ( "texMathFlowValue" ) ;
219- return content ( code ) ;
225+ return lineEnd ( code ) ;
220226 }
221227 effects . consume ( code ) ;
222228 return value ;
@@ -250,8 +256,7 @@ export const texMathFromMarkdown: FromMarkdownExtension = {
250256 this . enter ( { type : "inlineMath" , value : "" , data : { hName : "code" } } as never , token ) ;
251257 this . buffer ( ) ;
252258 } ,
253- texMathFlow ( token ) {
254- this . enter ( { type : "math" , value : "" , data : { hName : "pre" } } as never , token ) ;
259+ texMathFlow ( ) {
255260 this . buffer ( ) ;
256261 } ,
257262 } ,
@@ -279,29 +284,48 @@ export const texMathFromMarkdown: FromMarkdownExtension = {
279284 node . data . hChildren = [ { type : "text" , value } ] ;
280285 this . exit ( token ) ;
281286 } ,
287+ texMathFlowClosingFence ( ) {
288+ this . data . texMathFlowClosed = true ;
289+ } ,
282290 texMathFlow ( token ) {
291+ const closed = this . data . texMathFlowClosed ;
292+ this . data . texMathFlowClosed = undefined ;
283293 const value = this . resume ( )
284294 . replace ( / \r \n ? / g, "\n" )
285295 . replace ( / ^ \n | \n $ / g, "" ) ;
286- const node = this . stack . at ( - 1 ) as unknown as {
287- value : string ;
288- data : Record < string , unknown > ;
289- } ;
290- node . value = value ;
291- node . data . hChildren = [
296+ if ( ! closed ) {
297+ // Not a formula: a plain code node that keeps its opener line.
298+ const opener = this . sliceSerialize ( token ) . slice ( 0 , 2 ) ;
299+ this . enter (
300+ { type : "code" , lang : null , meta : null , value : value ? `${ opener } \n${ value } ` : opener } ,
301+ token ,
302+ ) ;
303+ this . exit ( token ) ;
304+ return ;
305+ }
306+ this . enter (
292307 {
293- type : "element" ,
294- tagName : "code" ,
295- properties : { className : [ "language-math-display" ] } ,
296- children : [ { type : "text" , value } ] ,
297- } ,
298- ] ;
308+ type : "math" ,
309+ value,
310+ data : {
311+ hName : "pre" ,
312+ hChildren : [
313+ {
314+ type : "element" ,
315+ tagName : "code" ,
316+ properties : { className : [ "language-math-display" ] } ,
317+ children : [ { type : "text" , value } ] ,
318+ } ,
319+ ] ,
320+ } ,
321+ } as never ,
322+ token ,
323+ ) ;
299324 this . exit ( token ) ;
300325 } ,
301326 } ,
302327} ;
303328
304- /** unified plugin: registers both extensions on the remark-parse processor. */
305329export const remarkTexMath = function ( this : Processor ) {
306330 const data = this . data ( ) as {
307331 micromarkExtensions ?: MicromarkExtension [ ] ;
0 commit comments