From ccd8840c3f29c2a00c706f59a38e945d7341a47e Mon Sep 17 00:00:00 2001 From: Grayson Hilliard Date: Thu, 18 Jan 2024 18:50:21 -0800 Subject: [PATCH 1/3] adds div newline prefix suspression --- ...SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.md | 4 ++++ ...ewlineFlag_PrefixDiv_Should_Be_Empty.verified.md | 7 +++++++ src/ReverseMarkdown.Test/ConverterTests.cs | 7 +++++++ src/ReverseMarkdown/Config.cs | 2 ++ src/ReverseMarkdown/Converters/Div.cs | 13 ++++++++++++- 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.md create mode 100644 src/ReverseMarkdown.Test/ConverterTests.When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.verified.md 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 00000000..5b90b3d4 --- /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 00000000..67ccadfe --- /dev/null +++ b/src/ReverseMarkdown.Test/ConverterTests.When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.verified.md @@ -0,0 +1,7 @@ +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 c98de34d..2454a558 100644 --- a/src/ReverseMarkdown.Test/ConverterTests.cs +++ b/src/ReverseMarkdown.Test/ConverterTests.cs @@ -1341,5 +1341,12 @@ 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); + } } } diff --git a/src/ReverseMarkdown/Config.cs b/src/ReverseMarkdown/Config.cs index 67437b45..70886f87 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 a782d576..689eaa0e 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)}"; } } } From a8919d6fdfcdc2eeeb90c22e2169594ea29bf55b Mon Sep 17 00:00:00 2001 From: Grayson Hilliard Date: Thu, 18 Jan 2024 18:53:43 -0800 Subject: [PATCH 2/3] adds config option to readme --- README.source.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.source.md b/README.source.md index 3b801ae3..aba9b703 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 From 73d793b203348636530dc475653c71e3e0f2c0e6 Mon Sep 17 00:00:00 2001 From: Grayson Hilliard Date: Thu, 18 Jan 2024 19:02:28 -0800 Subject: [PATCH 3/3] fixes test, uses config in test --- ...SuppressNewlineFlag_PrefixDiv_Should_Be_Empty.verified.md | 3 --- src/ReverseMarkdown.Test/ConverterTests.cs | 5 ++++- 2 files changed, 4 insertions(+), 4 deletions(-) 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 index 67ccadfe..25c59c6b 100644 --- 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 @@ -1,7 +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 2454a558..283d29d0 100644 --- a/src/ReverseMarkdown.Test/ConverterTests.cs +++ b/src/ReverseMarkdown.Test/ConverterTests.cs @@ -1346,7 +1346,10 @@ public Task When_Spaces_In_Inline_Tags_Should_Be_Retained() public Task When_SuppressNewlineFlag_PrefixDiv_Should_Be_Empty() { var html = $"
the
fox
jumps
over
"; - return CheckConversion(html); + return CheckConversion(html, new Config + { + SuppressNewlines = true + }); } } }