Skip to content

Commit

Permalink
Add functionality to trim encompassing new lines within block tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticmind committed Mar 26, 2019
1 parent 8b535a2 commit e98af5d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/ReverseMarkdown/Converters/ConverterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,25 @@ protected string TreatChildren(HtmlNode node)
}

private string Treat(HtmlNode node) {
TrimNewLine(node);
return Converter.Lookup(node.Name).Convert(node);
}

private static void TrimNewLine(HtmlNode node)
{
if (!node.HasChildNodes) return;

if (node.FirstChild.Name == "#text" && (node.FirstChild.InnerText.StartsWith("\r\n") || node.FirstChild.InnerText.StartsWith("\n")))
{
node.FirstChild.InnerHtml = node.FirstChild.InnerHtml.TrimStart('\r').TrimStart('\n');
}

if (node.LastChild.Name == "#text" && (node.LastChild.InnerText.EndsWith("\r\n") || node.LastChild.InnerText.EndsWith("\n")))
{
node.LastChild.InnerHtml = node.LastChild.InnerHtml.TrimEnd('\r').TrimEnd('\n');
}
}

protected string ExtractTitle(HtmlNode node)
{
var title = node.GetAttributeValue("title", "");
Expand Down
3 changes: 0 additions & 3 deletions src/ReverseMarkdown/Converters/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ private string TreatText(HtmlNode node)
case "ul":
content = content.Trim();
break;
case "p":
content = content.TrimStart('\r', '\n').TrimEnd('\r', '\n');
break;
}

content = content.Replace("\r\n", "<br>");
Expand Down

0 comments on commit e98af5d

Please sign in to comment.