mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> * 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 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
219492a64e
commit
e4ab77f6e4
@@ -23,6 +23,8 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
|
||||
*/
|
||||
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<ParserOutput = string, RendererOutput = string> {
|
||||
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<ParserOutput = string, RendererOutput = string> {
|
||||
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<ParserOutput = string, RendererOutput = string> {
|
||||
}
|
||||
|
||||
this.state.top = true;
|
||||
this.state.lastToken = outerLastToken;
|
||||
return tokens;
|
||||
}
|
||||
|
||||
|
||||
@@ -327,6 +327,13 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
|
||||
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;
|
||||
|
||||
@@ -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+/,
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<ul>
|
||||
<li>The number of windows in my house is
|
||||
14. The number of doors is 6.</li>
|
||||
</ul>
|
||||
<p>text</p>
|
||||
<ul>
|
||||
<li>foo
|
||||
*</li>
|
||||
</ul>
|
||||
<p>text</p>
|
||||
<ul>
|
||||
<li>bar
|
||||
<ol>
|
||||
<li>baz</li>
|
||||
</ol>
|
||||
</li>
|
||||
</ul>
|
||||
<p>text</p>
|
||||
<ul>
|
||||
<li>qux</li>
|
||||
</ul>
|
||||
<ol start="14">
|
||||
<li>quux</li>
|
||||
</ol>
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user