1
1
/**
2
- * @typedef {Extract< import('mdast').Root|import('mdast'). Content, import('unist').Parent> } Parent
2
+ * @typedef {import('mdast').Content } Content
3
3
* @typedef {import('mdast').ListItem } ListItem
4
4
* @typedef {import('mdast').Paragraph } Paragraph
5
- * @typedef {import('mdast').BlockContent } BlockContent
5
+ * @typedef {import('mdast').Parent } Parent
6
+ * @typedef {import('mdast').Root } Root
6
7
* @typedef {import('mdast-util-from-markdown').CompileContext } CompileContext
7
8
* @typedef {import('mdast-util-from-markdown').Extension } FromMarkdownExtension
8
9
* @typedef {import('mdast-util-from-markdown').Handle } FromMarkdownHandle
9
10
* @typedef {import('mdast-util-to-markdown').Options } ToMarkdownExtension
10
11
* @typedef {import('mdast-util-to-markdown').Handle } ToMarkdownHandle
11
12
*/
12
13
14
+ /**
15
+ * @typedef {Extract<Root | Content, Parent> } Parents
16
+ */
17
+
13
18
import { listItem } from 'mdast-util-to-markdown/lib/handle/list-item.js'
14
19
import { track } from 'mdast-util-to-markdown/lib/util/track.js'
15
20
16
- /** @type {FromMarkdownExtension } */
21
+ // To do: next major: rename `context` -> `state`, `safeOptions` -> `info`, use
22
+ // `track` from `state`.
23
+ // To do: next major: replace exports with functions.
24
+ // To do: next major: use `defaulthandlers.listItem`.
25
+
26
+ /**
27
+ * Extension for `mdast-util-from-markdown` to enable GFM task list items.
28
+ *
29
+ * @type {FromMarkdownExtension }
30
+ */
17
31
export const gfmTaskListItemFromMarkdown = {
18
32
exit : {
19
33
taskListCheckValueChecked : exitCheck ,
@@ -22,7 +36,11 @@ export const gfmTaskListItemFromMarkdown = {
22
36
}
23
37
}
24
38
25
- /** @type {ToMarkdownExtension } */
39
+ /**
40
+ * Extension for `mdast-util-to-markdown` to enable GFM task list items.
41
+ *
42
+ * @type {ToMarkdownExtension }
43
+ */
26
44
export const gfmTaskListItemToMarkdown = {
27
45
unsafe : [ { atBreak : true , character : '-' , after : '[:|-]' } ] ,
28
46
handlers : { listItem : listItemWithTaskListItem }
@@ -43,43 +61,45 @@ function exitCheck(token) {
43
61
* @type {FromMarkdownHandle }
44
62
*/
45
63
function exitParagraphWithTaskListItem ( token ) {
46
- const parent = /** @type {Parent } */ ( this . stack [ this . stack . length - 2 ] )
47
- const node = /** @type {Paragraph } */ ( this . stack [ this . stack . length - 1 ] )
48
- const siblings = parent . children
49
- const head = node . children [ 0 ]
50
- let index = - 1
51
- /** @type {Paragraph|undefined } */
52
- let firstParaghraph
64
+ const parent = /** @type {Parents } */ ( this . stack [ this . stack . length - 2 ] )
53
65
54
66
if (
55
67
parent &&
56
68
parent . type === 'listItem' &&
57
- typeof parent . checked === 'boolean' &&
58
- head &&
59
- head . type === 'text'
69
+ typeof parent . checked === 'boolean'
60
70
) {
61
- while ( ++ index < siblings . length ) {
62
- const sibling = siblings [ index ]
63
- if ( sibling . type === 'paragraph' ) {
64
- firstParaghraph = sibling
65
- break
71
+ const node = /** @type {Paragraph } */ ( this . stack [ this . stack . length - 1 ] )
72
+ const head = node . children [ 0 ]
73
+
74
+ if ( head && head . type === 'text' ) {
75
+ const siblings = parent . children
76
+ let index = - 1
77
+ /** @type {Paragraph | undefined } */
78
+ let firstParaghraph
79
+
80
+ while ( ++ index < siblings . length ) {
81
+ const sibling = siblings [ index ]
82
+ if ( sibling . type === 'paragraph' ) {
83
+ firstParaghraph = sibling
84
+ break
85
+ }
66
86
}
67
- }
68
87
69
- if ( firstParaghraph === node ) {
70
- // Must start with a space or a tab.
71
- head . value = head . value . slice ( 1 )
72
-
73
- if ( head . value . length === 0 ) {
74
- node . children . shift ( )
75
- } else if (
76
- node . position &&
77
- head . position &&
78
- typeof head . position . start . offset === 'number'
79
- ) {
80
- head . position . start . column ++
81
- head . position . start . offset ++
82
- node . position . start = Object . assign ( { } , head . position . start )
88
+ if ( firstParaghraph === node ) {
89
+ // Must start with a space or a tab.
90
+ head . value = head . value . slice ( 1 )
91
+
92
+ if ( head . value . length === 0 ) {
93
+ node . children . shift ( )
94
+ } else if (
95
+ node . position &&
96
+ head . position &&
97
+ typeof head . position . start . offset === 'number'
98
+ ) {
99
+ head . position . start . column ++
100
+ head . position . start . offset ++
101
+ node . position . start = Object . assign ( { } , head . position . start )
102
+ }
83
103
}
84
104
}
85
105
}
0 commit comments