Skip to content

Commit

Permalink
Fix spreading of lists and list items
Browse files Browse the repository at this point in the history
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 <tituswormer@gmail.com>
  • Loading branch information
Hamms and wooorm committed Nov 11, 2018
1 parent eded45e commit 06361e2
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 225 deletions.
31 changes: 20 additions & 11 deletions lib/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,36 @@ function generateFootnotes(h) {
var index = -1
var listItems = []
var def
var backReference
var content
var tail

if (!length) {
return null
}

while (++index < length) {
def = footnotes[index]
content = def.children.concat()
tail = content[content.length - 1]
backReference = {
type: 'link',
url: '#fnref-' + def.identifier,
data: {hProperties: {className: ['footnote-backref']}},
children: [{type: 'text', value: '↩'}]
}

if (!tail || tail.type !== 'paragraph') {
tail = {type: 'paragraph', children: []}
content.push(tail)
}

tail.children.push(backReference)

listItems[index] = {
type: 'listItem',
data: {hProperties: {id: 'fn-' + def.identifier}},
children: def.children.concat({
type: 'link',
url: '#fnref-' + def.identifier,
data: {hProperties: {className: ['footnote-backref']}},
children: [{type: 'text', value: '↩'}]
}),
children: content,
position: def.position
}
}
Expand All @@ -40,11 +53,7 @@ function generateFootnotes(h) {
wrap(
[
thematicBreak(h),
list(h, {
type: 'list',
ordered: true,
children: listItems
})
list(h, {type: 'list', ordered: true, children: listItems})
],
true
)
Expand Down
58 changes: 46 additions & 12 deletions lib/handlers/list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,40 @@ var all = require('../all')
function listItem(h, node, parent) {
var children = node.children
var head = children[0]
var raw = all(h, node)
var loose = parent ? listLoose(parent) : listItemLoose(node)
var props = {}
var single = false
var result
var container
var index
var length
var child

if (
(!parent || !parent.loose) &&
children.length === 1 &&
head.type === 'paragraph'
) {
single = true
}
/* Tight lists should not render 'paragraph' nodes as 'p' tags */
if (loose) {
result = raw
} else {
result = []
length = raw.length
index = -1

while (++index < length) {
child = raw[index]

result = all(h, single ? head : node)
if (child.tagName === 'p') {
result = result.concat(child.children)
} else {
result.push(child)
}
}
}

if (typeof node.checked === 'boolean') {
if (!single && (!head || head.type !== 'paragraph')) {
if (loose && (!head || head.type !== 'paragraph')) {
result.unshift(h(null, 'p', []))
}

container = single ? result : result[0].children
container = loose ? result[0].children : result

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

if (!single && result.length !== 0) {
if (loose && result.length !== 0) {
result = wrap(result, true)
}

return h(node, 'li', props, result)
}

function listLoose(node) {
var loose = node.spread
var children = node.children
var length = children.length
var index = -1

while (!loose && ++index < length) {
loose = listItemLoose(children[index])
}

return loose
}

function listItemLoose(node) {
var spread = node.spread

return spread === undefined || spread === null
? node.children.length > 1
: spread
}
10 changes: 3 additions & 7 deletions test/footnote-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ var to = require('..')
test('FootnoteDefinition', function(t) {
t.equal(
to(
u(
'footnoteDefinition',
{
identifier: 'zulu'
},
[u('paragraph', [u('text', 'alpha')])]
)
u('footnoteDefinition', {identifier: 'zulu'}, [
u('paragraph', [u('text', 'alpha')])
])
),
null,
'should ignore `footnoteDefinition`'
Expand Down
Loading

0 comments on commit 06361e2

Please sign in to comment.