Skip to content

Commit

Permalink
fix: fix paragraph continuation after block element (#2686)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech authored Dec 23, 2022
1 parent 26e2942 commit 1bbda68
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/Tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,25 +233,25 @@ export class Tokenizer {
// Check if following lines should be included in List Item
while (src) {
rawLine = src.split('\n', 1)[0];
line = rawLine;
nextLine = rawLine;

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

// End list item if found code fences
if (fencesBeginRegex.test(line)) {
if (fencesBeginRegex.test(nextLine)) {
break;
}

// End list item if found start of new heading
if (headingBeginRegex.test(line)) {
if (headingBeginRegex.test(nextLine)) {
break;
}

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

Expand All @@ -260,20 +260,38 @@ export class Tokenizer {
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 (nextLine.search(/[^ ]/) >= indent || !nextLine.trim()) { // Dedent if possible
itemContents += '\n' + nextLine.slice(indent);
} else {
// not enough indentation
if (blankLine) {
break;
}

// paragraph continuation unless last line was a different block level element
if (line.search(/[^ ]/) >= 4) { // indented code block
break;
}
if (fencesBeginRegex.test(line)) {
break;
}
if (headingBeginRegex.test(line)) {
break;
}
if (hrRegex.test(line)) {
break;
}

itemContents += '\n' + nextLine;
}

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

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

Expand Down
24 changes: 24 additions & 0 deletions test/specs/new/paragraph-after-list-item.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<ul>
<li>
<hr />
</li>
</ul>
<p>paragraph</p>
<ul>
<li>
<h1 id="heading">heading</h1>
</li>
</ul>
<p>paragraph</p>
<ul>
<li>
<pre><code>indented code</code></pre>
</li>
</ul>
<p>paragraph</p>
<ul>
<li>
<pre><code>fenced code</code></pre>
</li>
</ul>
<p>paragraph</p>
10 changes: 10 additions & 0 deletions test/specs/new/paragraph-after-list-item.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- ***
paragraph
- # heading
paragraph
- indented code
paragraph
- ```
fenced code
```
paragraph

1 comment on commit 1bbda68

@vercel
Copy link

@vercel vercel bot commented on 1bbda68 Dec 23, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.