Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions md2loop.Core/HtmlToMarkdownConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ private static void ConvertNode(HtmlNode node, StringBuilder sb, int listDepth,
{
sb.Append(text);
}
else if (text.AsSpan().IsWhiteSpace())
{
// Whitespace between block elements is layout, not content. A block
// ends with a newline, so a whitespace-only node there would become
// a stray leading space on the next block. Between inline elements
// the same node is a real word separator and has to be kept.
if (sb.Length > 0 && sb[^1] != '\n')
sb.Append(' ');
}
else
{
sb.Append(EscapeInline(CollapseWhitespaceRegex().Replace(text, " ")));
Expand Down
2 changes: 1 addition & 1 deletion tests/md2loop.Tests/RoundTripTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void ParagraphAfterAList_StaysASeparateBlock()
Assert.Equal("- one\n- two\n\nAfter the list.", RoundTrip("- one\n- two\n\nAfter the list."));
}

[Fact(Skip = "Known bug #33: whitespace between block elements leaks a leading space into the next block.")]
[Fact]
public void WhitespaceBetweenBlocks_DoesNotIndentTheNextBlock()
{
Assert.Equal("one\n\ntwo", HtmlToMarkdownConverter.Convert("<p>one</p>\n<p>two</p>").Replace("\r\n", "\n"));
Expand Down