22 * @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
33 * @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
44 * @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
5- * @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
65 * @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
7- * @typedef {import('../index.js ').Math } Math
6+ * @typedef {import('mdast-util-to-markdown ').Options } ToMarkdownExtension
87 * @typedef {import('../index.js').InlineMath } InlineMath
8+ * @typedef {import('../index.js').Math } Math
99 *
1010 * @typedef ToOptions
1111 * Configuration.
1212 * @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`) .
1414 *
1515 * Single dollars work in Pandoc and many other places, but often interfere
1616 * with “normal” dollars in text.
1717 * If you turn this off, you can still use two or more dollars for text math.
1818 */
1919
20+ import { ok as assert } from 'devlop'
2021import { longestStreak } from 'longest-streak'
2122
2223/**
@@ -76,7 +77,8 @@ export function mathFromMarkdown() {
7677 */
7778 function exitMathFlowMeta ( ) {
7879 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' )
8082 node . meta = data
8183 }
8284
@@ -97,10 +99,11 @@ export function mathFromMarkdown() {
9799 */
98100 function exitMathFlow ( token ) {
99101 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' )
101104 this . exit ( token )
102105 node . value = data
103- // @ts -expect-error: we defined it.
106+ // @ts -expect-error: we defined it in `enterMathFlow` .
104107 node . data . hChildren [ 0 ] . value = data
105108 this . data . mathFlowInside = undefined
106109 }
@@ -131,7 +134,8 @@ export function mathFromMarkdown() {
131134 */
132135 function exitMathText ( token ) {
133136 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' )
135139 this . exit ( token )
136140 node . value = data
137141 // @ts -expect-error: we defined it.
@@ -152,7 +156,7 @@ export function mathFromMarkdown() {
152156 * Create an extension for `mdast-util-to-markdown`.
153157 *
154158 * @param {ToOptions | null | undefined } [options]
155- * Configuration.
159+ * Configuration (optional) .
156160 * @returns {ToMarkdownExtension }
157161 * Extension for `mdast-util-to-markdown`.
158162 */
@@ -184,22 +188,21 @@ export function mathToMarkdown(options) {
184188 * @type {ToMarkdownHandle }
185189 * @param {Math } node
186190 */
187- // To do: next major: rename `context` to state, `safeOptions` to info.
188191 // Note: fixing this code? Please also fix the similar code for code:
189192 // <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 ) {
191194 const raw = node . value || ''
192- const tracker = context . createTracker ( safeOptions )
195+ const tracker = state . createTracker ( info )
193196 const sequence = '$' . repeat ( Math . max ( longestStreak ( raw , '$' ) + 1 , 2 ) )
194- const exit = context . enter ( 'mathFlow' )
197+ const exit = state . enter ( 'mathFlow' )
195198 let value = tracker . move ( sequence )
196199
197200 if ( node . meta ) {
198- const subexit = context . enter ( 'mathFlowMeta' )
201+ const subexit = state . enter ( 'mathFlowMeta' )
199202 value += tracker . move (
200- context . safe ( node . meta , {
201- before : value ,
203+ state . safe ( node . meta , {
202204 after : '\n' ,
205+ before : value ,
203206 encode : [ '$' ] ,
204207 ...tracker . current ( )
205208 } )
@@ -224,10 +227,7 @@ export function mathToMarkdown(options) {
224227 */
225228 // Note: fixing this code? Please also fix the similar code for inline code:
226229 // <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 ) {
231231 let value = node . value || ''
232232 let size = 1
233233
@@ -267,15 +267,15 @@ export function mathToMarkdown(options) {
267267 // We can’t escape characters in `inlineMath`, but because eols are
268268 // transformed to spaces when going from markdown to HTML anyway, we can swap
269269 // 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 ]
272272
273273 // Only look for `atBreak`s.
274274 // Btw: note that `atBreak` patterns will always start the regex at LF or
275275 // CR.
276276 if ( ! pattern . atBreak ) continue
277277
278- const expression = context . compilePattern ( pattern )
278+ const expression = state . compilePattern ( pattern )
279279 /** @type {RegExpExecArray | null } */
280280 let match
281281
0 commit comments