Skip to content

Commit

Permalink
Output paragraph element for last item in loose list (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoseley authored and rexxars committed Oct 11, 2018
1 parent 6ab90a7 commit 3bee068
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/ast-to-react.js
Expand Up @@ -80,7 +80,7 @@ function getNodeProps(node, key, opts, renderer, parent, index) {
props.tight = !node.loose
props.ordered = node.ordered
props.index = node.index
props.children = (props.tight ? unwrapParagraphs(node) : node.children).map((childNode, i) => {
props.children = getListItemChildren(node, parent).map((childNode, i) => {
return astToReact(childNode, opts, {node: node, props: props}, i)
})
break
Expand Down Expand Up @@ -200,6 +200,18 @@ function flattenPosition(pos) {
.join('')
}

function getListItemChildren(node, parent) {
if (node.loose) {
return node.children;
}

if (parent.node && node.index > 0 && parent.node.children[node.index - 1].loose) {
return node.children;
}

return unwrapParagraphs(node);
}

function unwrapParagraphs(node) {
return node.children.reduce((array, child) => {
return array.concat(child.type === 'paragraph' ? child.children || [] : [child])
Expand Down
12 changes: 9 additions & 3 deletions test/__snapshots__/react-markdown.test.js.snap
Expand Up @@ -186,7 +186,9 @@ Array [
</p>
</li>
<li>
bar
<p>
bar
</p>
</li>
</ul>,
<h2>
Expand Down Expand Up @@ -657,7 +659,9 @@ Array [
</p>
</li>
<li>
bar
<p>
bar
</p>
</li>
</ul>,
<h2>
Expand Down Expand Up @@ -1618,7 +1622,9 @@ exports[`should handle loose, unordered lists 1`] = `
</p>
</li>
<li>
bar
<p>
bar
</p>
</li>
</ul>
`;
Expand Down

0 comments on commit 3bee068

Please sign in to comment.