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