@@ -13,6 +13,7 @@ import {association} from 'mdast-util-to-markdown/lib/util/association.js'
13
13
import { containerFlow } from 'mdast-util-to-markdown/lib/util/container-flow.js'
14
14
import { indentLines } from 'mdast-util-to-markdown/lib/util/indent-lines.js'
15
15
import { safe } from 'mdast-util-to-markdown/lib/util/safe.js'
16
+ import { track } from 'mdast-util-to-markdown/lib/util/track.js'
16
17
import { visit , EXIT } from 'unist-util-visit'
17
18
18
19
let warningColonInFootnote = false
@@ -111,16 +112,22 @@ export function gfmFootnoteToMarkdown() {
111
112
* @type {ToMarkdownHandle }
112
113
* @param {FootnoteReference } node
113
114
*/
114
- function footnoteReference ( node , _ , context ) {
115
+ function footnoteReference ( node , _ , context , safeOptions ) {
116
+ const tracker = track ( safeOptions )
117
+ let value = tracker . move ( '[^' )
115
118
const exit = context . enter ( 'footnoteReference' )
116
119
const subexit = context . enter ( 'reference' )
117
- const reference = safe ( context , association ( node ) , {
118
- before : '^' ,
119
- after : ']'
120
- } )
120
+ value += tracker . move (
121
+ safe ( context , association ( node ) , {
122
+ ...tracker . current ( ) ,
123
+ before : value ,
124
+ after : ']'
125
+ } )
126
+ )
121
127
subexit ( )
122
128
exit ( )
123
- return '[^' + reference + ']'
129
+ value += tracker . move ( ']' )
130
+ return value
124
131
}
125
132
126
133
/** @type {ToMarkdownHandle } */
@@ -132,13 +139,25 @@ export function gfmFootnoteToMarkdown() {
132
139
* @type {ToMarkdownHandle }
133
140
* @param {FootnoteDefinition } node
134
141
*/
135
- function footnoteDefinition ( node , _ , context ) {
142
+ function footnoteDefinition ( node , _ , context , safeOptions ) {
143
+ const tracker = track ( safeOptions )
144
+ let value = tracker . move ( '[^' )
136
145
const exit = context . enter ( 'footnoteDefinition' )
137
146
const subexit = context . enter ( 'label' )
138
- const id = safe ( context , association ( node ) , { before : '^' , after : ']' } )
139
- const label = '[^' + id + ']:'
147
+ const id = safe ( context , association ( node ) , {
148
+ ...tracker . current ( ) ,
149
+ before : value ,
150
+ after : ']'
151
+ } )
152
+ value += tracker . move ( id )
153
+ value += tracker . move (
154
+ ']:' + ( node . children && node . children . length > 0 ? ' ' : '' )
155
+ )
140
156
subexit ( )
141
- const value = indentLines ( containerFlow ( node , context ) , map )
157
+ tracker . shift ( 4 )
158
+ value += tracker . move (
159
+ indentLines ( containerFlow ( node , context , tracker . current ( ) ) , map )
160
+ )
142
161
exit ( )
143
162
144
163
if ( ! warningColonInFootnote && id . includes ( ':' ) ) {
@@ -168,7 +187,7 @@ export function gfmFootnoteToMarkdown() {
168
187
return ( blank ? '' : ' ' ) + line
169
188
}
170
189
171
- return ( blank ? label : label + ' ' ) + line
190
+ return line
172
191
}
173
192
}
174
193
}
0 commit comments