Skip to content

Commit

Permalink
Do *not* trim whitespace in <code> elements (#140)
Browse files Browse the repository at this point in the history
* Preserve indentation for first line of <pre> content

* Preserve significant whitespace in <code> elements

* Ignore insignificant leading/trailing whitespace in <code> elements

* Revert previous 3 commits (to isolate pull requests)

* Preserve indentation for first line of <pre> content

* Preserve significant whitespace in <code> elements

* Ignore insignificant leading/trailing whitespace in <code> elements

* Add note describing the "hack" for handling <code> whitespace

* Do *not* trim whitespace in <code> elements
  • Loading branch information
jeremy-jameson authored Mar 1, 2021
1 parent ba33c04 commit 5cebd03
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A JavaScript ` function ` ...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A JavaScript` function `...
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A JavaScript` function `...
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
```
function foo {
```

22 changes: 22 additions & 0 deletions src/ReverseMarkdown.Test/ConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,28 @@ public Task When_Tag_In_PassThoughTags_List_Then_Use_PassThroughConverter()
return CheckConversion(html, config);
}

[Fact]
public Task When_CodeContainsSpaces_ShouldPreserveSpaces()
{
var html = $"A JavaScript<code> function </code>...";
return CheckConversion(html);
}

[Fact]
public Task When_CodeContainsSpanWithExtraSpaces_Should_NotNormalizeSpaces()
{
var html = $"A JavaScript<code><span> function </span></code>...";
return CheckConversion(html);
}


[Fact]
public Task When_CodeContainsSpacesAndIsSurroundedByWhitespace_Should_NotRemoveSpaces()
{
var html = $"A JavaScript <code> function </code> ...";
return CheckConversion(html);
}

[Fact]
public Task When_PreTag_Contains_IndentedFirstLine_Should_PreserveIndentation()
{
Expand Down
48 changes: 47 additions & 1 deletion src/ReverseMarkdown/Converters/Code.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using HtmlAgilityPack;
using System.Net;
using System.Text;

namespace ReverseMarkdown.Converters
{
Expand All @@ -11,7 +13,51 @@ public Code(Converter converter) : base(converter)

public override string Convert(HtmlNode node)
{
return $"`{System.Net.WebUtility.HtmlDecode(node.InnerText.Trim())}`";
// Depending on the content "surrounding" the <code> element,
// leading/trailing whitespace is significant. For example, the
// following HTML renders as expected in a browser (meaning there is
// proper spacing between words):
//
// <p>The JavaScript<code> function </code>keyword...</p>
//
// However, if we simply trim the contents of the <code> element,
// then the Markdown becomes:
//
// The JavaScript`function`keyword...
//
// To avoid this scenario, do *not* trim the inner text of the
// <code> element.
//
// For the HTML example above, the Markdown will be:
//
// The JavaScript` function `keyword...
//
// While it might seem preferable to try to "fix" this by trimming
// the <code> element and insert leading/trailing spaces as
// necessary, things become complicated rather quickly depending
// on the particular content. For example, what would be the
// "correct" conversion of the following HTML?
//
// <p>The JavaScript<b><code> function </code></b>keyword...</p>
//
// The simplest conversion to Markdown:
//
// The JavaScript**` function `**keyword...
//
// Some other, arguably "better", alternatives (that would require
// substantially more conversion logic):
//
// The JavaScript** `function` **keyword...
//
// The JavaScript **`function`** keyword...

var sb = new StringBuilder();

sb.Append('`');
sb.Append(WebUtility.HtmlDecode(node.InnerText));
sb.Append('`');

return sb.ToString();
}
}
}

0 comments on commit 5cebd03

Please sign in to comment.