Skip to content

Commit 346b162

Browse files
fix: Fix every third list item broken (#2318)
* Fix #2314
1 parent e709bd7 commit 346b162

File tree

3 files changed

+76
-32
lines changed

3 files changed

+76
-32
lines changed

src/Tokenizer.js

+35-32
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export class Tokenizer {
169169
let cap = this.rules.block.list.exec(src);
170170
if (cap) {
171171
let raw, istask, ischecked, indent, i, blankLine, endsWithBlankLine,
172-
line, nextLine, rawLine, itemContents;
172+
line, nextLine, rawLine, itemContents, endEarly;
173173

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

195195
// Check if current bullet point can start a new List Item
196196
while (src) {
197+
endEarly = false;
197198
if (!(cap = itemRegex.exec(src))) {
198199
break;
199200
}
@@ -223,40 +224,42 @@ export class Tokenizer {
223224
if (!line && /^ *$/.test(nextLine)) { // Items begin with at most one blank line
224225
raw += nextLine + '\n';
225226
src = src.substring(nextLine.length + 1);
226-
list.loose = true;
227+
endEarly = true;
227228
}
228229

229-
const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])`);
230-
231-
// Check if following lines should be included in List Item
232-
while (src && !list.loose) {
233-
rawLine = src.split('\n', 1)[0];
234-
line = rawLine;
235-
236-
// Re-align to follow commonmark nesting rules
237-
if (this.options.pedantic) {
238-
line = line.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
239-
}
240-
241-
// End list item if found start of new bullet
242-
if (nextBulletRegex.test(line)) {
243-
break;
230+
if (!endEarly) {
231+
const nextBulletRegex = new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])`);
232+
233+
// Check if following lines should be included in List Item
234+
while (src) {
235+
rawLine = src.split('\n', 1)[0];
236+
line = rawLine;
237+
238+
// Re-align to follow commonmark nesting rules
239+
if (this.options.pedantic) {
240+
line = line.replace(/^ {1,4}(?=( {4})*[^ ])/g, ' ');
241+
}
242+
243+
// End list item if found start of new bullet
244+
if (nextBulletRegex.test(line)) {
245+
break;
246+
}
247+
248+
if (line.search(/[^ ]/) >= indent || !line.trim()) { // Dedent if possible
249+
itemContents += '\n' + line.slice(indent);
250+
} else if (!blankLine) { // Until blank line, item doesn't need indentation
251+
itemContents += '\n' + line;
252+
} else { // Otherwise, improper indentation ends this item
253+
break;
254+
}
255+
256+
if (!blankLine && !line.trim()) { // Check if current line is blank
257+
blankLine = true;
258+
}
259+
260+
raw += rawLine + '\n';
261+
src = src.substring(rawLine.length + 1);
244262
}
245-
246-
if (line.search(/[^ ]/) >= indent || !line.trim()) { // Dedent if possible
247-
itemContents += '\n' + line.slice(indent);
248-
} else if (!blankLine) { // Until blank line, item doesn't need indentation
249-
itemContents += '\n' + line;
250-
} else { // Otherwise, improper indentation ends this item
251-
break;
252-
}
253-
254-
if (!blankLine && !line.trim()) { // Check if current line is blank
255-
blankLine = true;
256-
}
257-
258-
raw += rawLine + '\n';
259-
src = src.substring(rawLine.length + 1);
260263
}
261264

262265
if (!list.loose) {
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<ol>
2+
<li><p>list item one</p>
3+
<ol>
4+
<li>sublist item one</li>
5+
<li>sublist item two</li>
6+
</ol>
7+
</li>
8+
<li><p>list item two</p>
9+
<ol>
10+
<li>sublist item one</li>
11+
<li>sublist item two</li>
12+
</ol>
13+
</li>
14+
<li><p>list item three</p>
15+
<ol>
16+
<li>sublist item one</li>
17+
<li>sublist item two</li>
18+
</ol>
19+
</li>
20+
<li><p>list item four</p>
21+
<ol>
22+
<li>sublist item one</li>
23+
<li>sublist item two</li>
24+
</ol>
25+
</li>
26+
</ol>

test/specs/new/multiple_sub_lists.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
1. list item one
2+
1. sublist item one
3+
2. sublist item two
4+
5+
2. list item two
6+
1. sublist item one
7+
2. sublist item two
8+
9+
3. list item three
10+
1. sublist item one
11+
2. sublist item two
12+
13+
4. list item four
14+
1. sublist item one
15+
2. sublist item two

0 commit comments

Comments
 (0)