1+ /**
2+ * @typedef {import('mdast').Literal } Literal
3+ * @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
4+ * @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
5+ * @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
6+ * @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
10+ */
11+
112import { longestStreak } from 'longest-streak'
2- import safe from 'mdast-util-to-markdown/lib/util/safe.js'
13+ import { safe } from 'mdast-util-to-markdown/lib/util/safe.js'
314
15+ /** @type {FromMarkdownExtension } */
416export const mathFromMarkdown = {
517 enter : {
618 mathFlow : enterMathFlow ,
@@ -17,6 +29,7 @@ export const mathFromMarkdown = {
1729 }
1830}
1931
32+ /** @type {ToMarkdownExtension } */
2033export const mathToMarkdown = {
2134 unsafe : [
2235 { character : '\r' , inConstruct : [ 'mathFlowMeta' ] } ,
@@ -29,79 +42,94 @@ export const mathToMarkdown = {
2942
3043inlineMath . peek = inlineMathPeek
3144
45+ /** @type {FromMarkdownHandle } */
3246function enterMathFlow ( token ) {
33- this . enter (
34- {
35- type : 'math' ,
36- meta : null ,
37- value : '' ,
38- data : {
39- hName : 'div' ,
40- hProperties : { className : [ 'math' , 'math-display' ] } ,
41- hChildren : [ { type : 'text' , value : '' } ]
42- }
43- } ,
44- token
45- )
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 )
4660}
4761
62+ /** @type {FromMarkdownHandle } */
4863function enterMathFlowMeta ( ) {
4964 this . buffer ( )
5065}
5166
67+ /** @type {FromMarkdownHandle } */
5268function exitMathFlowMeta ( ) {
5369 const data = this . resume ( )
5470 this . stack [ this . stack . length - 1 ] . meta = data
5571}
5672
73+ /** @type {FromMarkdownHandle } */
5774function exitMathFlowFence ( ) {
5875 // Exit if this is the closing fence.
5976 if ( this . getData ( 'mathFlowInside' ) ) return
6077 this . buffer ( )
6178 this . setData ( 'mathFlowInside' , true )
6279}
6380
81+ /** @type {FromMarkdownHandle } */
6482function exitMathFlow ( token ) {
6583 const data = this . resume ( ) . replace ( / ^ ( \r ? \n | \r ) | ( \r ? \n | \r ) $ / g, '' )
6684 const node = this . exit ( token )
6785 node . value = data
86+ // @ts -expect-error: we defined it.
6887 node . data . hChildren [ 0 ] . value = data
6988 this . setData ( 'mathFlowInside' )
7089}
7190
91+ /** @type {FromMarkdownHandle } */
7292function enterMathText ( token ) {
73- this . enter (
74- {
75- type : 'inlineMath' ,
76- value : '' ,
77- data : {
78- hName : 'span' ,
79- hProperties : { className : [ 'math' , 'math-inline' ] } ,
80- hChildren : [ { type : 'text' , value : '' } ]
81- }
82- } ,
83- token
84- )
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 )
85105 this . buffer ( )
86106}
87107
108+ /** @type {FromMarkdownHandle } */
88109function exitMathText ( token ) {
89110 const data = this . resume ( )
90111 const node = this . exit ( token )
91112 node . value = data
113+ // @ts -expect-error: we defined it.
92114 node . data . hChildren [ 0 ] . value = data
93115}
94116
117+ /** @type {FromMarkdownHandle } */
95118function exitMathData ( token ) {
96119 this . config . enter . data . call ( this , token )
97120 this . config . exit . data . call ( this , token )
98121}
99122
123+ /**
124+ * @type {ToMarkdownHandle }
125+ * @param {Math } node
126+ */
100127function math ( node , _ , context ) {
101128 const raw = node . value || ''
102129 const fence = '$' . repeat ( Math . max ( longestStreak ( raw , '$' ) + 1 , 2 ) )
103130 const exit = context . enter ( 'mathFlow' )
104131 let value = fence
132+ /** @type {ReturnType<context['enter']> } */
105133 let subexit
106134
107135 if ( node . meta ) {
@@ -121,6 +149,10 @@ function math(node, _, context) {
121149 return value
122150}
123151
152+ /**
153+ * @type {ToMarkdownHandle }
154+ * @param {InlineMath } node
155+ */
124156function inlineMath ( node ) {
125157 const value = node . value || ''
126158 let size = 1
@@ -147,6 +179,7 @@ function inlineMath(node) {
147179 return sequence + pad + value + pad + sequence
148180}
149181
182+ /** @type {ToMarkdownHandle } */
150183function inlineMathPeek ( ) {
151184 return '$'
152185}
0 commit comments