1
+ /**
2
+ * @typedef {import('mdast').Footnote } Footnote
3
+ * @typedef {import('mdast').FootnoteReference } FootnoteReference
4
+ * @typedef {import('mdast').FootnoteDefinition } FootnoteDefinition
5
+ * @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
6
+ * @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
7
+ * @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
8
+ * @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
9
+ * @typedef {import('mdast-util-to-markdown').Map } Map
10
+ */
11
+
1
12
import { normalizeIdentifier } from 'micromark-util-normalize-identifier'
2
- import association from 'mdast-util-to-markdown/lib/util/association.js'
3
- import phrasing from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
4
- import flow from 'mdast-util-to-markdown/lib/util/container-flow.js'
5
- import indentLines from 'mdast-util-to-markdown/lib/util/indent-lines.js'
6
- import safe from 'mdast-util-to-markdown/lib/util/safe.js'
13
+ import { association } from 'mdast-util-to-markdown/lib/util/association.js'
14
+ import { containerPhrasing } from 'mdast-util-to-markdown/lib/util/container-phrasing.js'
15
+ import { containerFlow } from 'mdast-util-to-markdown/lib/util/container-flow.js'
16
+ import { indentLines } from 'mdast-util-to-markdown/lib/util/indent-lines.js'
17
+ import { safe } from 'mdast-util-to-markdown/lib/util/safe.js'
7
18
19
+ /** @type {FromMarkdownExtension } */
8
20
export const footnoteFromMarkdown = {
9
21
canContainEols : [ 'footnote' ] ,
10
22
enter : {
@@ -23,6 +35,7 @@ export const footnoteFromMarkdown = {
23
35
}
24
36
}
25
37
38
+ /** @type {ToMarkdownExtension } */
26
39
export const footnoteToMarkdown = {
27
40
// This is on by default already.
28
41
unsafe : [ { character : '[' , inConstruct : [ 'phrasing' , 'label' , 'reference' ] } ] ,
@@ -32,17 +45,20 @@ export const footnoteToMarkdown = {
32
45
footnoteReference . peek = footnoteReferencePeek
33
46
footnote . peek = footnotePeek
34
47
48
+ /** @type {FromMarkdownHandle } */
35
49
function enterFootnoteDefinition ( token ) {
36
50
this . enter (
37
51
{ type : 'footnoteDefinition' , identifier : '' , label : '' , children : [ ] } ,
38
52
token
39
53
)
40
54
}
41
55
56
+ /** @type {FromMarkdownHandle } */
42
57
function enterFootnoteDefinitionLabelString ( ) {
43
58
this . buffer ( )
44
59
}
45
60
61
+ /** @type {FromMarkdownHandle } */
46
62
function exitFootnoteDefinitionLabelString ( token ) {
47
63
const label = this . resume ( )
48
64
this . stack [ this . stack . length - 1 ] . label = label
@@ -51,18 +67,22 @@ function exitFootnoteDefinitionLabelString(token) {
51
67
) . toLowerCase ( )
52
68
}
53
69
70
+ /** @type {FromMarkdownHandle } */
54
71
function exitFootnoteDefinition ( token ) {
55
72
this . exit ( token )
56
73
}
57
74
75
+ /** @type {FromMarkdownHandle } */
58
76
function enterFootnoteCall ( token ) {
59
77
this . enter ( { type : 'footnoteReference' , identifier : '' , label : '' } , token )
60
78
}
61
79
80
+ /** @type {FromMarkdownHandle } */
62
81
function enterFootnoteCallString ( ) {
63
82
this . buffer ( )
64
83
}
65
84
85
+ /** @type {FromMarkdownHandle } */
66
86
function exitFootnoteCallString ( token ) {
67
87
const label = this . resume ( )
68
88
this . stack [ this . stack . length - 1 ] . label = label
@@ -71,18 +91,25 @@ function exitFootnoteCallString(token) {
71
91
) . toLowerCase ( )
72
92
}
73
93
94
+ /** @type {FromMarkdownHandle } */
74
95
function exitFootnoteCall ( token ) {
75
96
this . exit ( token )
76
97
}
77
98
99
+ /** @type {FromMarkdownHandle } */
78
100
function enterNote ( token ) {
79
101
this . enter ( { type : 'footnote' , children : [ ] } , token )
80
102
}
81
103
104
+ /** @type {FromMarkdownHandle } */
82
105
function exitNote ( token ) {
83
106
this . exit ( token )
84
107
}
85
108
109
+ /**
110
+ * @type {ToMarkdownHandle }
111
+ * @param {FootnoteReference } node
112
+ */
86
113
function footnoteReference ( node , _ , context ) {
87
114
const exit = context . enter ( 'footnoteReference' )
88
115
const subexit = context . enter ( 'reference' )
@@ -92,34 +119,46 @@ function footnoteReference(node, _, context) {
92
119
return '[^' + reference + ']'
93
120
}
94
121
122
+ /** @type {ToMarkdownHandle } */
95
123
function footnoteReferencePeek ( ) {
96
124
return '['
97
125
}
98
126
127
+ /**
128
+ * @type {ToMarkdownHandle }
129
+ * @param {Footnote } node
130
+ */
99
131
function footnote ( node , _ , context ) {
100
132
const exit = context . enter ( 'footnote' )
101
133
const subexit = context . enter ( 'label' )
102
- const value = '^[' + phrasing ( node , context , { before : '[' , after : ']' } ) + ']'
134
+ const value =
135
+ '^[' + containerPhrasing ( node , context , { before : '[' , after : ']' } ) + ']'
103
136
subexit ( )
104
137
exit ( )
105
138
return value
106
139
}
107
140
141
+ /** @type {ToMarkdownHandle } */
108
142
function footnotePeek ( ) {
109
143
return '^'
110
144
}
111
145
146
+ /**
147
+ * @type {ToMarkdownHandle }
148
+ * @param {FootnoteDefinition } node
149
+ */
112
150
function footnoteDefinition ( node , _ , context ) {
113
151
const exit = context . enter ( 'footnoteDefinition' )
114
152
const subexit = context . enter ( 'label' )
115
153
const label =
116
154
'[^' + safe ( context , association ( node ) , { before : '^' , after : ']' } ) + ']:'
117
155
subexit ( )
118
- const value = indentLines ( flow ( node , context ) , map )
156
+ const value = indentLines ( containerFlow ( node , context ) , map )
119
157
exit ( )
120
158
121
159
return value
122
160
161
+ /** @type {Map } */
123
162
function map ( line , index , blank ) {
124
163
if ( index ) {
125
164
return ( blank ? '' : ' ' ) + line
0 commit comments