From 201bfce495e6e57be6f2c91e822e65f33a546040 Mon Sep 17 00:00:00 2001 From: Taha Ahmed Date: Tue, 24 Nov 2026 03:42:42 +0200 Subject: [PATCH] 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 --- src/Lexer.ts | 3 + src/Tokenizer.ts | 11 ++- src/helpers.ts | 93 +++++++++++++++++++ src/rules.ts | 3 + .../specs/new/blockquote_tab_after_space.html | 12 +++ test/specs/new/blockquote_tab_after_space.md | 5 + .../new/blockquote_tab_list_item_fence.html | 7 ++ .../new/blockquote_tab_list_item_fence.md | 6 ++ .../new/blockquote_tab_list_item_lazy.html | 7 ++ .../new/blockquote_tab_list_item_lazy.md | 3 + 10 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 test/specs/new/blockquote_tab_after_space.html create mode 100644 test/specs/new/blockquote_tab_after_space.md create mode 100644 test/specs/new/blockquote_tab_list_item_fence.html create mode 100644 test/specs/new/blockquote_tab_list_item_fence.md create mode 100644 test/specs/new/blockquote_tab_list_item_lazy.html create mode 100644 test/specs/new/blockquote_tab_list_item_lazy.md diff --git a/src/Lexer.ts b/src/Lexer.ts index 07c55eff..c71d51df 100644 --- a/src/Lexer.ts +++ b/src/Lexer.ts @@ -23,6 +23,8 @@ export class _Lexer { */ linkParenPossible: 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 */ lastToken?: Token; }; @@ -47,6 +49,7 @@ export class _Lexer { linkEmitted: false, linkParenPossible: true, top: true, + inBlockquote: false, }; const rules = { diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index f868f442..f9d134eb 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -4,6 +4,7 @@ import { splitCells, findClosingBracket, expandTabs, + expandBlockquoteTabs, normalizeLabel, decodeNumericCharacterReferences, trimTrailingBlankLines, @@ -264,7 +265,10 @@ export class _Tokenizer { lines = lines.slice(i); 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 // 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. @@ -290,9 +294,12 @@ export class _Tokenizer { // parse blockquote lines as top level tokens // merge paragraphs if this is a continuation const top = this.lexer.state.top; + const outerInBlockquote = this.lexer.state.inBlockquote; this.lexer.state.top = true; + this.lexer.state.inBlockquote = true; this.lexer.blockTokens(currentText, tokens, true); this.lexer.state.top = top; + this.lexer.state.inBlockquote = outerInBlockquote; // if there is no continuation then we are done if (lines.length === 0) { @@ -313,7 +320,9 @@ export class _Tokenizer { // lazy line is re-parsed as a spurious deeper blockquote. const continuation = lines.join('\n'); const newText = oldToken.raw + '\n' + continuation.replace(this.rules.other.blockquoteSetextReplace2, ''); + this.lexer.state.inBlockquote = true; const newToken = this.blockquote(newText)!; + this.lexer.state.inBlockquote = outerInBlockquote; tokens[tokens.length - 1] = newToken; // Only include continuation lines the nested blockquote actually diff --git a/src/helpers.ts b/src/helpers.ts index 68105ca8..41e26c4d 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -326,3 +326,96 @@ export function expandTabs(line: string, indent = 0) { 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; +} diff --git a/src/rules.ts b/src/rules.ts index aeb1b9e7..41379da6 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -68,6 +68,9 @@ export const other = { blockquoteSetextReplace: /\n {0,3}((?:=+|-+)[ \t]*)(?=\n|$)/g, blockquoteSetextReplace2: /^ {0,3}>[ \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}[.)])(?: |$)|> ?)/, listReplaceNesting: /^ {1,4}(?=( {4})*[^ ])/g, listIsTask: /^\[[ xX]\][ \t]+\S/, diff --git a/test/specs/new/blockquote_tab_after_space.html b/test/specs/new/blockquote_tab_after_space.html new file mode 100644 index 00000000..3c1d76e9 --- /dev/null +++ b/test/specs/new/blockquote_tab_after_space.html @@ -0,0 +1,12 @@ +
+

foo

+
+
+

heading

+
+
+
+
code
+
+
+
diff --git a/test/specs/new/blockquote_tab_after_space.md b/test/specs/new/blockquote_tab_after_space.md new file mode 100644 index 00000000..3965a726 --- /dev/null +++ b/test/specs/new/blockquote_tab_after_space.md @@ -0,0 +1,5 @@ +> foo + +> # heading + +> > code diff --git a/test/specs/new/blockquote_tab_list_item_fence.html b/test/specs/new/blockquote_tab_list_item_fence.html new file mode 100644 index 00000000..9781c73e --- /dev/null +++ b/test/specs/new/blockquote_tab_list_item_fence.html @@ -0,0 +1,7 @@ +
+
    +
  • >	foo
    +
    +
  • +
+
diff --git a/test/specs/new/blockquote_tab_list_item_fence.md b/test/specs/new/blockquote_tab_list_item_fence.md new file mode 100644 index 00000000..f514f352 --- /dev/null +++ b/test/specs/new/blockquote_tab_list_item_fence.md @@ -0,0 +1,6 @@ +--- +renderExact: true +--- +> - ``` +> > foo +> ``` diff --git a/test/specs/new/blockquote_tab_list_item_lazy.html b/test/specs/new/blockquote_tab_list_item_lazy.html new file mode 100644 index 00000000..3d4870de --- /dev/null +++ b/test/specs/new/blockquote_tab_list_item_lazy.html @@ -0,0 +1,7 @@ +
+
    +
  • a +foo +bar
  • +
+
diff --git a/test/specs/new/blockquote_tab_list_item_lazy.md b/test/specs/new/blockquote_tab_list_item_lazy.md new file mode 100644 index 00000000..e4e82c8b --- /dev/null +++ b/test/specs/new/blockquote_tab_list_item_lazy.md @@ -0,0 +1,3 @@ +> - a +> foo +bar