Skip to content

Commit

Permalink
Add a final step to clean up multiple lines of new lines into single …
Browse files Browse the repository at this point in the history
…line
  • Loading branch information
mysticmind committed Jun 18, 2024
1 parent b7dcfe3 commit a6b7027
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ If you have used and benefitted from this library. Please feel free to buy me a
Install the package from NuGet using `Install-Package ReverseMarkdown` or clone the repository and built it yourself.

<!-- snippet: Usage -->
<a id='snippet-usage'></a>
<a id='snippet-Usage'></a>
```cs
var converter = new ReverseMarkdown.Converter();

string html = "This a sample <strong>paragraph</strong> from <a href=\"http://test.com\">my site</a>";

string result = converter.Convert(html);
```
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L11-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-usage' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L11-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-Usage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in:
Expand All @@ -43,7 +43,7 @@ This a sample **paragraph** from [my site](http://test.com)
The conversion can be customized:

<!-- snippet: UsageWithConfig -->
<a id='snippet-usagewithconfig'></a>
<a id='snippet-UsageWithConfig'></a>
```cs
var config = new ReverseMarkdown.Config
{
Expand All @@ -59,7 +59,7 @@ var config = new ReverseMarkdown.Config

var converter = new ReverseMarkdown.Converter(config);
```
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L27-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-usagewithconfig' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L27-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-UsageWithConfig' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Configuration options
Expand Down
8 changes: 4 additions & 4 deletions README.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ If you have used and benefitted from this library. Please feel free to buy me a
Install the package from NuGet using `Install-Package ReverseMarkdown` or clone the repository and build it yourself.

<!-- snippet: Usage -->
<a id='snippet-usage'></a>
<a id='snippet-Usage'></a>
```cs
var converter = new ReverseMarkdown.Converter();

string html = "This a sample <strong>paragraph</strong> from <a href=\"http://test.com\">my site</a>";

string result = converter.Convert(html);
```
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L11-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-usage' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L11-L19' title='Snippet source file'>snippet source</a> | <a href='#snippet-Usage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Will result in:
Expand All @@ -36,7 +36,7 @@ This a sample **paragraph** from [my site](http://test.com)
The conversion can also be customized:

<!-- snippet: UsageWithConfig -->
<a id='snippet-usagewithconfig'></a>
<a id='snippet-UsageWithConfig'></a>
```cs
var config = new ReverseMarkdown.Config
{
Expand All @@ -52,7 +52,7 @@ var config = new ReverseMarkdown.Config

var converter = new ReverseMarkdown.Converter(config);
```
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L27-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-usagewithconfig' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L27-L43' title='Snippet source file'>snippet source</a> | <a href='#snippet-UsageWithConfig' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Configuration options
Expand Down
2 changes: 1 addition & 1 deletion src/ReverseMarkdown/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public string Convert(string html)
// cleanup multiple new lines
result = Regex.Replace( result, @"(^\p{Zs}*(\r\n|\n)){2,}", Environment.NewLine, RegexOptions.Multiline);

return result.Trim();
return result.Trim().FixMultipleNewlines();
}

public void Register(string tagName, IConverter converter)
Expand Down
7 changes: 7 additions & 0 deletions src/ReverseMarkdown/StringUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ public static string EmphasizeContentWhitespaceGuard(this string content, string

return $"{leadingSpaces}{emphasis}{content.Chomp(all:true)}{emphasis}{(trailingSpaces.Length > 0 ? trailingSpaces : nextSiblingSpaceSuffix)}";
}

public static string FixMultipleNewlines(this string markdown)
{
var normalizedMarkdown = Regex.Replace(markdown, @"\r\n|\r|\n", "\n");
var pattern = @"\n{2,}";
return Regex.Replace(normalizedMarkdown, pattern, Environment.NewLine + Environment.NewLine);
}

private static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> enumerable, Func<T, TKey> keySelector)
{
Expand Down

0 comments on commit a6b7027

Please sign in to comment.