18
18
*/
19
19
20
20
import { 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'
24
21
25
22
/**
26
23
* Create an extension for `mdast-util-from-markdown`.
@@ -89,9 +86,9 @@ export function mathFromMarkdown() {
89
86
*/
90
87
function exitMathFlowFence ( ) {
91
88
// Exit if this is the closing fence.
92
- if ( this . getData ( ' mathFlowInside' ) ) return
89
+ if ( this . data . mathFlowInside ) return
93
90
this . buffer ( )
94
- this . setData ( ' mathFlowInside' , true )
91
+ this . data . mathFlowInside = true
95
92
}
96
93
97
94
/**
@@ -100,11 +97,12 @@ export function mathFromMarkdown() {
100
97
*/
101
98
function exitMathFlow ( token ) {
102
99
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 )
104
102
node . value = data
105
103
// @ts -expect-error: we defined it.
106
104
node . data . hChildren [ 0 ] . value = data
107
- this . setData ( ' mathFlowInside' )
105
+ this . data . mathFlowInside = undefined
108
106
}
109
107
110
108
/**
@@ -133,7 +131,8 @@ export function mathFromMarkdown() {
133
131
*/
134
132
function exitMathText ( token ) {
135
133
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 )
137
136
node . value = data
138
137
// @ts -expect-error: we defined it.
139
138
node . data . hChildren [ 0 ] . value = data
@@ -190,15 +189,15 @@ export function mathToMarkdown(options) {
190
189
// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/handle/code.js>
191
190
function math ( node , _ , context , safeOptions ) {
192
191
const raw = node . value || ''
193
- const tracker = track ( safeOptions )
192
+ const tracker = context . createTracker ( safeOptions )
194
193
const sequence = '$' . repeat ( Math . max ( longestStreak ( raw , '$' ) + 1 , 2 ) )
195
194
const exit = context . enter ( 'mathFlow' )
196
195
let value = tracker . move ( sequence )
197
196
198
197
if ( node . meta ) {
199
198
const subexit = context . enter ( 'mathFlowMeta' )
200
199
value += tracker . move (
201
- safe ( context , node . meta , {
200
+ context . safe ( node . meta , {
202
201
before : value ,
203
202
after : '\n' ,
204
203
encode : [ '$' ] ,
@@ -270,15 +269,16 @@ export function mathToMarkdown(options) {
270
269
// them out.
271
270
while ( ++ index < context . unsafe . length ) {
272
271
const pattern = context . unsafe [ index ]
273
- const expression = patternCompile ( pattern )
274
- /** @type {RegExpExecArray | null } */
275
- let match
276
272
277
273
// Only look for `atBreak`s.
278
274
// Btw: note that `atBreak` patterns will always start the regex at LF or
279
275
// CR.
280
276
if ( ! pattern . atBreak ) continue
281
277
278
+ const expression = context . compilePattern ( pattern )
279
+ /** @type {RegExpExecArray | null } */
280
+ let match
281
+
282
282
while ( ( match = expression . exec ( value ) ) ) {
283
283
let position = match . index
284
284
0 commit comments