1
1
'use strict'
2
2
3
- module . exports = dataList
3
+ module . exports = dl
4
4
5
- var is = require ( 'hast-util-is-element' )
6
- var wrapListItems = require ( '../util/wrap-list-items' )
5
+ var convert = require ( 'hast-util-is-element/convert' )
7
6
var spread = require ( '../util/list-items-spread' )
7
+ var wrapListItems = require ( '../util/wrap-list-items' )
8
8
9
- function dataList ( h , node ) {
9
+ var div = convert ( 'div' )
10
+ var dt = convert ( 'dt' )
11
+ var dd = convert ( 'dd' )
12
+
13
+ function dl ( h , node ) {
10
14
var children = node . children
11
- var length = children . length
12
15
var index = - 1
13
16
var clean = [ ]
14
17
var groups = [ ]
18
+ var group = { titles : [ ] , definitions : [ ] }
15
19
var content
16
- var breakpoint
17
- var title
18
20
var child
19
- var group = { titles : [ ] , definitions : [ ] }
20
21
21
22
// Unwrap `<div>`s
22
- while ( ++ index < length ) {
23
+ while ( ++ index < children . length ) {
23
24
child = children [ index ]
24
- clean = clean . concat ( is ( child , 'div' ) ? child . children : child )
25
+ clean = clean . concat ( div ( child ) ? child . children : child )
25
26
}
26
27
27
- length = clean . length
28
28
index = - 1
29
29
30
30
// Group titles and definitions.
31
- while ( ++ index < length ) {
31
+ while ( ++ index < clean . length ) {
32
32
child = clean [ index ]
33
- title = is ( child , 'dt' )
34
33
35
- if ( title && breakpoint ) {
36
- groups . push ( group )
37
- group = { titles : [ ] , definitions : [ ] }
38
- }
34
+ if ( dt ( child ) ) {
35
+ if ( dd ( clean [ index - 1 ] ) ) {
36
+ groups . push ( group )
37
+ group = { titles : [ ] , definitions : [ ] }
38
+ }
39
39
40
- group [ title ? 'titles' : 'definitions' ] . push ( child )
41
- breakpoint = is ( child , 'dd' )
40
+ group . titles . push ( child )
41
+ } else {
42
+ group . definitions . push ( child )
43
+ }
42
44
}
43
45
44
46
groups . push ( group )
45
47
46
48
// Create items.
47
- length = groups . length
48
49
index = - 1
49
50
content = [ ]
50
51
51
- while ( ++ index < length ) {
52
- group = groups [ index ]
53
- group = handle ( h , group . titles ) . concat ( handle ( h , group . definitions ) )
52
+ while ( ++ index < groups . length ) {
53
+ group = handle ( h , groups [ index ] . titles ) . concat (
54
+ handle ( h , groups [ index ] . definitions )
55
+ )
54
56
55
- if ( group . length > 0 ) {
57
+ if ( group . length ) {
56
58
content . push ( {
57
59
type : 'listItem' ,
58
60
spread : group . length > 1 ,
@@ -63,7 +65,7 @@ function dataList(h, node) {
63
65
}
64
66
65
67
// Create a list if there are items.
66
- if ( content . length > 0 ) {
68
+ if ( content . length ) {
67
69
return h (
68
70
node ,
69
71
'list' ,
@@ -73,11 +75,11 @@ function dataList(h, node) {
73
75
}
74
76
}
75
77
76
- function handle ( h , category ) {
77
- var nodes = wrapListItems ( h , { children : category } )
78
+ function handle ( h , children ) {
79
+ var nodes = wrapListItems ( h , { children : children } )
78
80
79
- if ( nodes . length === 0 ) {
80
- return [ ]
81
+ if ( ! nodes . length ) {
82
+ return nodes
81
83
}
82
84
83
85
if ( nodes . length === 1 ) {
0 commit comments