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 every third list item broken #2318

Merged
merged 2 commits into from Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 35 additions & 32 deletions src/Tokenizer.js
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
@@ -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
@@ -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