Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ var converter = new ReverseMarkdown.Converter(config);
<sup><a href='/src/ReverseMarkdown.Test/Snippets.cs#L28-L44' title='Snippet source file'>snippet source</a> | <a href='#snippet-UsageWithConfig' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

To treat `<pre>` (and `<pre><code>`) 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
Expand All @@ -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 `<pre>` (and `<pre><code>`) 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 `<a>` tag href attribute
Expand Down
34 changes: 34 additions & 0 deletions src/ReverseMarkdown.Test/ConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,40 @@ public async Task Converter_Is_Thread_Safe_For_Concurrent_Use()
}
}

[Fact]
public void WhenPreContainsTable_ThenTreatAsCodeBlock()
{
var htmlTable = "<table><tr><td>a</td></tr></table>";
var htmlPre = $"<pre>{htmlTable}</pre>";
var htmlPreCode = $"<pre><code>{htmlTable}</code></pre>";
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 = "<p>Title</p><p><strong>Bold</strong></p>";
var htmlPre = $"<pre>{htmlContent}</pre>";
var htmlPreCode = $"<pre><code>{htmlContent}</code></pre>";
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()
{
Expand Down
6 changes: 6 additions & 0 deletions src/ReverseMarkdown/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public class Config

public bool RemoveComments { get; set; } = false;

/// <summary>
/// When enabled, treat &lt;pre&gt; (and &lt;pre&gt;&lt;code&gt;) content as normal HTML
/// instead of converting it to a code block.
/// </summary>
public bool ConvertPreContentAsHtml { get; set; } = false;

/// <summary>
/// Specify which schemes (without trailing colon) are to be allowed for &lt;a&gt; and &lt;img&gt; tags. Others will be bypassed. By default, allows everything.
/// <para>If <see cref="string.Empty" /> provided and when href schema couldn't be determined - whitelists</para>
Expand Down
19 changes: 19 additions & 0 deletions src/ReverseMarkdown/Converters/Pre.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down