Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where li with first child that's a block rendered incorrectly #447

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "turndown",
"description": "A library that converts HTML to Markdown",
"version": "7.1.2",
"version": "7.1.3",
"author": "Dom Christie",
"main": "lib/turndown.cjs.js",
"module": "lib/turndown.es.js",
Expand Down
17 changes: 16 additions & 1 deletion src/commonmark-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,28 @@ rules.listItem = {
.replace(/\n/gm, '\n ') // indent
var prefix = options.bulletListMarker + ' '
var parent = node.parentNode

// Check if the first child is a block-level element
var blockElements = [
'address', 'article', 'aside', 'blockquote', 'canvas', 'dd', 'div',
'dl', 'dt', 'fieldset', 'figcaption', 'figure', 'footer', 'form',
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hr', 'li', 'main',
'nav', 'noscript', 'ol', 'p', 'pre', 'section', 'table', 'tfoot',
'ul', 'video'
].map(function (t) {
return t.toUpperCase()
})

var firstChildIsBlock = node.firstChild && blockElements.includes(node.firstChild.nodeName)

if (parent.nodeName === 'OL') {
var start = parent.getAttribute('start')
var index = Array.prototype.indexOf.call(parent.children, node)
prefix = (start ? Number(start) + index : index + 1) + '. '
}

return (
prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '')
prefix + (firstChildIsBlock ? '\n\n ' : '') + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '')
)
}
}
Expand Down
64 changes: 56 additions & 8 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,38 @@
* Unordered list item 3</pre>
</div>

<div class="case" data-name="ol with li that has block-level first child (heading)">
<div class="input">
<ol>
<li>Inline</li>
<li>
<h1>Block</h1>
</li>
</ol>
</div>
<pre class="expected">1. Inline
2.

Block
=====</pre>
</div>

<div class="case" data-name="ul with li that has block-level first child (heading)">
<div class="input">
<ul>
<li>Inline</li>
<li>
<h1>Block</h1>
</li>
</ul>
</div>
<pre class="expected">* Inline
*

Block
=====</pre>
</div>

<div class="case" data-name="ul">
<div class="input">
<ul>
Expand Down Expand Up @@ -437,7 +469,9 @@
<li>List item without paragraph</li>
</ul>
</div>
<pre class="expected">* List item with paragraph
<pre class="expected">*

List item with paragraph

* List item without paragraph</pre>
</div>
Expand All @@ -454,11 +488,15 @@
</li>
</ol>
</div>
<pre class="expected">1. This is a paragraph in a list item.
<pre class="expected">1.

This is a paragraph in a list item.

This is a paragraph in the same list item as above.

2. A paragraph in a second list item.</pre>
2.

A paragraph in a second list item.</pre>
</div>

<div class="case" data-name="nested uls">
Expand All @@ -484,9 +522,13 @@
</div>
<pre class="expected">* This is a list item at root level
* This is another item at root level
* * This is a nested list item
*

* This is a nested list item
* This is another nested list item
* * This is a deeply nested list item
*

* This is a deeply nested list item
* This is another deeply nested list item
* This is a third deeply nested list item
* This is a third item at root level</pre>
Expand Down Expand Up @@ -515,9 +557,13 @@
</div>
<pre class="expected">* This is a list item at root level
* This is another item at root level
* 1. This is a nested list item
*

1. This is a nested list item
2. This is another nested list item
3. * This is a deeply nested list item
3.

* This is a deeply nested list item
* This is another deeply nested list item
* This is a third deeply nested list item
* This is a third item at root level</pre>
Expand All @@ -534,7 +580,9 @@
</li>
</ul>
</div>
<pre class="expected">* A list item with a blockquote:
<pre class="expected">*

A list item with a blockquote:

> This is a blockquote inside a list item.</pre>
</div>
Expand Down