diff --git a/README.source.md b/README.source.md index 3b801ae..aba9b70 100644 --- a/README.source.md +++ b/README.source.md @@ -59,6 +59,7 @@ var converter = new ReverseMarkdown.Converter(config); * `DefaultCodeBlockLanguage` - Option to set the default code block language for Github style markdown if class based language markers are not available * `GithubFlavored` - Github style markdown for br, pre and table. Default is false +* `SuppressNewlines` - Removes prefixed newlines from `div` tags. Default is false * `ListBulletChar` - Allows you to change the bullet character. Default value is `-`. Some systems expect the bullet character to be `*` rather than `-`, this config allows you to change it. * `RemoveComments` - Remove comment tags with text. Default is false * `SmartHrefHandling` - How to handle `` tag href attribute diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.md b/src/ReverseMarkdown.Test/ConverterTests.When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.md new file mode 100644 index 0000000..5b90b3d --- /dev/null +++ b/src/ReverseMarkdown.Test/ConverterTests.When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.md @@ -0,0 +1,4 @@ +the +fox +jumps +over \ No newline at end of file diff --git a/src/ReverseMarkdown.Test/ConverterTests.When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.verified.md b/src/ReverseMarkdown.Test/ConverterTests.When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.verified.md new file mode 100644 index 0000000..25c59c6 --- /dev/null +++ b/src/ReverseMarkdown.Test/ConverterTests.When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.verified.md @@ -0,0 +1,4 @@ +the +fox +jumps +over \ No newline at end of file diff --git a/src/ReverseMarkdown.Test/ConverterTests.cs b/src/ReverseMarkdown.Test/ConverterTests.cs index c98de34..283d29d 100644 --- a/src/ReverseMarkdown.Test/ConverterTests.cs +++ b/src/ReverseMarkdown.Test/ConverterTests.cs @@ -1341,5 +1341,15 @@ public Task When_Spaces_In_Inline_Tags_Should_Be_Retained() var html = $"... example html code block"; return CheckConversion(html); } + + [Fact] + public Task When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty() + { + var html = $"
the
fox
jumps
over
"; + return CheckConversion(html, new Config + { + SuppressNewlines = true + }); + } } } diff --git a/src/ReverseMarkdown/Config.cs b/src/ReverseMarkdown/Config.cs index 67437b4..70886f8 100644 --- a/src/ReverseMarkdown/Config.cs +++ b/src/ReverseMarkdown/Config.cs @@ -8,6 +8,8 @@ public class Config public UnknownTagsOption UnknownTags { get; set; } = UnknownTagsOption.PassThrough; public bool GithubFlavored { get; set; } = false; + + public bool SuppressNewlines { get; set; } = false; public bool RemoveComments { get; set; } = false; diff --git a/src/ReverseMarkdown/Converters/Div.cs b/src/ReverseMarkdown/Converters/Div.cs index a782d57..689eaa0 100644 --- a/src/ReverseMarkdown/Converters/Div.cs +++ b/src/ReverseMarkdown/Converters/Div.cs @@ -42,7 +42,18 @@ public override string Convert(HtmlNode node) return content; } - return $"{(Td.FirstNodeWithinCell(node) ? "" : Environment.NewLine)}{content}{(Td.LastNodeWithinCell(node) ? "" : Environment.NewLine)}"; + var prefix = Environment.NewLine; + + if (Td.FirstNodeWithinCell(node)) + { + prefix = string.Empty; + } + else if (Converter.Config.SuppressNewlines) + { + prefix = string.Empty; + } + + return $"{prefix}{content}{(Td.LastNodeWithinCell(node) ? "" : Environment.NewLine)}"; } } }