Skip to content

Commit 2375b2f

Browse files
committed
Add support for position tracking
1 parent 21c30fc commit 2375b2f

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
import {listItem} from 'mdast-util-to-markdown/lib/handle/list-item.js'
13+
import {track} from 'mdast-util-to-markdown/lib/util/track.js'
1314

1415
/** @type {FromMarkdownExtension} */
1516
export const gfmTaskListItemFromMarkdown = {
@@ -83,11 +84,23 @@ function exitParagraphWithTaskListItem(token) {
8384
* @type {ToMarkdownHandle}
8485
* @param {ListItem} node
8586
*/
86-
function listItemWithTaskListItem(node, parent, context) {
87+
function listItemWithTaskListItem(node, parent, context, safeOptions) {
8788
const head = node.children[0]
88-
let value = listItem(node, parent, context)
89+
const checkable =
90+
typeof node.checked === 'boolean' && head && head.type === 'paragraph'
91+
const checkbox = '[' + (node.checked ? 'x' : ' ') + '] '
92+
const tracker = track(safeOptions)
8993

90-
if (typeof node.checked === 'boolean' && head && head.type === 'paragraph') {
94+
if (checkable) {
95+
tracker.move(checkbox)
96+
}
97+
98+
let value = listItem(node, parent, context, {
99+
...safeOptions,
100+
...tracker.current()
101+
})
102+
103+
if (checkable) {
91104
value = value.replace(/^(?:[*+-]|\d+\.)([\r\n]| {1,3})/, check)
92105
}
93106

@@ -98,6 +111,6 @@ function listItemWithTaskListItem(node, parent, context) {
98111
* @returns {string}
99112
*/
100113
function check($0) {
101-
return $0 + '[' + (node.checked ? 'x' : ' ') + '] '
114+
return $0 + checkbox
102115
}
103116
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
"index.js"
3939
],
4040
"dependencies": {
41-
"@types/mdast": "^3.0.3",
42-
"mdast-util-to-markdown": "^1.0.0"
41+
"@types/mdast": "^3.0.0",
42+
"mdast-util-to-markdown": "^1.3.0"
4343
},
4444
"devDependencies": {
4545
"@types/tape": "^4.0.0",

0 commit comments

Comments
 (0)