feat: add inBlockquote to lexer state (#4222)

* fix: measure tabs after a block quote marker from the start of the line

- Expand tabs in block quote markers and the indentation after them with tab stops from the start of the line, nested markers included
- Keep tabs literal inside fenced code in a block quote (#4184)
- Keep the old handling inside list items, where the marker's column is unknown

Fixes #4221

* fix: keep the existing tab handling from a list item in a block quote on

- Return lines as written from the first list item on, since its columns and fences are not tracked
- Expand columns only in the outermost block quote, including the nested continuation path
- Add exact and structural specs for a fence and a lazy line in a list item under a block quote
This commit is contained in:
Taha Ahmed
2026-11-23 18:42:42 -07:00
committed by GitHub
parent c57230757e
commit 201bfce495
10 changed files with 149 additions and 1 deletions
+3
View File
@@ -23,6 +23,8 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
*/ */
linkParenPossible: boolean; linkParenPossible: boolean;
top: boolean; top: boolean;
/** lexing the content of a block quote, whose nested markers the outermost one has expanded */
inBlockquote: boolean;
/** the last block token lexed in the current block run, so a tokenizer can check the token before it */ /** the last block token lexed in the current block run, so a tokenizer can check the token before it */
lastToken?: Token; lastToken?: Token;
}; };
@@ -47,6 +49,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
linkEmitted: false, linkEmitted: false,
linkParenPossible: true, linkParenPossible: true,
top: true, top: true,
inBlockquote: false,
}; };
const rules = { const rules = {
+10 -1
View File
@@ -4,6 +4,7 @@ import {
splitCells, splitCells,
findClosingBracket, findClosingBracket,
expandTabs, expandTabs,
expandBlockquoteTabs,
normalizeLabel, normalizeLabel,
decodeNumericCharacterReferences, decodeNumericCharacterReferences,
trimTrailingBlankLines, trimTrailingBlankLines,
@@ -264,7 +265,10 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
lines = lines.slice(i); lines = lines.slice(i);
const currentRaw = currentLines.join('\n'); const currentRaw = currentLines.join('\n');
const currentText = currentRaw // expandBlockquoteTabs needs the marker's column, which is unknown inside a list item
// or a block quote, where the outermost block quote has already expanded the markers
const expandColumns = this.lexer.state.top && !this.lexer.state.inBlockquote && currentRaw.includes('\t');
const currentText = (expandColumns ? expandBlockquoteTabs(currentLines) : currentRaw)
// a tab right after the block quote marker runs to the next tab stop, and // a tab right after the block quote marker runs to the next tab stop, and
// the marker only consumes one space of it; the rest is content. // the marker only consumes one space of it; the rest is content.
// After a space the marker consumes it whole, so later tabs stay literal. // After a space the marker consumes it whole, so later tabs stay literal.
@@ -290,9 +294,12 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
// parse blockquote lines as top level tokens // parse blockquote lines as top level tokens
// merge paragraphs if this is a continuation // merge paragraphs if this is a continuation
const top = this.lexer.state.top; const top = this.lexer.state.top;
const outerInBlockquote = this.lexer.state.inBlockquote;
this.lexer.state.top = true; this.lexer.state.top = true;
this.lexer.state.inBlockquote = true;
this.lexer.blockTokens(currentText, tokens, true); this.lexer.blockTokens(currentText, tokens, true);
this.lexer.state.top = top; this.lexer.state.top = top;
this.lexer.state.inBlockquote = outerInBlockquote;
// if there is no continuation then we are done // if there is no continuation then we are done
if (lines.length === 0) { if (lines.length === 0) {
@@ -313,7 +320,9 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
// lazy line is re-parsed as a spurious deeper blockquote. // lazy line is re-parsed as a spurious deeper blockquote.
const continuation = lines.join('\n'); const continuation = lines.join('\n');
const newText = oldToken.raw + '\n' + continuation.replace(this.rules.other.blockquoteSetextReplace2, ''); const newText = oldToken.raw + '\n' + continuation.replace(this.rules.other.blockquoteSetextReplace2, '');
this.lexer.state.inBlockquote = true;
const newToken = this.blockquote(newText)!; const newToken = this.blockquote(newText)!;
this.lexer.state.inBlockquote = outerInBlockquote;
tokens[tokens.length - 1] = newToken; tokens[tokens.length - 1] = newToken;
// Only include continuation lines the nested blockquote actually // Only include continuation lines the nested blockquote actually
+93
View File
@@ -326,3 +326,96 @@ export function expandTabs(line: string, indent = 0) {
return expanded; return expanded;
} }
/**
* Expands the tabs in block quote markers and the indentation after them to spaces, with tab
* stops measured from the start of the line. Inside fenced code the indentation is code content,
* so a tab there stays literal. Lines from the first list item on are returned as is, because
* the item's columns and the fences inside it are not tracked here.
*/
export function expandBlockquoteTabs(lines: string[]) {
let fence: { marker: string, depth: number } | undefined;
let inListItem = false;
return lines.map((line) => {
if (inListItem) {
return line;
}
const { depth, expanded, keptIndent, content } = splitBlockquoteLine(line, fence?.depth ?? Infinity);
if (fence?.depth !== depth) {
fence = undefined;
}
if (fence) {
const close = other.blockquoteFenceClose.exec(content)?.[1];
if (!close || close[0] !== fence.marker[0] || close.length < fence.marker.length) {
return keptIndent;
}
fence = undefined;
return expanded;
}
inListItem = other.blockquoteListItemStart.test(content);
if (inListItem) {
return line;
}
const open = other.blockquoteFenceOpen.exec(content)?.[1];
if (open) {
fence = { marker: open, depth };
}
return expanded;
}).join('\n');
}
/**
* Reads the `>` markers at the start of a line. `expanded` has the tabs in them and in the
* content's first four columns of indentation expanded, `keptIndent` only the tab that the
* optional space after the last marker cuts into, and `content` is what follows the markers.
*/
function splitBlockquoteLine(line: string, maxDepth: number) {
let i = 0;
let col = 0;
let depth = 0;
let contentCol = 0;
let prefix = '';
while (depth < maxDepth) {
let j = i;
let markerCol = col;
while (line[j] === ' ' || line[j] === '\t') {
markerCol += tabWidth(line[j], markerCol);
j++;
}
if (line[j] !== '>' || markerCol - contentCol > 3) {
break;
}
prefix += ' '.repeat(markerCol - col) + '>';
depth++;
i = j + 1;
col = markerCol + 1;
contentCol = line[i] === ' ' || line[i] === '\t' ? col + 1 : col;
}
if (depth === 0) {
return { depth, expanded: line, keptIndent: line, content: line };
}
let indent = '';
let keptIndent = '';
while ((line[i] === ' ' || line[i] === '\t') && col < contentCol + 4) {
const added = tabWidth(line[i], col);
indent += ' '.repeat(added);
keptIndent += col < contentCol && added > 1 ? ' '.repeat(added) : line[i];
col += added;
i++;
}
const rest = line.slice(i);
return {
depth,
expanded: prefix + indent + rest,
keptIndent: prefix + keptIndent + rest,
content: ' '.repeat(Math.max(0, col - contentCol)) + rest,
};
}
function tabWidth(char: string, col: number) {
return char === '\t' ? 4 - (col % 4) : 1;
}
+3
View File
@@ -68,6 +68,9 @@ export const other = {
blockquoteSetextReplace: /\n {0,3}((?:=+|-+)[ \t]*)(?=\n|$)/g, blockquoteSetextReplace: /\n {0,3}((?:=+|-+)[ \t]*)(?=\n|$)/g,
blockquoteSetextReplace2: /^ {0,3}>[ \t]?/gm, blockquoteSetextReplace2: /^ {0,3}>[ \t]?/gm,
blockquoteMarkerWhitespace: /^( {0,3}>)(\t[ \t]*)/gm, blockquoteMarkerWhitespace: /^( {0,3}>)(\t[ \t]*)/gm,
blockquoteFenceOpen: /^ {0,3}(`{3,}(?=[^`]*$)|~{3,})/,
blockquoteFenceClose: /^ {0,3}(`{3,}|~{3,})[ \t]*$/,
blockquoteListItemStart: /^ {0,3}(?:[*+-]|\d{1,9}[.)])(?:[ \t]|$)/,
nestedBlockMarker: /^ {0,3}(?:(?:[*+-]|\d{1,9}[.)])(?: |$)|> ?)/, nestedBlockMarker: /^ {0,3}(?:(?:[*+-]|\d{1,9}[.)])(?: |$)|> ?)/,
listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g, listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g,
listIsTask: /^\[[ xX]\][ \t]+\S/, listIsTask: /^\[[ xX]\][ \t]+\S/,
@@ -0,0 +1,12 @@
<blockquote>
<p>foo</p>
</blockquote>
<blockquote>
<h1>heading</h1>
</blockquote>
<blockquote>
<blockquote>
<pre><code>code
</code></pre>
</blockquote>
</blockquote>
@@ -0,0 +1,5 @@
> foo
> # heading
> > code
@@ -0,0 +1,7 @@
<blockquote>
<ul>
<li><pre><code>&gt; foo
</code></pre>
</li>
</ul>
</blockquote>
@@ -0,0 +1,6 @@
---
renderExact: true
---
> - ```
> > foo
> ```
@@ -0,0 +1,7 @@
<blockquote>
<ul>
<li>a
foo
bar</li>
</ul>
</blockquote>
@@ -0,0 +1,3 @@
> - a
> foo
bar