Skip to content

Commit ef53234

Browse files
committed
Add support for position tracking
1 parent 3a00da9 commit ef53234

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

index.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import {longestStreak} from 'longest-streak'
1818
import {safe} from 'mdast-util-to-markdown/lib/util/safe.js'
19+
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
1920

2021
/**
2122
* @returns {FromMarkdownExtension}
@@ -150,29 +151,33 @@ export function mathToMarkdown(options = {}) {
150151
* @type {ToMarkdownHandle}
151152
* @param {Math} node
152153
*/
153-
function math(node, _, context) {
154+
function math(node, _, context, safeOptions) {
154155
const raw = node.value || ''
155-
const fence = '$'.repeat(Math.max(longestStreak(raw, '$') + 1, 2))
156+
const sequence = '$'.repeat(Math.max(longestStreak(raw, '$') + 1, 2))
156157
const exit = context.enter('mathFlow')
157-
let value = fence
158+
const tracker = track(safeOptions)
159+
let value = tracker.move(sequence)
158160

159161
if (node.meta) {
160162
const subexit = context.enter('mathFlowMeta')
161-
value += safe(context, node.meta, {
162-
before: '$',
163-
after: ' ',
164-
encode: ['$']
165-
})
163+
value += tracker.move(
164+
safe(context, node.meta, {
165+
...tracker.current(),
166+
before: value,
167+
after: ' ',
168+
encode: ['$']
169+
})
170+
)
166171
subexit()
167172
}
168173

169-
value += '\n'
174+
value += tracker.move('\n')
170175

171176
if (raw) {
172-
value += raw + '\n'
177+
value += tracker.move(raw + '\n')
173178
}
174179

175-
value += fence
180+
value += tracker.move(sequence)
176181
exit()
177182
return value
178183
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"dependencies": {
3939
"@types/mdast": "^3.0.0",
4040
"longest-streak": "^3.0.0",
41-
"mdast-util-to-markdown": "^1.0.0"
41+
"mdast-util-to-markdown": "^1.3.0"
4242
},
4343
"devDependencies": {
4444
"@types/tape": "^4.0.0",

0 commit comments

Comments
 (0)