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: fix paragraph continuation after block element #2686

Merged
merged 1 commit into from
Dec 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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