Skip to content

Commit

Permalink
Fix rendering diff between line endings
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih committed Jun 29, 2021
1 parent 64875c3 commit 54d85eb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Markdig.Tests/RoundtripSpecs/TestParagraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public void Test(string value)
}


//[TestCase("\n")]
//[TestCase("\r\n")]
//[TestCase("\r")]
[TestCase("\n")]
[TestCase("\r\n")]
[TestCase("\r")]

[TestCase("p\n")]
[TestCase("p\r")]
Expand Down
17 changes: 17 additions & 0 deletions src/Markdig.Tests/TestNewLine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using NUnit.Framework;

namespace Markdig.Tests
{
[TestFixture]
public class TestNewLine
{
[TestCase("a \nb", "<p>a<br />\nb</p>\n")]
[TestCase("a\\\nb", "<p>a<br />\nb</p>\n")]
[TestCase("a `b\nc`", "<p>a <code>b c</code></p>\n")]
public void Test(string value, string expectedHtml)
{
Assert.AreEqual(expectedHtml, Markdown.ToHtml(value));
Assert.AreEqual(expectedHtml, Markdown.ToHtml(value.Replace("\n", "\r\n")));
}
}
}
8 changes: 7 additions & 1 deletion src/Markdig/Parsers/Inlines/CodeInlineParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Markdig.Helpers;
Expand Down Expand Up @@ -63,6 +63,12 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
{
c = ' ';
}
else if (c == '\r')
{
slice.SkipChar();
c = slice.CurrentChar;
continue;
}

if (c == match)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Markdig/Parsers/Inlines/EscapeInlineParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// This file is licensed under the BSD-Clause 2 license.
// This file is licensed under the BSD-Clause 2 license.
// See the license.txt file in the project root for more information.

using Markdig.Helpers;
Expand Down Expand Up @@ -66,7 +66,7 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
}
else
{
if (c == '\n')
if (c == '\n' || c == '\r')
{
processor.Inline = new LineBreakInline()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Markdig/Parsers/Inlines/LiteralInlineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public override bool Match(InlineProcessor processor, ref StringSlice slice)
length = nextStart - slice.Start;
if (!processor.TrackTrivia)
{
if (text[nextStart] == '\n')
var nextText = text[nextStart];
if (nextText == '\n' || nextText == '\r')
{
int end = nextStart - 1;
while (length > 0 && text[end].IsSpace())
Expand Down

0 comments on commit 54d85eb

Please sign in to comment.