Skip to content

Commit 84693b9

Browse files
committed
Refactor utilities
1 parent cf92230 commit 84693b9

File tree

10 files changed

+77
-63
lines changed

10 files changed

+77
-63
lines changed

lib/handlers/blockquote.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
module.exports = blockquote
44

5-
var all = require('../all')
6-
var wrap = require('../wrap')
5+
var wrapChildren = require('../util/wrap-children')
76

87
function blockquote(h, node) {
9-
return h(node, 'blockquote', wrap(all(h, node)))
8+
return h(node, 'blockquote', wrapChildren(h, node))
109
}

lib/handlers/code.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ var trim = require('trim-trailing-lines')
1010
var prefix = 'language-'
1111

1212
function code(h, node) {
13-
var values = node.children
14-
var length = values.length
13+
var children = node.children
14+
var length = children.length
1515
var index = -1
16-
var value
16+
var child
1717
var classList
18+
var className
1819
var lang
1920

2021
if (node.tagName === 'pre') {
2122
while (++index < length) {
22-
value = values[index]
23-
if (is(value, 'code') && has(value, 'className')) {
24-
classList = value.properties.className
23+
child = children[index]
24+
25+
if (is(child, 'code') && has(child, 'className')) {
26+
classList = child.properties.className
2527
break
2628
}
2729
}
@@ -32,10 +34,10 @@ function code(h, node) {
3234
index = -1
3335

3436
while (++index < length) {
35-
value = classList[index]
37+
className = classList[index]
3638

37-
if (value.slice(0, prefix.length) === prefix) {
38-
lang = value.slice(prefix.length)
39+
if (className.slice(0, prefix.length) === prefix) {
40+
lang = className.slice(prefix.length)
3941
break
4042
}
4143
}

lib/handlers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
var all = require('../all')
4-
var wrapped = require('../wrap-children')
4+
var wrapped = require('../util/wrap-children')
55

66
exports.root = require('./root')
77
exports.text = require('./text')

lib/handlers/list-item.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
module.exports = listItem
44

55
var is = require('hast-util-is-element')
6-
var all = require('../all')
7-
var wrap = require('../wrap')
6+
var wrapChildren = require('../util/wrap-children')
87

98
function listItem(h, node) {
109
var children = node.children
1110
var head = children[0]
1211
var checked = null
13-
var loose = false
1412
var checkbox
1513
var grandchildren
1614
var content
@@ -26,12 +24,10 @@ function listItem(h, node) {
2624
checkbox.properties.type === 'checkbox'
2725
) {
2826
checked = Boolean(checkbox.properties.checked)
29-
} else {
30-
checkbox = null
3127
}
3228
}
3329

34-
content = wrap(all(h, node))
30+
content = wrapChildren(h, node)
3531

3632
/* Remove initial spacing if we previously found a checkbox. */
3733
if (checked !== null) {
@@ -48,21 +44,19 @@ function listItem(h, node) {
4844
}
4945
}
5046

51-
if (content.length > 1) {
52-
/* More than one node is always loose. */
53-
loose = true
54-
} else if (content.length === 1) {
47+
/* Drop empty paragraph. */
48+
if (content.length === 1) {
5549
head = content[0]
5650

57-
if (head.type === 'paragraph') {
58-
/* One `paragraph`, always tight. Ensure it isn’t empty. */
59-
loose = false
60-
61-
if (head.children.length === 0) {
62-
content = []
63-
}
51+
if (head.type === 'paragraph' && head.children.length === 0) {
52+
content = []
6453
}
6554
}
6655

67-
return h(node, 'listItem', {loose: loose, checked: checked}, content)
56+
return h(
57+
node,
58+
'listItem',
59+
{loose: content.length > 1, checked: checked},
60+
content
61+
)
6862
}

lib/handlers/list.js

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,48 +3,24 @@
33
module.exports = list
44

55
var has = require('hast-util-has-property')
6-
var all = require('../all')
6+
var wrapListItems = require('../util/wrap-list-items')
7+
var loose = require('../util/list-loose')
78

89
function list(h, node) {
910
var ordered = node.tagName === 'ol'
1011
var start = null
11-
var loose = false
12-
var content
13-
var child
14-
var length
15-
var index
12+
var children
1613

1714
if (ordered) {
1815
start = has(node, 'start') ? node.properties.start : 1
1916
}
2017

21-
content = all(h, node)
22-
length = content.length
23-
index = -1
24-
25-
while (++index < length) {
26-
child = content[index]
27-
28-
if (child.type !== 'listItem') {
29-
child = {
30-
type: 'listItem',
31-
loose: false,
32-
checked: null,
33-
children: [child]
34-
}
35-
36-
content[index] = child
37-
}
38-
39-
if (child.loose === true) {
40-
loose = true
41-
}
42-
}
18+
children = wrapListItems(h, node)
4319

4420
return h(
4521
node,
4622
'list',
47-
{ordered: ordered, start: start, loose: loose},
48-
content
23+
{ordered: ordered, start: start, loose: loose(children)},
24+
children
4925
)
5026
}

lib/handlers/root.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module.exports = root
44

55
var all = require('../all')
6-
var wrap = require('../wrap')
6+
var wrap = require('../util/wrap')
77

88
function root(h, node) {
99
var children = all(h, node)

lib/util/list-loose.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
module.exports = loose
4+
5+
function loose(children) {
6+
var length = children.length
7+
var index = -1
8+
9+
while (++index < length) {
10+
if (children[index].loose) {
11+
return true
12+
}
13+
}
14+
15+
return false
16+
}

lib/wrap-children.js renamed to lib/util/wrap-children.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = wrapped
44

5-
var all = require('./all')
5+
var all = require('../all')
66
var wrap = require('./wrap')
77

88
function wrapped(h, node) {

lib/util/wrap-list-items.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
module.exports = wrapped
4+
5+
var all = require('../all')
6+
7+
function wrapped(h, node) {
8+
var children = all(h, node)
9+
var length = children.length
10+
var index = -1
11+
var child
12+
13+
while (++index < length) {
14+
child = children[index]
15+
16+
if (child.type !== 'listItem') {
17+
children[index] = {
18+
type: 'listItem',
19+
loose: false,
20+
checked: null,
21+
children: [child]
22+
}
23+
}
24+
}
25+
26+
return children
27+
}
File renamed without changes.

0 commit comments

Comments
 (0)