diff --git a/site/docs/commonmark.md b/site/docs/commonmark.md
index e11cd6c9..f9be91b7 100644
--- a/site/docs/commonmark.md
+++ b/site/docs/commonmark.md
@@ -232,6 +232,11 @@ Raw HTML can be included directly:
```
+HTML blocks that end at a blank line, such as `
` blocks and complete tags
+on their own line, also end at a line containing only spaces or tabs. Markdown
+after that line is parsed normally. Blocks such as `")]
+ [TestCase("")]
+ [TestCase("
\n\u00a0\n*foo*")]
+ [TestCase("\n\u00a0\n*foo*")]
+ public void NonTerminatingHtmlBlockLinesArePreserved(string markdown)
+ {
+ TestParser.TestSpec(markdown, markdown + "\n");
+ TestRoundtrip.RoundTrip(markdown);
+ }
+}
diff --git a/src/Markdig/Parsers/HtmlBlockParser.cs b/src/Markdig/Parsers/HtmlBlockParser.cs
index c21e5978..64db6b6e 100644
--- a/src/Markdig/Parsers/HtmlBlockParser.cs
+++ b/src/Markdig/Parsers/HtmlBlockParser.cs
@@ -181,6 +181,12 @@ public class HtmlBlockParser : BlockParser
private BlockState MatchEnd(BlockProcessor state, HtmlBlock htmlBlock)
{
+ // Keep blank-line indentation consumed so BlockProcessor can preserve it as trivia.
+ if (state.IsBlankLine && (htmlBlock.Type is HtmlBlockType.InterruptingBlock or HtmlBlockType.NonInterruptingBlock))
+ {
+ return BlockState.BreakDiscard;
+ }
+
state.GoToColumn(state.ColumnBeforeIndent);
// Early exit if it is not starting by an HTML tag
@@ -256,18 +262,6 @@ public class HtmlBlockParser : BlockParser
}
}
break;
- case HtmlBlockType.InterruptingBlock:
- if (state.IsBlankLine)
- {
- result = BlockState.BreakDiscard;
- }
- break;
- case HtmlBlockType.NonInterruptingBlock:
- if (state.IsBlankLine)
- {
- result = BlockState.BreakDiscard;
- }
- break;
}
// Update only if we don't have a break discard