Skip to content

Commit e236216

Browse files
committed
Add location tracking
1 parent 6e75123 commit e236216

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

index.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {association} from 'mdast-util-to-markdown/lib/util/association.js'
1313
import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js'
1414
import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js'
1515
import {safe} from 'mdast-util-to-markdown/lib/util/safe.js'
16+
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
1617
import {visit, EXIT} from 'unist-util-visit'
1718

1819
let 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
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
],
3636
"dependencies": {
3737
"@types/mdast": "^3.0.0",
38-
"mdast-util-to-markdown": "^1.0.0",
38+
"mdast-util-to-markdown": "^1.3.0",
3939
"micromark-util-normalize-identifier": "^1.0.0",
4040
"unist-util-visit": "^4.0.0"
4141
},

0 commit comments

Comments
 (0)