1
1
/**
2
+ * @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
2
3
* @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
3
4
* @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
4
5
* @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
@@ -38,7 +39,10 @@ export function mathFromMarkdown() {
38
39
}
39
40
}
40
41
41
- /** @type {FromMarkdownHandle } */
42
+ /**
43
+ * @this {CompileContext}
44
+ * @type {FromMarkdownHandle }
45
+ */
42
46
function enterMathFlow ( token ) {
43
47
this . enter (
44
48
{
@@ -55,27 +59,39 @@ export function mathFromMarkdown() {
55
59
)
56
60
}
57
61
58
- /** @type {FromMarkdownHandle } */
62
+ /**
63
+ * @this {CompileContext}
64
+ * @type {FromMarkdownHandle }
65
+ */
59
66
function enterMathFlowMeta ( ) {
60
67
this . buffer ( )
61
68
}
62
69
63
- /** @type {FromMarkdownHandle } */
70
+ /**
71
+ * @this {CompileContext}
72
+ * @type {FromMarkdownHandle }
73
+ */
64
74
function exitMathFlowMeta ( ) {
65
75
const data = this . resume ( )
66
76
const node = /** @type {Math } */ ( this . stack [ this . stack . length - 1 ] )
67
77
node . meta = data
68
78
}
69
79
70
- /** @type {FromMarkdownHandle } */
80
+ /**
81
+ * @this {CompileContext}
82
+ * @type {FromMarkdownHandle }
83
+ */
71
84
function exitMathFlowFence ( ) {
72
85
// Exit if this is the closing fence.
73
86
if ( this . getData ( 'mathFlowInside' ) ) return
74
87
this . buffer ( )
75
88
this . setData ( 'mathFlowInside' , true )
76
89
}
77
90
78
- /** @type {FromMarkdownHandle } */
91
+ /**
92
+ * @this {CompileContext}
93
+ * @type {FromMarkdownHandle }
94
+ */
79
95
function exitMathFlow ( token ) {
80
96
const data = this . resume ( ) . replace ( / ^ ( \r ? \n | \r ) | ( \r ? \n | \r ) $ / g, '' )
81
97
const node = /** @type {Math } */ ( this . exit ( token ) )
@@ -85,7 +101,10 @@ export function mathFromMarkdown() {
85
101
this . setData ( 'mathFlowInside' )
86
102
}
87
103
88
- /** @type {FromMarkdownHandle } */
104
+ /**
105
+ * @this {CompileContext}
106
+ * @type {FromMarkdownHandle }
107
+ */
89
108
function enterMathText ( token ) {
90
109
this . enter (
91
110
{
@@ -102,7 +121,10 @@ export function mathFromMarkdown() {
102
121
this . buffer ( )
103
122
}
104
123
105
- /** @type {FromMarkdownHandle } */
124
+ /**
125
+ * @this {CompileContext}
126
+ * @type {FromMarkdownHandle }
127
+ */
106
128
function exitMathText ( token ) {
107
129
const data = this . resume ( )
108
130
const node = /** @type {Math } */ ( this . exit ( token ) )
@@ -111,7 +133,10 @@ export function mathFromMarkdown() {
111
133
node . data . hChildren [ 0 ] . value = data
112
134
}
113
135
114
- /** @type {FromMarkdownHandle } */
136
+ /**
137
+ * @this {CompileContext}
138
+ * @type {FromMarkdownHandle }
139
+ */
115
140
function exitMathData ( token ) {
116
141
this . config . enter . data . call ( this , token )
117
142
this . config . exit . data . call ( this , token )
@@ -133,8 +158,11 @@ export function mathToMarkdown(options = {}) {
133
158
134
159
return {
135
160
unsafe : [
161
+ // @ts -expect-error: To do: use context map.
136
162
{ character : '\r' , inConstruct : [ 'mathFlowMeta' ] } ,
163
+ // @ts -expect-error: To do: use context map.
137
164
{ character : '\r' , inConstruct : [ 'mathFlowMeta' ] } ,
165
+ // @ts -expect-error: To do: use context map.
138
166
single
139
167
? { character : '$' , inConstruct : [ 'mathFlowMeta' , 'phrasing' ] }
140
168
: {
@@ -154,11 +182,13 @@ export function mathToMarkdown(options = {}) {
154
182
function math ( node , _ , context , safeOptions ) {
155
183
const raw = node . value || ''
156
184
const sequence = '$' . repeat ( Math . max ( longestStreak ( raw , '$' ) + 1 , 2 ) )
185
+ // @ts -expect-error: To do: use context map.
157
186
const exit = context . enter ( 'mathFlow' )
158
187
const tracker = track ( safeOptions )
159
188
let value = tracker . move ( sequence )
160
189
161
190
if ( node . meta ) {
191
+ // @ts -expect-error: To do: use context map.
162
192
const subexit = context . enter ( 'mathFlowMeta' )
163
193
value += tracker . move (
164
194
safe ( context , node . meta , {
0 commit comments