11/**
2- * @typedef {import('mdast').Literal } Literal
32 * @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
43 * @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
54 * @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
65 * @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
7- *
8- * @typedef {Literal & {type: 'math', lang?: string|null, meta?: string|null} } Math
9- * @typedef {Literal & {type: 'inlineMath'} } InlineMath
6+ * @typedef {import('./complex-types').Math } Math
7+ * @typedef {import('./complex-types').InlineMath } InlineMath
108 */
119
1210import { longestStreak } from 'longest-streak'
@@ -44,19 +42,19 @@ inlineMath.peek = inlineMathPeek
4442
4543/** @type {FromMarkdownHandle } */
4644function enterMathFlow ( token ) {
47- /** @type { Math } */
48- const node = {
49- type : 'math' ,
50- meta : null ,
51- value : '' ,
52- data : {
53- hName : 'div' ,
54- hProperties : { className : [ 'math' , 'math-display' ] } ,
55- hChildren : [ { type : 'text' , value : '' } ]
56- }
57- }
58- // @ts -expect-error: custom node.
59- this . enter ( node , token )
45+ this . enter (
46+ {
47+ type : 'math' ,
48+ meta : null ,
49+ value : '' ,
50+ data : {
51+ hName : 'div' ,
52+ hProperties : { className : [ 'math' , 'math-display' ] } ,
53+ hChildren : [ { type : 'text' , value : '' } ]
54+ }
55+ } ,
56+ token
57+ )
6058}
6159
6260/** @type {FromMarkdownHandle } */
@@ -67,7 +65,8 @@ function enterMathFlowMeta() {
6765/** @type {FromMarkdownHandle } */
6866function exitMathFlowMeta ( ) {
6967 const data = this . resume ( )
70- this . stack [ this . stack . length - 1 ] . meta = data
68+ const node = /** @type {Math } */ ( this . stack [ this . stack . length - 1 ] )
69+ node . meta = data
7170}
7271
7372/** @type {FromMarkdownHandle } */
@@ -81,7 +80,7 @@ function exitMathFlowFence() {
8180/** @type {FromMarkdownHandle } */
8281function exitMathFlow ( token ) {
8382 const data = this . resume ( ) . replace ( / ^ ( \r ? \n | \r ) | ( \r ? \n | \r ) $ / g, '' )
84- const node = this . exit ( token )
83+ const node = /** @type { Math } */ ( this . exit ( token ) )
8584 node . value = data
8685 // @ts -expect-error: we defined it.
8786 node . data . hChildren [ 0 ] . value = data
@@ -90,25 +89,25 @@ function exitMathFlow(token) {
9089
9190/** @type {FromMarkdownHandle } */
9291function enterMathText ( token ) {
93- /** @type { InlineMath } */
94- const node = {
95- type : 'inlineMath' ,
96- value : '' ,
97- data : {
98- hName : 'span' ,
99- hProperties : { className : [ 'math' , 'math-inline' ] } ,
100- hChildren : [ { type : 'text' , value : '' } ]
101- }
102- }
103- // @ts -expect-error: custom node.
104- this . enter ( node , token )
92+ this . enter (
93+ {
94+ type : 'inlineMath' ,
95+ value : '' ,
96+ data : {
97+ hName : 'span' ,
98+ hProperties : { className : [ 'math' , 'math-inline' ] } ,
99+ hChildren : [ { type : 'text' , value : '' } ]
100+ }
101+ } ,
102+ token
103+ )
105104 this . buffer ( )
106105}
107106
108107/** @type {FromMarkdownHandle } */
109108function exitMathText ( token ) {
110109 const data = this . resume ( )
111- const node = this . exit ( token )
110+ const node = /** @type { Math } */ ( this . exit ( token ) )
112111 node . value = data
113112 // @ts -expect-error: we defined it.
114113 node . data . hChildren [ 0 ] . value = data
@@ -129,11 +128,9 @@ function math(node, _, context) {
129128 const fence = '$' . repeat ( Math . max ( longestStreak ( raw , '$' ) + 1 , 2 ) )
130129 const exit = context . enter ( 'mathFlow' )
131130 let value = fence
132- /** @type {ReturnType<context['enter']> } */
133- let subexit
134131
135132 if ( node . meta ) {
136- subexit = context . enter ( 'mathFlowMeta' )
133+ const subexit = context . enter ( 'mathFlowMeta' )
137134 value += safe ( context , node . meta , { before : '$' , after : ' ' , encode : [ '$' ] } )
138135 subexit ( )
139136 }
0 commit comments