@@ -13,6 +13,7 @@ import {association} from 'mdast-util-to-markdown/lib/util/association.js'
1313import { containerFlow } from 'mdast-util-to-markdown/lib/util/container-flow.js'
1414import { indentLines } from 'mdast-util-to-markdown/lib/util/indent-lines.js'
1515import { safe } from 'mdast-util-to-markdown/lib/util/safe.js'
16+ import { track } from 'mdast-util-to-markdown/lib/util/track.js'
1617import { visit , EXIT } from 'unist-util-visit'
1718
1819let warningColonInFootnote = false
@@ -111,16 +112,22 @@ export function gfmFootnoteToMarkdown() {
111112 * @type {ToMarkdownHandle }
112113 * @param {FootnoteReference } node
113114 */
114- function footnoteReference ( node , _ , context ) {
115+ function footnoteReference ( node , _ , context , safeOptions ) {
116+ const tracker = track ( safeOptions )
117+ let value = tracker . move ( '[^' )
115118 const exit = context . enter ( 'footnoteReference' )
116119 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+ )
121127 subexit ( )
122128 exit ( )
123- return '[^' + reference + ']'
129+ value += tracker . move ( ']' )
130+ return value
124131 }
125132
126133 /** @type {ToMarkdownHandle } */
@@ -132,13 +139,25 @@ export function gfmFootnoteToMarkdown() {
132139 * @type {ToMarkdownHandle }
133140 * @param {FootnoteDefinition } node
134141 */
135- function footnoteDefinition ( node , _ , context ) {
142+ function footnoteDefinition ( node , _ , context , safeOptions ) {
143+ const tracker = track ( safeOptions )
144+ let value = tracker . move ( '[^' )
136145 const exit = context . enter ( 'footnoteDefinition' )
137146 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+ )
140156 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+ )
142161 exit ( )
143162
144163 if ( ! warningColonInFootnote && id . includes ( ':' ) ) {
@@ -168,7 +187,7 @@ export function gfmFootnoteToMarkdown() {
168187 return ( blank ? '' : ' ' ) + line
169188 }
170189
171- return ( blank ? label : label + ' ' ) + line
190+ return line
172191 }
173192 }
174193}
0 commit comments