Skip to content

Commit

Permalink
Add support for position tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 31, 2022
1 parent 3a00da9 commit ef53234
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 16 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {longestStreak} from 'longest-streak'
import {safe} from 'mdast-util-to-markdown/lib/util/safe.js'
import {track} from 'mdast-util-to-markdown/lib/util/track.js'

/**
* @returns {FromMarkdownExtension}
Expand Down Expand Up @@ -150,29 +151,33 @@ export function mathToMarkdown(options = {}) {
* @type {ToMarkdownHandle}
* @param {Math} node
*/
function math(node, _, context) {
function math(node, _, context, safeOptions) {
const raw = node.value || ''
const fence = '$'.repeat(Math.max(longestStreak(raw, '$') + 1, 2))
const sequence = '$'.repeat(Math.max(longestStreak(raw, '$') + 1, 2))
const exit = context.enter('mathFlow')
let value = fence
const tracker = track(safeOptions)
let value = tracker.move(sequence)

if (node.meta) {
const subexit = context.enter('mathFlowMeta')
value += safe(context, node.meta, {
before: '$',
after: ' ',
encode: ['$']
})
value += tracker.move(
safe(context, node.meta, {
...tracker.current(),
before: value,
after: ' ',
encode: ['$']
})
)
subexit()
}

value += '\n'
value += tracker.move('\n')

if (raw) {
value += raw + '\n'
value += tracker.move(raw + '\n')
}

value += fence
value += tracker.move(sequence)
exit()
return value
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"dependencies": {
"@types/mdast": "^3.0.0",
"longest-streak": "^3.0.0",
"mdast-util-to-markdown": "^1.0.0"
"mdast-util-to-markdown": "^1.3.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
Expand Down

0 comments on commit ef53234

Please sign in to comment.