Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rendering diff between line endings #560

Merged
merged 1 commit into from
Jul 6, 2021
Merged
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
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