Skip to content
This repository was archived by the owner on Aug 9, 2023. It is now read-only.

Commit beecf3e

Browse files
committed
Add location tracking
1 parent e95aa2c commit beecf3e

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

index.js

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {containerPhrasing} from 'mdast-util-to-markdown/lib/util/container-phras
1515
import {containerFlow} from 'mdast-util-to-markdown/lib/util/container-flow.js'
1616
import {indentLines} from 'mdast-util-to-markdown/lib/util/indent-lines.js'
1717
import {safe} from 'mdast-util-to-markdown/lib/util/safe.js'
18+
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
1819

1920
/** @type {FromMarkdownExtension} */
2021
export const footnoteFromMarkdown = {
@@ -116,13 +117,22 @@ function exitNote(token) {
116117
* @type {ToMarkdownHandle}
117118
* @param {FootnoteReference} node
118119
*/
119-
function footnoteReference(node, _, context) {
120+
function footnoteReference(node, _, context, safeOptions) {
121+
const tracker = track(safeOptions)
122+
let value = tracker.move('[^')
120123
const exit = context.enter('footnoteReference')
121124
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+
)
123132
subexit()
124133
exit()
125-
return '[^' + reference + ']'
134+
value += tracker.move(']')
135+
return value
126136
}
127137

128138
/** @type {ToMarkdownHandle} */
@@ -134,13 +144,21 @@ function footnoteReferencePeek() {
134144
* @type {ToMarkdownHandle}
135145
* @param {Footnote} node
136146
*/
137-
function footnote(node, _, context) {
147+
function footnote(node, _, context, safeOptions) {
148+
const tracker = track(safeOptions)
149+
let value = tracker.move('^[')
138150
const exit = context.enter('footnote')
139151
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+
)
142159
subexit()
143160
exit()
161+
value += tracker.move(']')
144162
return value
145163
}
146164

@@ -153,13 +171,26 @@ function footnotePeek() {
153171
* @type {ToMarkdownHandle}
154172
* @param {FootnoteDefinition} node
155173
*/
156-
function footnoteDefinition(node, _, context) {
174+
function footnoteDefinition(node, _, context, safeOptions) {
175+
const tracker = track(safeOptions)
176+
let value = tracker.move('[^')
157177
const exit = context.enter('footnoteDefinition')
158178
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+
)
161186
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+
)
163194
exit()
164195

165196
return value
@@ -170,6 +201,6 @@ function footnoteDefinition(node, _, context) {
170201
return (blank ? '' : ' ') + line
171202
}
172203

173-
return (blank ? label : label + ' ') + line
204+
return line
174205
}
175206
}

0 commit comments

Comments
 (0)