From f36a2548d19b454cc2a367fd4bf024885832be02 Mon Sep 17 00:00:00 2001 From: Wleeaf Date: Wed, 28 Oct 2026 22:58:30 +0300 Subject: [PATCH] Fix thematic break inside a list item closing the list (#970) A thematic break indented to the content column of the current list item is parsed inside the item instead of closing the list, matching cmark and commonmark.js. --- src/Markdig.Tests/MiscTests.cs | 9 +++++++++ src/Markdig/Parsers/ListBlockParser.cs | 6 ++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Markdig.Tests/MiscTests.cs b/src/Markdig.Tests/MiscTests.cs index 05f65fd2..c68a8ec5 100644 --- a/src/Markdig.Tests/MiscTests.cs +++ b/src/Markdig.Tests/MiscTests.cs @@ -129,6 +129,15 @@ public class MiscTests TestParser.TestSpec(input, expected); } + [Test] + public void ThematicBreakIndentedToListItemContentStaysInTheItem() + { + TestParser.TestSpec("- item one\n\n ---\n\n- item two", ""); + TestParser.TestSpec("1. o\n\n ***", "
    \n
  1. \n

    o

    \n
    \n
  2. \n
"); + TestParser.TestSpec("-\n ***", ""); + TestParser.TestSpec("> - o\n>\n> ---", "
\n\n
"); + } + [Test] public void TestAltTextIsCorrectlyEscaped() { diff --git a/src/Markdig/Parsers/ListBlockParser.cs b/src/Markdig/Parsers/ListBlockParser.cs index 008a135c..83fa8d22 100644 --- a/src/Markdig/Parsers/ListBlockParser.cs +++ b/src/Markdig/Parsers/ListBlockParser.cs @@ -91,10 +91,12 @@ public class ListBlockParser : BlockParser } // When both a thematic break and a list item are possible - // interpretations of a line, the thematic break takes precedence + // interpretations of a line, the thematic break takes precedence. + // A thematic break indented to the content of the current list item belongs to that item. BlockState result; var thematicParser = ThematicBreakParser.Default; - if (processor.LastBlock is not FencedCodeBlock && thematicParser.HasOpeningCharacter(processor.CurrentChar)) + if (processor.LastBlock is not FencedCodeBlock && thematicParser.HasOpeningCharacter(processor.CurrentChar) + && !(block is ListItemBlock currentItem && processor.Indent >= Math.Abs(currentItem.ColumnWidth))) { result = thematicParser.TryOpen(processor); if (result.IsBreak())