fix: keep rejected table candidates in the preceding paragraph (#4218)

* fix: keep rejected table candidates in the preceding paragraph

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>

* test: preserve inline queue and custom paragraph tokenizers

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>

* test: cover rejected GFM table candidates and valid controls

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>

* test: record reference HTML for rejected table continuation

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>

---------

Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com>
This commit is contained in:
generaltony007
2026-11-16 09:34:42 -07:00
committed by GitHub
co-authored by GeneralTony007
parent d2c6f736f3
commit 1c5969e395
4 changed files with 66 additions and 1 deletions
+9 -1
View File
@@ -131,6 +131,9 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
}
let srcLength = Infinity;
let lastParagraph: Token | undefined;
const mergeAdjacentParagraphs = this.options.gfm && !this.options.pedantic
&& this.tokenizer.paragraph === _Tokenizer.prototype.paragraph;
while (src) {
if (src.length < srcLength) {
srcLength = src.length;
@@ -278,13 +281,18 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
}
if (this.state.top && (token = this.tokenizer.paragraph(cutSrc))) {
const lastToken = tokens.at(-1);
if (lastParagraphClipped && lastToken?.type === 'paragraph') {
// A table-shaped header rejected by the table tokenizer is still
// paragraph continuation, not a new block. Keep custom paragraph
// tokenizers' choice to emit separate paragraphs unchanged.
if ((lastParagraphClipped || (lastToken === lastParagraph && mergeAdjacentParagraphs)) && lastToken?.type === 'paragraph') {
lastToken.raw += (lastToken.raw.endsWith('\n') ? '' : '\n') + token.raw;
lastToken.text += '\n' + token.text;
this.inlineQueue.pop();
this.inlineQueue.at(-1)!.src = lastToken.text;
lastParagraph = lastToken;
} else {
tokens.push(token);
lastParagraph = token;
}
lastParagraphClipped = cutSrc.length !== src.length;
src = src.substring(token.raw.length);
@@ -0,0 +1,20 @@
<p>a
b|c
-|-|-</p>
<p>a
|
|---</p>
<blockquote>
<p>a
b|c
-|-|-</p>
</blockquote>
<p>a</p>
<table>
<thead>
<tr>
<th>b</th>
<th>c</th>
</tr>
</thead>
</table>
@@ -0,0 +1,15 @@
a
b|c
-|-|-
a
|
|---
> a
> b|c
> -|-|-
a
b|c
-|-
+22
View File
@@ -22,6 +22,28 @@ describe('marked unit', () => {
});
});
describe('rejected table paragraph continuation', () => {
it('keeps one inline queue entry so emphasis can cross the rejected table header', () => {
assert.strictEqual(marked.parse('*a\nb|c\n-|-|-*'), '<p><em>a\nb|c\n-|-|-</em></p>\n');
});
it('does not merge paragraphs separated by a blank line', () => {
assert.strictEqual(marked.parse('a\n\nb|c\n-|-|-'), '<p>a</p>\n<p>b|c\n-|-|-</p>\n');
});
it('preserves custom paragraph tokenizers that emit separate paragraphs', () => {
marked.use({
tokenizer: {
paragraph(src) {
const raw = src[0];
return { type: 'paragraph', raw, text: raw, tokens: this.lexer.inline(raw) };
},
},
});
assert.strictEqual(marked.parse('abc'), '<p>a</p>\n<p>b</p>\n<p>c</p>\n');
});
});
describe('changeDefaults', () => {
it('should change global defaults', async() => {
const { defaults, setOptions } = await import('../../lib/marked.esm.js');