From 738bd5f6712b1c76cb3ac8d0d9a6ba3951ad7b78 Mon Sep 17 00:00:00 2001 From: mysticmnd Date: Sat, 29 Jun 2024 06:12:31 +0530 Subject: [PATCH] Fix GH-393 regression with mixed new line characters --- ...Tests.Bug393_RegressionWithVaryingNewLines.verified.md | 7 +++++++ src/ReverseMarkdown.Test/ConverterTests.cs | 8 ++++++++ src/ReverseMarkdown.Test/ReverseMarkdown.Test.csproj | 3 +++ src/ReverseMarkdown/StringUtils.cs | 4 ++-- 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 src/ReverseMarkdown.Test/ConverterTests.Bug393_RegressionWithVaryingNewLines.verified.md diff --git a/src/ReverseMarkdown.Test/ConverterTests.Bug393_RegressionWithVaryingNewLines.verified.md b/src/ReverseMarkdown.Test/ConverterTests.Bug393_RegressionWithVaryingNewLines.verified.md new file mode 100644 index 0000000..fa57f5f --- /dev/null +++ b/src/ReverseMarkdown.Test/ConverterTests.Bug393_RegressionWithVaryingNewLines.verified.md @@ -0,0 +1,7 @@ +This is regular text + +This is HTML: + +* Line 1 +* Line 2 +* Line 3 has an unknown tag \ No newline at end of file diff --git a/src/ReverseMarkdown.Test/ConverterTests.cs b/src/ReverseMarkdown.Test/ConverterTests.cs index ba5c600..1099cff 100644 --- a/src/ReverseMarkdown.Test/ConverterTests.cs +++ b/src/ReverseMarkdown.Test/ConverterTests.cs @@ -1376,5 +1376,13 @@ public Task Bug391_AnchorTagUnnecessarilyIndented() return CheckConversion(html, config); } + + [Fact] + public Task Bug393_RegressionWithVaryingNewLines() + { + const string html = "This is regular text\r\n

This is HTML:

"; + var config = new Config { UnknownTags = Config.UnknownTagsOption.Bypass, ListBulletChar = '*' }; + return CheckConversion(html, config); + } } } diff --git a/src/ReverseMarkdown.Test/ReverseMarkdown.Test.csproj b/src/ReverseMarkdown.Test/ReverseMarkdown.Test.csproj index 74df7f3..7270403 100644 --- a/src/ReverseMarkdown.Test/ReverseMarkdown.Test.csproj +++ b/src/ReverseMarkdown.Test/ReverseMarkdown.Test.csproj @@ -50,6 +50,9 @@ ConverterTests.WhenThereIsSingleAsteriskInText_ThenConvertToMarkdownEscapedAsterisk.verified.md + + + ConverterTests.WhenThereIsPreTag_ThenConvertToMarkdownPre.verified.md diff --git a/src/ReverseMarkdown/StringUtils.cs b/src/ReverseMarkdown/StringUtils.cs index cc54a24..9f434b1 100644 --- a/src/ReverseMarkdown/StringUtils.cs +++ b/src/ReverseMarkdown/StringUtils.cs @@ -123,8 +123,8 @@ public static string EmphasizeContentWhitespaceGuard(this string content, string public static string FixMultipleNewlines(this string markdown) { - var normalizedMarkdown = Regex.Replace(markdown, @"\r\n|\r|\n", "\n"); - var pattern = @"\n{2,}"; + var normalizedMarkdown = Regex.Replace(markdown, @"\r\n|\r|\n", Environment.NewLine); + var pattern = $"{Environment.NewLine}{{2,}}"; return Regex.Replace(normalizedMarkdown, pattern, Environment.NewLine + Environment.NewLine); }