fix: text after a heading or code block in a nested list item is pulled into the outer item (#4179)

fix: end a list item before a lazy line after a nested block

A line with less indentation than a list item is only a lazy
continuation when it continues a paragraph. The list tokenizer checked
the previous line for a fence, heading or hr, but it used the regexes
scaled to the item indent on a line that is already relative to the
item content, and it did not look inside a nested list item or
blockquote on that line. So text after `- - # heading`, `- > ***`, or a
fenced code block in a nested item was pulled into the outer item.
This commit is contained in:
yash
2026-11-08 11:09:29 -07:00
committed by GitHub
parent ed54c541ef
commit 4cb767a966
4 changed files with 81 additions and 0 deletions
+14
View File
@@ -403,6 +403,9 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
const htmlBeginRegex = this.rules.other.htmlBeginRegex(indent);
const blockquoteBeginRegex = this.rules.other.blockquoteBeginRegex(indent);
// A lazy line continues a paragraph, so it is not checked for the start of a block
let lineIsLazy = false;
// Check if following lines should be included in List Item
while (src) {
const rawLine = src.split('\n', 1)[0];
@@ -449,6 +452,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
if (nextLineWithoutTabs.search(this.rules.other.nonSpaceChar) >= indent || !nextLine.trim()) { // Dedent if possible
itemContents += '\n' + nextLineWithoutTabs.slice(indent);
lineIsLazy = false;
} else {
// not enough indentation
if (blankLine) {
@@ -471,6 +475,15 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
if (hrRegex.test(line)) {
break;
}
if (!this.options.pedantic && !lineIsLazy) {
// `line` is relative to the item content, so a block on it can be indented up to 3 spaces whatever
// the item indent is. A nested list item or blockquote on it, as in `- # heading` or `> ---`, holds
// a block that a lazy line cannot continue either, so check what follows those markers too.
const lastBlockLine = line.replace(this.rules.other.nestedContainerMarkers, '');
if (this.rules.block.fences.test(lastBlockLine) || this.rules.block.heading.test(lastBlockLine) || this.rules.block.hr.test(lastBlockLine)) {
break;
}
}
// A lazy continuation can look like a setext underline after
// list indentation is removed. Keep it in the paragraph.
@@ -481,6 +494,7 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
? ` ${nextLineWithoutTabs.replace(/^ {0,3}/, '')}`
: nextLine;
itemContents += '\n' + continuation;
lineIsLazy = true;
}
blankLine = !nextLine.trim();
+2
View File
@@ -110,6 +110,8 @@ export const other = {
// html start conditions in the paragraph rule below; type 7 is excluded there
htmlBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}(?:</?(?:${_tag})(?: +|$|/?>)|<(?:(?:script|pre|style|textarea)(?:[\\s>]|$)|!--)|<\\?|<![A-Za-z]|<!\\[CDATA\\[)`, 'i')),
blockquoteBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}>`)),
// list item and blockquote markers that open nested blocks at the start of a line, as in `- > # heading`
nestedContainerMarkers: /^(?: {0,3}(?:(?:[*+-]|\d{1,9}[.)])[ \t]+|>[ \t]?))+/,
};
/**
@@ -0,0 +1,45 @@
<ul>
<li>Setup
<ul>
<li>Install it:
<pre><code>npm install
</code></pre>
</li>
</ul>
</li>
</ul>
<p>Then run it.</p>
<ul>
<li>Notes
<blockquote>
<h2>Warning</h2>
</blockquote>
</li>
</ul>
<p>Not in the list.</p>
<ul>
<li>
<ul>
<li>
<h1>heading</h1>
</li>
</ul>
</li>
</ul>
<p>Not in the list.</p>
<ul>
<li>
<blockquote>
<hr />
</blockquote>
</li>
</ul>
<p>Not in the list.</p>
<ul>
<li>item
<ul>
<li>nested
lazy line in the nested item</li>
</ul>
</li>
</ul>
@@ -0,0 +1,20 @@
- Setup
- Install it:
```
npm install
```
Then run it.
- Notes
> ## Warning
Not in the list.
- - # heading
Not in the list.
- > ***
Not in the list.
- item
- nested
lazy line in the nested item