Skip to content

Commit 06361e2

Browse files
Hammswooorm
andcommitted
Fix spreading of lists and list items
Related to syntax-tree/mdast#4. Related to remarkjs/remark#349. Related to remarkjs/remark#350. Related to remarkjs/remark#364. Closes GH-23. Co-authored-by: Titus Wormer <[email protected]>
1 parent eded45e commit 06361e2

File tree

7 files changed

+279
-225
lines changed

7 files changed

+279
-225
lines changed

lib/footer.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,36 @@ function generateFootnotes(h) {
1212
var index = -1
1313
var listItems = []
1414
var def
15+
var backReference
16+
var content
17+
var tail
1518

1619
if (!length) {
1720
return null
1821
}
1922

2023
while (++index < length) {
2124
def = footnotes[index]
25+
content = def.children.concat()
26+
tail = content[content.length - 1]
27+
backReference = {
28+
type: 'link',
29+
url: '#fnref-' + def.identifier,
30+
data: {hProperties: {className: ['footnote-backref']}},
31+
children: [{type: 'text', value: '↩'}]
32+
}
33+
34+
if (!tail || tail.type !== 'paragraph') {
35+
tail = {type: 'paragraph', children: []}
36+
content.push(tail)
37+
}
38+
39+
tail.children.push(backReference)
2240

2341
listItems[index] = {
2442
type: 'listItem',
2543
data: {hProperties: {id: 'fn-' + def.identifier}},
26-
children: def.children.concat({
27-
type: 'link',
28-
url: '#fnref-' + def.identifier,
29-
data: {hProperties: {className: ['footnote-backref']}},
30-
children: [{type: 'text', value: '↩'}]
31-
}),
44+
children: content,
3245
position: def.position
3346
}
3447
}
@@ -40,11 +53,7 @@ function generateFootnotes(h) {
4053
wrap(
4154
[
4255
thematicBreak(h),
43-
list(h, {
44-
type: 'list',
45-
ordered: true,
46-
children: listItems
47-
})
56+
list(h, {type: 'list', ordered: true, children: listItems})
4857
],
4958
true
5059
)

lib/handlers/list-item.js

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,40 @@ var all = require('../all')
99
function listItem(h, node, parent) {
1010
var children = node.children
1111
var head = children[0]
12+
var raw = all(h, node)
13+
var loose = parent ? listLoose(parent) : listItemLoose(node)
1214
var props = {}
13-
var single = false
1415
var result
1516
var container
17+
var index
18+
var length
19+
var child
1620

17-
if (
18-
(!parent || !parent.loose) &&
19-
children.length === 1 &&
20-
head.type === 'paragraph'
21-
) {
22-
single = true
23-
}
21+
/* Tight lists should not render 'paragraph' nodes as 'p' tags */
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]
2431

25-
result = all(h, single ? head : node)
32+
if (child.tagName === 'p') {
33+
result = result.concat(child.children)
34+
} else {
35+
result.push(child)
36+
}
37+
}
38+
}
2639

2740
if (typeof node.checked === 'boolean') {
28-
if (!single && (!head || head.type !== 'paragraph')) {
41+
if (loose && (!head || head.type !== 'paragraph')) {
2942
result.unshift(h(null, 'p', []))
3043
}
3144

32-
container = single ? result : result[0].children
45+
container = loose ? result[0].children : result
3346

3447
if (container.length !== 0) {
3548
container.unshift(u('text', ' '))
@@ -47,9 +60,30 @@ function listItem(h, node, parent) {
4760
props.className = ['task-list-item']
4861
}
4962

50-
if (!single && result.length !== 0) {
63+
if (loose && result.length !== 0) {
5164
result = wrap(result, true)
5265
}
5366

5467
return h(node, 'li', props, result)
5568
}
69+
70+
function listLoose(node) {
71+
var loose = node.spread
72+
var children = node.children
73+
var length = children.length
74+
var index = -1
75+
76+
while (!loose && ++index < length) {
77+
loose = listItemLoose(children[index])
78+
}
79+
80+
return loose
81+
}
82+
83+
function listItemLoose(node) {
84+
var spread = node.spread
85+
86+
return spread === undefined || spread === null
87+
? node.children.length > 1
88+
: spread
89+
}

test/footnote-definition.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ var to = require('..')
77
test('FootnoteDefinition', function(t) {
88
t.equal(
99
to(
10-
u(
11-
'footnoteDefinition',
12-
{
13-
identifier: 'zulu'
14-
},
15-
[u('paragraph', [u('text', 'alpha')])]
16-
)
10+
u('footnoteDefinition', {identifier: 'zulu'}, [
11+
u('paragraph', [u('text', 'alpha')])
12+
])
1713
),
1814
null,
1915
'should ignore `footnoteDefinition`'

0 commit comments

Comments
 (0)