11'use strict'
22
3- module . exports = dataList
3+ module . exports = dl
44
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' )
76var spread = require ( '../util/list-items-spread' )
7+ var wrapListItems = require ( '../util/wrap-list-items' )
88
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 ) {
1014 var children = node . children
11- var length = children . length
1215 var index = - 1
1316 var clean = [ ]
1417 var groups = [ ]
18+ var group = { titles : [ ] , definitions : [ ] }
1519 var content
16- var breakpoint
17- var title
1820 var child
19- var group = { titles : [ ] , definitions : [ ] }
2021
2122 // Unwrap `<div>`s
22- while ( ++ index < length ) {
23+ while ( ++ index < children . length ) {
2324 child = children [ index ]
24- clean = clean . concat ( is ( child , 'div' ) ? child . children : child )
25+ clean = clean . concat ( div ( child ) ? child . children : child )
2526 }
2627
27- length = clean . length
2828 index = - 1
2929
3030 // Group titles and definitions.
31- while ( ++ index < length ) {
31+ while ( ++ index < clean . length ) {
3232 child = clean [ index ]
33- title = is ( child , 'dt' )
3433
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+ }
3939
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+ }
4244 }
4345
4446 groups . push ( group )
4547
4648 // Create items.
47- length = groups . length
4849 index = - 1
4950 content = [ ]
5051
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+ )
5456
55- if ( group . length > 0 ) {
57+ if ( group . length ) {
5658 content . push ( {
5759 type : 'listItem' ,
5860 spread : group . length > 1 ,
@@ -63,7 +65,7 @@ function dataList(h, node) {
6365 }
6466
6567 // Create a list if there are items.
66- if ( content . length > 0 ) {
68+ if ( content . length ) {
6769 return h (
6870 node ,
6971 'list' ,
@@ -73,11 +75,11 @@ function dataList(h, node) {
7375 }
7476}
7577
76- function handle ( h , category ) {
77- var nodes = wrapListItems ( h , { children : category } )
78+ function handle ( h , children ) {
79+ var nodes = wrapListItems ( h , { children : children } )
7880
79- if ( nodes . length === 0 ) {
80- return [ ]
81+ if ( ! nodes . length ) {
82+ return nodes
8183 }
8284
8385 if ( nodes . length === 1 ) {
0 commit comments