1818 */
1919
2020import { longestStreak } from 'longest-streak'
21- import { safe } from 'mdast-util-to-markdown/lib/util/safe.js'
22- import { track } from 'mdast-util-to-markdown/lib/util/track.js'
23- import { patternCompile } from 'mdast-util-to-markdown/lib/util/pattern-compile.js'
2421
2522/**
2623 * Create an extension for `mdast-util-from-markdown`.
@@ -89,9 +86,9 @@ export function mathFromMarkdown() {
8986 */
9087 function exitMathFlowFence ( ) {
9188 // Exit if this is the closing fence.
92- if ( this . getData ( ' mathFlowInside' ) ) return
89+ if ( this . data . mathFlowInside ) return
9390 this . buffer ( )
94- this . setData ( ' mathFlowInside' , true )
91+ this . data . mathFlowInside = true
9592 }
9693
9794 /**
@@ -100,11 +97,12 @@ export function mathFromMarkdown() {
10097 */
10198 function exitMathFlow ( token ) {
10299 const data = this . resume ( ) . replace ( / ^ ( \r ? \n | \r ) | ( \r ? \n | \r ) $ / g, '' )
103- const node = /** @type {Math } */ ( this . exit ( token ) )
100+ const node = /** @type {Math } */ ( this . stack [ this . stack . length - 1 ] )
101+ this . exit ( token )
104102 node . value = data
105103 // @ts -expect-error: we defined it.
106104 node . data . hChildren [ 0 ] . value = data
107- this . setData ( ' mathFlowInside' )
105+ this . data . mathFlowInside = undefined
108106 }
109107
110108 /**
@@ -133,7 +131,8 @@ export function mathFromMarkdown() {
133131 */
134132 function exitMathText ( token ) {
135133 const data = this . resume ( )
136- const node = /** @type {Math } */ ( this . exit ( token ) )
134+ const node = /** @type {Math } */ ( this . stack [ this . stack . length - 1 ] )
135+ this . exit ( token )
137136 node . value = data
138137 // @ts -expect-error: we defined it.
139138 node . data . hChildren [ 0 ] . value = data
@@ -190,15 +189,15 @@ export function mathToMarkdown(options) {
190189 // <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/handle/code.js>
191190 function math ( node , _ , context , safeOptions ) {
192191 const raw = node . value || ''
193- const tracker = track ( safeOptions )
192+ const tracker = context . createTracker ( safeOptions )
194193 const sequence = '$' . repeat ( Math . max ( longestStreak ( raw , '$' ) + 1 , 2 ) )
195194 const exit = context . enter ( 'mathFlow' )
196195 let value = tracker . move ( sequence )
197196
198197 if ( node . meta ) {
199198 const subexit = context . enter ( 'mathFlowMeta' )
200199 value += tracker . move (
201- safe ( context , node . meta , {
200+ context . safe ( node . meta , {
202201 before : value ,
203202 after : '\n' ,
204203 encode : [ '$' ] ,
@@ -270,15 +269,16 @@ export function mathToMarkdown(options) {
270269 // them out.
271270 while ( ++ index < context . unsafe . length ) {
272271 const pattern = context . unsafe [ index ]
273- const expression = patternCompile ( pattern )
274- /** @type {RegExpExecArray | null } */
275- let match
276272
277273 // Only look for `atBreak`s.
278274 // Btw: note that `atBreak` patterns will always start the regex at LF or
279275 // CR.
280276 if ( ! pattern . atBreak ) continue
281277
278+ const expression = context . compilePattern ( pattern )
279+ /** @type {RegExpExecArray | null } */
280+ let match
281+
282282 while ( ( match = expression . exec ( value ) ) ) {
283283 let position = match . index
284284
0 commit comments