From 8c30f9896c806be6290dc404d2c890b37468cd8c Mon Sep 17 00:00:00 2001 From: mysticmnd Date: Tue, 24 Feb 2026 17:56:32 +0530 Subject: [PATCH] Add option to convert
 content as HTML

---
 README.md                                  | 12 ++++++++
 src/ReverseMarkdown.Test/ConverterTests.cs | 34 ++++++++++++++++++++++
 src/ReverseMarkdown/Config.cs              |  6 ++++
 src/ReverseMarkdown/Converters/Pre.cs      | 19 ++++++++++++
 4 files changed, 71 insertions(+)

diff --git a/README.md b/README.md
index 16e5c7c..4550928 100644
--- a/README.md
+++ b/README.md
@@ -85,6 +85,17 @@ var converter = new ReverseMarkdown.Converter(config);
 snippet source | anchor
 
 
+To treat `
` (and `
`) content as normal HTML instead of code blocks:
+
+```cs
+var config = new ReverseMarkdown.Config
+{
+    ConvertPreContentAsHtml = true
+};
+
+var converter = new ReverseMarkdown.Converter(config);
+```
+
 If you need to preserve markdown-like text as literal content (for example `# Heading` or `- Item`), either enable `EscapeMarkdownLineStarts` or use `CommonMark`:
 
 ```cs
@@ -111,6 +122,7 @@ var converter = new ReverseMarkdown.Converter(config);
 * `OutputLineEnding` - Output line endings used in generated markdown. Default is `Environment.NewLine`
 * `CleanupUnnecessarySpaces` - Cleanup unnecessary spaces in the output. Default is true
 * `SuppressDivNewlines` - Removes prefixed newlines from `div` tags. Default is false
+* `ConvertPreContentAsHtml` - Treat `
` (and `
`) content as normal HTML instead of a code block. 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. Note: This option is ignored when `SlackFlavored` is enabled
 * `RemoveComments` - Remove comment tags with text. Default is false
 * `SmartHrefHandling` - How to handle `` tag href attribute
diff --git a/src/ReverseMarkdown.Test/ConverterTests.cs b/src/ReverseMarkdown.Test/ConverterTests.cs
index 34f2887..50a2ea3 100644
--- a/src/ReverseMarkdown.Test/ConverterTests.cs
+++ b/src/ReverseMarkdown.Test/ConverterTests.cs
@@ -579,6 +579,40 @@ public async Task Converter_Is_Thread_Safe_For_Concurrent_Use()
             }
         }
 
+        [Fact]
+        public void WhenPreContainsTable_ThenTreatAsCodeBlock()
+        {
+            var htmlTable = "
a
"; + var htmlPre = $"
{htmlTable}
"; + var htmlPreCode = $"
{htmlTable}
"; + var converter = new Converter(new Config { GithubFlavored = true }); + var expected = string.Join(Environment.NewLine, new[] { + "```", + "a", + "```" + }); + + Assert.Equal(expected, converter.Convert(htmlPre)); + Assert.Equal(expected, converter.Convert(htmlPreCode)); + } + + [Fact] + public void WhenPreContainsHtml_WithConvertPreContentAsHtml_ThenConvertHtml() + { + var htmlContent = "

Title

Bold

"; + var htmlPre = $"
{htmlContent}
"; + var htmlPreCode = $"
{htmlContent}
"; + var converter = new Converter(new Config { ConvertPreContentAsHtml = true }); + var expected = string.Join(Environment.NewLine, new[] { + "Title", + string.Empty, + "**Bold**" + }); + + Assert.Equal(expected, converter.Convert(htmlPre)); + Assert.Equal(expected, converter.Convert(htmlPreCode)); + } + [Fact] public void SlackFlavored_Unsupported_Hr() { diff --git a/src/ReverseMarkdown/Config.cs b/src/ReverseMarkdown/Config.cs index fce398f..5e6e229 100644 --- a/src/ReverseMarkdown/Config.cs +++ b/src/ReverseMarkdown/Config.cs @@ -41,6 +41,12 @@ public class Config public bool RemoveComments { get; set; } = false; + /// + /// When enabled, treat <pre> (and <pre><code>) content as normal HTML + /// instead of converting it to a code block. + /// + public bool ConvertPreContentAsHtml { get; set; } = false; + /// /// Specify which schemes (without trailing colon) are to be allowed for <a> and <img> tags. Others will be bypassed. By default, allows everything. /// If provided and when href schema couldn't be determined - whitelists diff --git a/src/ReverseMarkdown/Converters/Pre.cs b/src/ReverseMarkdown/Converters/Pre.cs index 2189904..53a1d19 100644 --- a/src/ReverseMarkdown/Converters/Pre.cs +++ b/src/ReverseMarkdown/Converters/Pre.cs @@ -14,6 +14,11 @@ public Pre(Converter converter) : base(converter) public override void Convert(TextWriter writer, HtmlNode node) { + if (Converter.Config.ConvertPreContentAsHtml) { + ConvertHtmlContent(writer, node); + return; + } + var isFencedCodeBlock = Converter.Config.GithubFlavored || Converter.Config.CommonMark; var indentation = Converter.Config.CommonMark @@ -64,6 +69,20 @@ public override void Convert(TextWriter writer, HtmlNode node) writer.WriteLine(); } + private void ConvertHtmlContent(TextWriter writer, HtmlNode node) + { + var contentNode = node.ChildNodes["code"] ?? node; + if (contentNode.HasChildNodes) { + foreach (var child in contentNode.ChildNodes) { + Converter.ConvertNode(writer, child); + } + + return; + } + + TreatChildren(writer, node); + } + private string? GetLanguage(HtmlNode node) {