Skip to content

Commit

Permalink
fix: Fix every third list item broken (#2318)
Browse files Browse the repository at this point in the history
* Fix #2314
  • Loading branch information
calculuschild authored Dec 9, 2021
1 parent e709bd7 commit 346b162
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 32 deletions.
67 changes: 35 additions & 32 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class Tokenizer {
let cap = this.rules.block.list.exec(src);
if (cap) {
let raw, istask, ischecked, indent, i, blankLine, endsWithBlankLine,
line, nextLine, rawLine, itemContents;
line, nextLine, rawLine, itemContents, endEarly;

let bull = cap[1].trim();
const isordered = bull.length > 1;
Expand All @@ -194,6 +194,7 @@ export class Tokenizer {

// Check if current bullet point can start a new List Item
while (src) {
endEarly = false;
if (!(cap = itemRegex.exec(src))) {
break;
}
Expand Down Expand Up @@ -223,40 +224,42 @@ export class Tokenizer {
if (!line && /^ *$/.test(nextLine)) { // Items begin with at most one blank line
raw += nextLine + '\n';
src = src.substring(nextLine.length + 1);
list.loose = true;
endEarly = true;
}

const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])`);

// Check if following lines should be included in List Item
while (src && !list.loose) {
rawLine = src.split('\n', 1)[0];
line = rawLine;

// Re-align to follow commonmark nesting rules
if (this.options.pedantic) {
line = line.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
}

// End list item if found start of new bullet
if (nextBulletRegex.test(line)) {
break;
if (!endEarly) {
const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])`);

// Check if following lines should be included in List Item
while (src) {
rawLine = src.split('\n', 1)[0];
line = rawLine;

// Re-align to follow commonmark nesting rules
if (this.options.pedantic) {
line = line.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
}

// End list item if found start of new bullet
if (nextBulletRegex.test(line)) {
break;
}

if (line.search(/[^ ]/) >= indent || !line.trim()) { // Dedent if possible
itemContents += '\n' + line.slice(indent);
} else if (!blankLine) { // Until blank line, item doesn't need indentation
itemContents += '\n' + line;
} else { // Otherwise, improper indentation ends this item
break;
}

if (!blankLine && !line.trim()) { // Check if current line is blank
blankLine = true;
}

raw += rawLine + '\n';
src = src.substring(rawLine.length + 1);
}

if (line.search(/[^ ]/) >= indent || !line.trim()) { // Dedent if possible
itemContents += '\n' + line.slice(indent);
} else if (!blankLine) { // Until blank line, item doesn't need indentation
itemContents += '\n' + line;
} else { // Otherwise, improper indentation ends this item
break;
}

if (!blankLine && !line.trim()) { // Check if current line is blank
blankLine = true;
}

raw += rawLine + '\n';
src = src.substring(rawLine.length + 1);
}

if (!list.loose) {
Expand Down
26 changes: 26 additions & 0 deletions test/specs/new/multiple_sub_lists.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<ol>
<li><p>list item one</p>
<ol>
<li>sublist item one</li>
<li>sublist item two</li>
</ol>
</li>
<li><p>list item two</p>
<ol>
<li>sublist item one</li>
<li>sublist item two</li>
</ol>
</li>
<li><p>list item three</p>
<ol>
<li>sublist item one</li>
<li>sublist item two</li>
</ol>
</li>
<li><p>list item four</p>
<ol>
<li>sublist item one</li>
<li>sublist item two</li>
</ol>
</li>
</ol>
15 changes: 15 additions & 0 deletions test/specs/new/multiple_sub_lists.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1. list item one
1. sublist item one
2. sublist item two

2. list item two
1. sublist item one
2. sublist item two

3. list item three
1. sublist item one
2. sublist item two

4. list item four
1. sublist item one
2. sublist item two

1 comment on commit 346b162

@vercel
Copy link

@vercel vercel bot commented on 346b162 Dec 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please # to comment.