@@ -15,6 +15,7 @@ import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phras
15
15
import { containerFlow } from 'mdast-util-to-markdown/lib/util/container-flow.js'
16
16
import { indentLines } from 'mdast-util-to-markdown/lib/util/indent-lines.js'
17
17
import { safe } from 'mdast-util-to-markdown/lib/util/safe.js'
18
+ import { track } from 'mdast-util-to-markdown/lib/util/track.js'
18
19
19
20
/** @type {FromMarkdownExtension } */
20
21
export const footnoteFromMarkdown = {
@@ -116,13 +117,22 @@ function exitNote(token) {
116
117
* @type {ToMarkdownHandle }
117
118
* @param {FootnoteReference } node
118
119
*/
119
- function footnoteReference ( node , _ , context ) {
120
+ function footnoteReference ( node , _ , context , safeOptions ) {
121
+ const tracker = track ( safeOptions )
122
+ let value = tracker . move ( '[^' )
120
123
const exit = context . enter ( 'footnoteReference' )
121
124
const subexit = context . enter ( 'reference' )
122
- const reference = safe ( context , association ( node ) , { before : '^' , after : ']' } )
125
+ value += tracker . move (
126
+ safe ( context , association ( node ) , {
127
+ ...tracker . current ( ) ,
128
+ before : value ,
129
+ after : ']'
130
+ } )
131
+ )
123
132
subexit ( )
124
133
exit ( )
125
- return '[^' + reference + ']'
134
+ value += tracker . move ( ']' )
135
+ return value
126
136
}
127
137
128
138
/** @type {ToMarkdownHandle } */
@@ -134,13 +144,21 @@ function footnoteReferencePeek() {
134
144
* @type {ToMarkdownHandle }
135
145
* @param {Footnote } node
136
146
*/
137
- function footnote ( node , _ , context ) {
147
+ function footnote ( node , _ , context , safeOptions ) {
148
+ const tracker = track ( safeOptions )
149
+ let value = tracker . move ( '^[' )
138
150
const exit = context . enter ( 'footnote' )
139
151
const subexit = context . enter ( 'label' )
140
- const value =
141
- '^[' + containerPhrasing ( node , context , { before : '[' , after : ']' } ) + ']'
152
+ value += tracker . move (
153
+ containerPhrasing ( node , context , {
154
+ ...tracker . current ( ) ,
155
+ before : value ,
156
+ after : ']'
157
+ } )
158
+ )
142
159
subexit ( )
143
160
exit ( )
161
+ value += tracker . move ( ']' )
144
162
return value
145
163
}
146
164
@@ -153,13 +171,26 @@ function footnotePeek() {
153
171
* @type {ToMarkdownHandle }
154
172
* @param {FootnoteDefinition } node
155
173
*/
156
- function footnoteDefinition ( node , _ , context ) {
174
+ function footnoteDefinition ( node , _ , context , safeOptions ) {
175
+ const tracker = track ( safeOptions )
176
+ let value = tracker . move ( '[^' )
157
177
const exit = context . enter ( 'footnoteDefinition' )
158
178
const subexit = context . enter ( 'label' )
159
- const label =
160
- '[^' + safe ( context , association ( node ) , { before : '^' , after : ']' } ) + ']:'
179
+ value += tracker . move (
180
+ safe ( context , association ( node ) , {
181
+ ...tracker . current ( ) ,
182
+ before : value ,
183
+ after : ']'
184
+ } )
185
+ )
161
186
subexit ( )
162
- const value = indentLines ( containerFlow ( node , context ) , map )
187
+ value += tracker . move (
188
+ ']:' + ( node . children && node . children . length > 0 ? ' ' : '' )
189
+ )
190
+ tracker . shift ( 4 )
191
+ value += tracker . move (
192
+ indentLines ( containerFlow ( node , context , tracker . current ( ) ) , map )
193
+ )
163
194
exit ( )
164
195
165
196
return value
@@ -170,6 +201,6 @@ function footnoteDefinition(node, _, context) {
170
201
return ( blank ? '' : ' ' ) + line
171
202
}
172
203
173
- return ( blank ? label : label + ' ' ) + line
204
+ return line
174
205
}
175
206
}
0 commit comments