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

Alter list items (standalone and with header) #358

Open
wants to merge 1 commit 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
7 changes: 7 additions & 0 deletions src/commonmark-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ rules.heading = {
replacement: function (content, node, options) {
var hLevel = Number(node.nodeName.charAt(1))

var parent = node.parentNode
if (parent.nodeName === 'LI') {
if (!content.trim()) return ''
return options.strongDelimiter + content + options.strongDelimiter
}

if (options.headingStyle === 'setext' && hLevel < 3) {
var underline = repeat((hLevel === 1 ? '=' : '-'), content.length)
return (
Expand Down Expand Up @@ -74,6 +80,7 @@ rules.listItem = {
prefix = (start ? Number(start) + index : index + 1) + '. '
}
return (
(node.previousSibling && node.previousSibling.nodeName !== 'LI' ? '\n' : '') +
prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '')
)
}
Expand Down
17 changes: 17 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,15 @@ <h2>This is a header.</h2>
3. Chapter Three with trailing whitespace</pre>
</div>

<div class="case" data-name="header inside list item">
<div class="input">
<ul>
<li><h1>Header inside list item</h1></li>
</ul>
</div>
<pre class="expected">* **Header inside list item**</pre>
</div>

<div class="case" data-name="multilined and bizarre formatting">
<div class="input">
<ul>
Expand All @@ -671,6 +680,14 @@ <h2>This is a header.</h2>
* Leading space, text, lots of whitespace … text</pre>
</div>

<div class="case" data-name="standalone list item">
<div class="input">
<img src="logo.png" alt="Img with alt"> <li>Standalone list item</li>
</div>
<pre class="expected">![Img with alt](logo.png)
* Standalone list item</pre>
</div>

<div class="case" data-name="whitespace between inline elements">
<div class="input">
<p>I <a href="http://example.com/need">need</a> <a href="http://www.example.com/more">more</a> spaces!</p>
Expand Down