3
3
module . exports = listItem
4
4
5
5
var u = require ( 'unist-builder' )
6
- var wrap = require ( '../wrap' )
7
6
var all = require ( '../all' )
8
7
9
8
function listItem ( h , node , parent ) {
10
- var children = node . children
11
- var head = children [ 0 ]
12
- var raw = all ( h , node )
9
+ var result = all ( h , node )
10
+ var head = result [ 0 ]
13
11
var loose = parent ? listLoose ( parent ) : listItemLoose ( node )
14
12
var props = { }
15
- var result
16
- var container
17
- var index
13
+ var wrapped = [ ]
18
14
var length
15
+ var index
19
16
var child
20
17
21
- // Tight lists should not render `paragraph` nodes as `p` elements.
22
- if ( loose ) {
23
- result = raw
24
- } else {
25
- result = [ ]
26
- length = raw . length
27
- index = - 1
28
-
29
- while ( ++ index < length ) {
30
- child = raw [ index ]
31
-
32
- if ( child . tagName === 'p' ) {
33
- result = result . concat ( child . children )
34
- } else {
35
- result . push ( child )
36
- }
37
- }
38
- }
39
-
40
18
if ( typeof node . checked === 'boolean' ) {
41
- if ( loose && ( ! head || head . type !== 'paragraph' ) ) {
42
- result . unshift ( h ( null , 'p' , [ ] ) )
19
+ if ( ! head || head . tagName !== 'p' ) {
20
+ head = h ( null , 'p' , [ ] )
21
+ result . unshift ( head )
43
22
}
44
23
45
- container = loose ? result [ 0 ] . children : result
46
-
47
- if ( container . length !== 0 ) {
48
- container . unshift ( u ( 'text' , ' ' ) )
24
+ if ( head . children . length !== 0 ) {
25
+ head . children . unshift ( u ( 'text' , ' ' ) )
49
26
}
50
27
51
- container . unshift (
28
+ head . children . unshift (
52
29
h ( null , 'input' , {
53
30
type : 'checkbox' ,
54
31
checked : node . checked ,
@@ -61,11 +38,30 @@ function listItem(h, node, parent) {
61
38
props . className = [ 'task-list-item' ]
62
39
}
63
40
64
- if ( loose && result . length !== 0 ) {
65
- result = wrap ( result , true )
41
+ length = result . length
42
+ index = - 1
43
+
44
+ while ( ++ index < length ) {
45
+ child = result [ index ]
46
+
47
+ // Add eols before nodes, except if this is a loose, first paragraph.
48
+ if ( loose || index !== 0 || child . tagName !== 'p' ) {
49
+ wrapped . push ( u ( 'text' , '\n' ) )
50
+ }
51
+
52
+ if ( child . tagName === 'p' && ! loose ) {
53
+ wrapped = wrapped . concat ( child . children )
54
+ } else {
55
+ wrapped . push ( child )
56
+ }
57
+ }
58
+
59
+ // Add a final eol.
60
+ if ( length && ( loose || child . tagName !== 'p' ) ) {
61
+ wrapped . push ( u ( 'text' , '\n' ) )
66
62
}
67
63
68
- return h ( node , 'li' , props , result )
64
+ return h ( node , 'li' , props , wrapped )
69
65
}
70
66
71
67
function listLoose ( node ) {
0 commit comments