From e4ab77f6e46d478cfed8697c308dddc60b9ddf99 Mon Sep 17 00:00:00 2001 From: vidigoat Date: Tue, 10 Nov 2026 04:09:58 +0530 Subject: [PATCH] feat: add lastToken to lexer state (#4160) * fix: only let a non-empty list starting with 1 interrupt a paragraph in a list item Inside a list item, paragraph lines are lexed as text tokens, and the list tokenizer ran before the text tokenizer without checking whether the list may interrupt the paragraph, so `14.` or an empty `*` on the next line started a nested list. Apply the same rule as the paragraph interrupt: only a non-empty list starting with a bullet or 1 can interrupt a paragraph. Co-Authored-By: Claude Opus 5.5 * refactor: move the list interrupt check into the list tokenizer The lexer passes its tokens to `list(src, tokens)`, and the tokenizer checks the last one, so `list(src)` overrides keep working. Co-Authored-By: Claude Opus 5.5 * refactor: keep the block tokens in the lexer state for the list tokenizer `blockTokens` sets `this.lexer.state.tokens` to the tokens it is lexing and restores the outer ones when it returns, so `list(src)` keeps its signature and checks the token before it from the state. Co-Authored-By: Claude Opus 5.5 * refactor: keep only the last block token in the lexer state `blockTokens` records the last token of the run it is lexing in `this.lexer.state.lastToken` before each tokenizer runs, and restores the outer one when a nested call returns, so the list tokenizer checks `state.lastToken` instead of the whole token list. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- src/Lexer.ts | 7 ++++++ src/Tokenizer.ts | 7 ++++++ src/rules.ts | 1 + .../new/list_item_paragraph_interrupt.html | 24 +++++++++++++++++++ .../new/list_item_paragraph_interrupt.md | 18 ++++++++++++++ 5 files changed, 57 insertions(+) create mode 100644 test/specs/new/list_item_paragraph_interrupt.html create mode 100644 test/specs/new/list_item_paragraph_interrupt.md diff --git a/src/Lexer.ts b/src/Lexer.ts index 3c785bec..834435d3 100644 --- a/src/Lexer.ts +++ b/src/Lexer.ts @@ -23,6 +23,8 @@ export class _Lexer { */ linkParenPossible: boolean; top: boolean; + /** the last block token lexed in the current block run, so a tokenizer can check the token before it */ + lastToken?: Token; }; public inlineQueue: { src: string, tokens: Token[] }[]; @@ -121,6 +123,9 @@ export class _Lexer { blockTokens(src: string, tokens?: TokensList, lastParagraphClipped?: boolean): TokensList; blockTokens(src: string, tokens: Token[] = [], lastParagraphClipped = false) { this.tokenizer.lexer = this; + // list items and block quotes lex their contents with a nested call, so + // restore the outer last token when it returns + const outerLastToken = this.state.lastToken; if (this.options.pedantic) { src = src.replace(other.tabCharGlobal, ' ').replace(other.spaceLine, ''); } @@ -134,6 +139,7 @@ export class _Lexer { break; } + this.state.lastToken = tokens.at(-1); let token: Tokens.Generic | undefined; if (this.options.extensions?.block?.some((extTokenizer) => { @@ -307,6 +313,7 @@ export class _Lexer { } this.state.top = true; + this.state.lastToken = outerLastToken; return tokens; } diff --git a/src/Tokenizer.ts b/src/Tokenizer.ts index e92df643..7e304417 100644 --- a/src/Tokenizer.ts +++ b/src/Tokenizer.ts @@ -327,6 +327,13 @@ export class _Tokenizer { list(src: string): Tokens.List | undefined { let cap = this.rules.block.list.exec(src); if (cap) { + // Inside a list item, paragraph lines are lexed as text tokens, so a list after one would + // interrupt the paragraph. Only a non-empty list starting with a bullet or 1 can do that + if (!this.options.pedantic && this.lexer.state.lastToken?.type === 'text' + && !this.rules.other.listInterruptsParagraph.test(src)) { + return; + } + // src is only consumed from the front, so every raw below is a slice of // listSrc rather than a string built up line by line const listSrc = src; diff --git a/src/rules.ts b/src/rules.ts index 77c160c0..5c799d08 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -42,6 +42,7 @@ try { export const other = { codeRemoveIndent: /^(?: {0,3}\t| {1,4})/gm, + listInterruptsParagraph: /^ {0,3}(?:[*+-]|1[.)])[ \t]+[^ \t\n]/, outputLinkReplace: /\\([\[\]])/g, indentCodeCompensation: /^(\s+)(?:```)/, beginningSpace: /^\s+/, diff --git a/test/specs/new/list_item_paragraph_interrupt.html b/test/specs/new/list_item_paragraph_interrupt.html new file mode 100644 index 00000000..04a8bb8f --- /dev/null +++ b/test/specs/new/list_item_paragraph_interrupt.html @@ -0,0 +1,24 @@ +
    +
  • The number of windows in my house is +14. The number of doors is 6.
  • +
+

text

+
    +
  • foo +*
  • +
+

text

+
    +
  • bar +
      +
    1. baz
    2. +
    +
  • +
+

text

+
    +
  • qux
  • +
+
    +
  1. quux
  2. +
diff --git a/test/specs/new/list_item_paragraph_interrupt.md b/test/specs/new/list_item_paragraph_interrupt.md new file mode 100644 index 00000000..ad34c60c --- /dev/null +++ b/test/specs/new/list_item_paragraph_interrupt.md @@ -0,0 +1,18 @@ +- The number of windows in my house is + 14. The number of doors is 6. + +text + +- foo + * + +text + +- bar + 1. baz + +text + +- qux + +14. quux