2
2
* @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
3
3
* @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
4
4
* @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
5
- * @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
6
5
* @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
7
- * @typedef {import('../index.js ').Math } Math
6
+ * @typedef {import('mdast-util-to-markdown ').Options } ToMarkdownExtension
8
7
* @typedef {import('../index.js').InlineMath } InlineMath
8
+ * @typedef {import('../index.js').Math } Math
9
9
*
10
10
* @typedef ToOptions
11
11
* Configuration.
12
12
* @property {boolean | null | undefined } [singleDollarTextMath=true]
13
- * Whether to support math (text) with a single dollar.
13
+ * Whether to support math (text) with a single dollar (default: `true`) .
14
14
*
15
15
* Single dollars work in Pandoc and many other places, but often interfere
16
16
* with “normal” dollars in text.
17
17
* If you turn this off, you can still use two or more dollars for text math.
18
18
*/
19
19
20
+ import { ok as assert } from 'devlop'
20
21
import { longestStreak } from 'longest-streak'
21
22
22
23
/**
@@ -76,7 +77,8 @@ export function mathFromMarkdown() {
76
77
*/
77
78
function exitMathFlowMeta ( ) {
78
79
const data = this . resume ( )
79
- const node = /** @type {Math } */ ( this . stack [ this . stack . length - 1 ] )
80
+ const node = this . stack [ this . stack . length - 1 ]
81
+ assert ( node . type === 'math' )
80
82
node . meta = data
81
83
}
82
84
@@ -97,10 +99,11 @@ export function mathFromMarkdown() {
97
99
*/
98
100
function exitMathFlow ( token ) {
99
101
const data = this . resume ( ) . replace ( / ^ ( \r ? \n | \r ) | ( \r ? \n | \r ) $ / g, '' )
100
- const node = /** @type {Math } */ ( this . stack [ this . stack . length - 1 ] )
102
+ const node = this . stack [ this . stack . length - 1 ]
103
+ assert ( node . type === 'math' )
101
104
this . exit ( token )
102
105
node . value = data
103
- // @ts -expect-error: we defined it.
106
+ // @ts -expect-error: we defined it in `enterMathFlow` .
104
107
node . data . hChildren [ 0 ] . value = data
105
108
this . data . mathFlowInside = undefined
106
109
}
@@ -131,7 +134,8 @@ export function mathFromMarkdown() {
131
134
*/
132
135
function exitMathText ( token ) {
133
136
const data = this . resume ( )
134
- const node = /** @type {Math } */ ( this . stack [ this . stack . length - 1 ] )
137
+ const node = this . stack [ this . stack . length - 1 ]
138
+ assert ( node . type === 'inlineMath' )
135
139
this . exit ( token )
136
140
node . value = data
137
141
// @ts -expect-error: we defined it.
@@ -152,7 +156,7 @@ export function mathFromMarkdown() {
152
156
* Create an extension for `mdast-util-to-markdown`.
153
157
*
154
158
* @param {ToOptions | null | undefined } [options]
155
- * Configuration.
159
+ * Configuration (optional) .
156
160
* @returns {ToMarkdownExtension }
157
161
* Extension for `mdast-util-to-markdown`.
158
162
*/
@@ -184,22 +188,21 @@ export function mathToMarkdown(options) {
184
188
* @type {ToMarkdownHandle }
185
189
* @param {Math } node
186
190
*/
187
- // To do: next major: rename `context` to state, `safeOptions` to info.
188
191
// Note: fixing this code? Please also fix the similar code for code:
189
192
// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/handle/code.js>
190
- function math ( node , _ , context , safeOptions ) {
193
+ function math ( node , _ , state , info ) {
191
194
const raw = node . value || ''
192
- const tracker = context . createTracker ( safeOptions )
195
+ const tracker = state . createTracker ( info )
193
196
const sequence = '$' . repeat ( Math . max ( longestStreak ( raw , '$' ) + 1 , 2 ) )
194
- const exit = context . enter ( 'mathFlow' )
197
+ const exit = state . enter ( 'mathFlow' )
195
198
let value = tracker . move ( sequence )
196
199
197
200
if ( node . meta ) {
198
- const subexit = context . enter ( 'mathFlowMeta' )
201
+ const subexit = state . enter ( 'mathFlowMeta' )
199
202
value += tracker . move (
200
- context . safe ( node . meta , {
201
- before : value ,
203
+ state . safe ( node . meta , {
202
204
after : '\n' ,
205
+ before : value ,
203
206
encode : [ '$' ] ,
204
207
...tracker . current ( )
205
208
} )
@@ -224,10 +227,7 @@ export function mathToMarkdown(options) {
224
227
*/
225
228
// Note: fixing this code? Please also fix the similar code for inline code:
226
229
// <https://github.com/syntax-tree/mdast-util-to-markdown/blob/main/lib/handle/inline-code.js>
227
- //
228
- // To do: next major: rename `context` to state.
229
- // To do: next major: use `state` (`safe`, `track`, `patternCompile`).
230
- function inlineMath ( node , _ , context ) {
230
+ function inlineMath ( node , _ , state ) {
231
231
let value = node . value || ''
232
232
let size = 1
233
233
@@ -267,15 +267,15 @@ export function mathToMarkdown(options) {
267
267
// We can’t escape characters in `inlineMath`, but because eols are
268
268
// transformed to spaces when going from markdown to HTML anyway, we can swap
269
269
// them out.
270
- while ( ++ index < context . unsafe . length ) {
271
- const pattern = context . unsafe [ index ]
270
+ while ( ++ index < state . unsafe . length ) {
271
+ const pattern = state . unsafe [ index ]
272
272
273
273
// Only look for `atBreak`s.
274
274
// Btw: note that `atBreak` patterns will always start the regex at LF or
275
275
// CR.
276
276
if ( ! pattern . atBreak ) continue
277
277
278
- const expression = context . compilePattern ( pattern )
278
+ const expression = state . compilePattern ( pattern )
279
279
/** @type {RegExpExecArray | null } */
280
280
let match
281
281
0 commit comments