Skip to content

Commit

Permalink
fix: Fix serialization of blank lines in code blocks
Browse files Browse the repository at this point in the history
Blank lines in the content of MdCodeBlock block were being omitted when saving Markdown.

See-Also: ap0llo/mddocs#245
Pull-Request: #201
  • Loading branch information
ap0llo authored Feb 25, 2023
1 parent 7f52aa8 commit 2b9ffed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,36 @@ public void Code_blocks_are_serialized_as_expected_02() =>
new MdDocument(new MdCodeBlock("Line1\r\nLine2", "xml"))
);

[Fact]
public void Code_blocks_are_serialized_as_expected_03() =>
AssertToStringEquals(
"""
```lang
Line 1

Line 2


Line 3

```

""",
new MdDocument(new MdCodeBlock(
"""
Line 1

Line 2


Line 3

""",
"lang"
))
);


[Fact]
public void Tables_are_serialized_as_expected_01() =>
AssertToStringEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Grynwald.MarkdownGenerator.Internal
{
internal partial class DocumentSerializer : IBlockVisitor
{
private static readonly char[] s_LineBreakChars = "\r\n".ToCharArray();
private static readonly string[] s_LineBreaks = { "\n", "\r\n" };

private readonly PrefixTextWriter m_Writer;
private readonly MdSerializationOptions m_Options;
Expand Down Expand Up @@ -128,7 +128,7 @@ public void Visit(MdParagraph paragraph)
{
m_Writer.RequestBlankLine();

var lines = paragraph.Text.ToString(m_Options).Split(s_LineBreakChars, StringSplitOptions.RemoveEmptyEntries);
var lines = paragraph.Text.ToString(m_Options).Split(s_LineBreaks, StringSplitOptions.RemoveEmptyEntries);

// skip paragraph if it is empty
if (lines.Length == 0)
Expand Down Expand Up @@ -278,7 +278,7 @@ public void Visit(MdCodeBlock codeBlock)

m_Writer.WriteLine($"{codeFence}{codeBlock.InfoString ?? ""}");

var lines = codeBlock.Text.Split(s_LineBreakChars, StringSplitOptions.RemoveEmptyEntries);
var lines = codeBlock.Text.Split(s_LineBreaks, StringSplitOptions.None);
foreach (var line in lines)
{
m_Writer.WriteLine(line);
Expand Down

0 comments on commit 2b9ffed

Please sign in to comment.