1
1
/**
2
- * @typedef {import('mdast').Content } Content
3
2
* @typedef {import('mdast').ListItem } ListItem
4
3
* @typedef {import('mdast').Paragraph } Paragraph
5
- * @typedef {import('mdast').Parent } Parent
6
- * @typedef {import('mdast').Root } Root
7
4
* @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
8
5
* @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
9
6
* @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
10
7
* @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
11
8
* @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
12
9
*/
13
10
14
- /**
15
- * @typedef {Extract<Root | Content, Parent> } Parents
16
- */
17
-
11
+ import { ok as assert } from 'devlop'
18
12
import { defaultHandlers } from 'mdast-util-to-markdown'
19
13
20
- // To do: next major: rename `context` -> `state`, `safeOptions` -> `info`, use
21
- // `track` from `state`.
22
14
// To do: next major: replace exports with functions.
23
- // To do: next major: use `defaulthandlers.listItem`.
24
15
25
16
/**
26
17
* Extension for `mdast-util-from-markdown` to enable GFM task list items.
@@ -50,8 +41,9 @@ export const gfmTaskListItemToMarkdown = {
50
41
* @type {FromMarkdownHandle }
51
42
*/
52
43
function exitCheck ( token ) {
53
- const node = /** @type {ListItem } */ ( this . stack [ this . stack . length - 2 ] )
54
44
// We’re always in a paragraph, in a list item.
45
+ const node = this . stack [ this . stack . length - 2 ]
46
+ assert ( node . type === 'listItem' )
55
47
node . checked = token . type === 'taskListCheckValueChecked'
56
48
}
57
49
@@ -60,14 +52,15 @@ function exitCheck(token) {
60
52
* @type {FromMarkdownHandle }
61
53
*/
62
54
function exitParagraphWithTaskListItem ( token ) {
63
- const parent = /** @type { Parents } */ ( this . stack [ this . stack . length - 2 ] )
55
+ const parent = this . stack [ this . stack . length - 2 ]
64
56
65
57
if (
66
58
parent &&
67
59
parent . type === 'listItem' &&
68
60
typeof parent . checked === 'boolean'
69
61
) {
70
- const node = /** @type {Paragraph } */ ( this . stack [ this . stack . length - 1 ] )
62
+ const node = this . stack [ this . stack . length - 1 ]
63
+ assert ( node . type === 'paragraph' )
71
64
const head = node . children [ 0 ]
72
65
73
66
if ( head && head . type === 'text' ) {
@@ -110,19 +103,19 @@ function exitParagraphWithTaskListItem(token) {
110
103
* @type {ToMarkdownHandle }
111
104
* @param {ListItem } node
112
105
*/
113
- function listItemWithTaskListItem ( node , parent , context , safeOptions ) {
106
+ function listItemWithTaskListItem ( node , parent , state , info ) {
114
107
const head = node . children [ 0 ]
115
108
const checkable =
116
109
typeof node . checked === 'boolean' && head && head . type === 'paragraph'
117
110
const checkbox = '[' + ( node . checked ? 'x' : ' ' ) + '] '
118
- const tracker = context . createTracker ( safeOptions )
111
+ const tracker = state . createTracker ( info )
119
112
120
113
if ( checkable ) {
121
114
tracker . move ( checkbox )
122
115
}
123
116
124
- let value = defaultHandlers . listItem ( node , parent , context , {
125
- ...safeOptions ,
117
+ let value = defaultHandlers . listItem ( node , parent , state , {
118
+ ...info ,
126
119
...tracker . current ( )
127
120
} )
128
121
0 commit comments