Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
FossPrime committed May 11, 2022
1 parent 4499b5c commit 954f574
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
20 changes: 14 additions & 6 deletions src/commonmark-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,24 @@ rules.listItem = {
filter: 'li',

replacement: function (content, node, options) {
const spaces = 2
let prefix = ''
content = content
.replace(/^\n+/, '') // remove leading newlines
.replace(/\n+$/, '\n') // replace trailing newlines with just a single one
.replace(/\n/gm, '\n ') // indent
var prefix = options.bulletListMarker + ' '
var parent = node.parentNode
const parent = node.parentNode
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) + '. '
const start = parseInt(parent.getAttribute('start')) || 0
const digits = Math.log(parent.children.length + start) * Math.LOG10E + 1 | 0
const index = Array.prototype.indexOf.call(parent.children, node)
const itemNumber = (start ? Number(start) + index : index + 1)
const suffix = '.'
const padding = (digits > spaces ? digits + 1 : spaces + 1) + suffix.length // increase padding if beyond 99
prefix = (itemNumber + suffix).padEnd(padding)
content = content.replace(/\n/gm, '\n '.padEnd(1 + padding))
} else {
prefix = options.bulletListMarker + ' '.padEnd(1 + spaces)
content = content.replace(/\n/gm, '\n '.padEnd(3 + spaces)) // indent
}
return (
prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '')
Expand Down
58 changes: 51 additions & 7 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,31 @@
<li>Ordered list item 44</li>
</ol>
</div>
<pre class="expected">42. Ordered list item 42
43. Ordered list item 43
44. Ordered list item 44</pre>
<pre class="expected">42. Ordered list item 42
43. Ordered list item 43
44. Ordered list item 44</pre>
</div>

<div class="case" data-name="ol with increasing start">
<div class="input">
<ol start="9">
<li>Ordered list item 9</li>
<li>Ordered list item 10</li>
</ol>
</div>
<pre class="expected">9. Ordered list item 9
10. Ordered list item 10</pre>
</div>

<div class="case" data-name="ol with high start">
<div class="input">
<ol start="99">
<li>Ordered list item 99</li>
<li>Ordered list item 100</li>
</ol>
</div>
<pre class="expected">99. Ordered list item 99
100. Ordered list item 100</pre>
</div>

<div class="case" data-name="list spacing">
Expand Down Expand Up @@ -461,6 +483,28 @@
2. A paragraph in a second list item.</pre>
</div>

<div class="case" data-name="ol with start and paragraphs">
<div class="input">
<ol start="9">
<li>
<p>This is a paragraph in a list item.</p>
<p>This is a paragraph in the same list item as above.</p>
</li>
<li>
<p>A paragraph in a second list item.</p>
<p>This is a paragraph in the same list item as above.</p>
</li>
</ol>
</div>
<pre class="expected">9. This is a paragraph in a list item.

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

10. A paragraph in a second list item.

This is a paragraph in the same list item as above.</pre>
</div>

<div class="case" data-name="nested uls">
<div class="input">
<ul>
Expand Down Expand Up @@ -498,7 +542,7 @@
<li>This is a list item at root level</li>
<li>This is another item at root level</li>
<li>
<ol>
<ol start="9">
<li>This is a nested list item</li>
<li>This is another nested list item</li>
<li>
Expand All @@ -515,9 +559,9 @@
</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
2. This is another nested list item
3. * This is a deeply nested list item
* 9. This is a nested list item
10. This is another nested list item
11. * 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

0 comments on commit 954f574

Please sign in to comment.