Skip to content

Commit

Permalink
Fix GH-388 handling surrounding spaces for emphasis/inline tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticmind committed May 4, 2024
1 parent 162dc63 commit d145501
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
... example html *code *block
... example html *code* block
12 changes: 5 additions & 7 deletions src/ReverseMarkdown/Converters/Em.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ public override string Convert(HtmlNode node)
{
return content;
}
else
{
var spaceSuffix = (node.NextSibling?.Name == "i" || node.NextSibling?.Name == "em")
? " "
: "";

return $"*{content.Chomp(all:true)}*{spaceSuffix}";
}
var spaceSuffix = (node.NextSibling?.Name == "i" || node.NextSibling?.Name == "em")
? " "
: "";

return content.EmphasizeContentWhitespaceGuard("*", spaceSuffix);
}

private static bool AlreadyItalic(HtmlNode node)
Expand Down
6 changes: 2 additions & 4 deletions src/ReverseMarkdown/Converters/S.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public override string Convert(HtmlNode node)
{
return content;
}
else
{
return $"~~{content.Chomp(all:true)}~~";
}

return content.EmphasizeContentWhitespaceGuard("~~");
}

private static bool AlreadyStrikethrough(HtmlNode node)
Expand Down
12 changes: 5 additions & 7 deletions src/ReverseMarkdown/Converters/Strong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ public override string Convert(HtmlNode node)
{
return content;
}
else
{
var spaceSuffix = (node.NextSibling?.Name == "strong" || node.NextSibling?.Name == "b")
? " "
: "";

var spaceSuffix = (node.NextSibling?.Name == "strong" || node.NextSibling?.Name == "b")
? " "
: "";

return $"**{content.Chomp(all:true)}**{spaceSuffix}";
}
return content.EmphasizeContentWhitespaceGuard("**", spaceSuffix);
}

private static bool AlreadyBold(HtmlNode node)
Expand Down
6 changes: 2 additions & 4 deletions src/ReverseMarkdown/Converters/Sup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ public override string Convert(HtmlNode node)
{
return content;
}
else
{
return $"^{content.Chomp(all:true)}^";
}

return $"^{content.Chomp(all:true)}^";
}

private static bool AlreadySup(HtmlNode node)
Expand Down
45 changes: 44 additions & 1 deletion src/ReverseMarkdown/StringUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static string Chomp(this string content, bool all=false)
{
return content
.Replace("\r", "")
.Replace("\n", "");
.Replace("\n", "")
.Trim();
}

return content.Trim().TrimEnd('\r', '\n');
Expand Down Expand Up @@ -78,6 +79,48 @@ public static Dictionary<string, string> ParseStyle(string style)
.ToDictionary(styleParts => styleParts[0], styleParts => styleParts[1]);
}

public static int LeadingSpaceCount(this string content)
{
var leadingSpaces = 0;
foreach (var c in content)
{
if (c == ' ')
{
leadingSpaces++;
}
else
{
break;
}
}
return leadingSpaces;
}

public static int TrailingSpaceCount(this string content)
{
var trailingSpaces = 0;
for (var i = content.Length - 1; i >= 0; i--)
{
if (content[i] == ' ')
{
trailingSpaces++;
}
else
{
break;
}
}
return trailingSpaces;
}

public static string EmphasizeContentWhitespaceGuard(this string content, string emphasis, string nextSiblingSpaceSuffix="")
{
var leadingSpaces = new string(' ', content.LeadingSpaceCount());
var trailingSpaces = new string(' ', content.TrailingSpaceCount());

return $"{leadingSpaces}{emphasis}{content.Chomp(all:true)}{emphasis}{(trailingSpaces.Length > 0 ? trailingSpaces : nextSiblingSpaceSuffix)}";
}

private static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> enumerable, Func<T, TKey> keySelector)
{
return enumerable.GroupBy(keySelector).Select(grp => grp.First());
Expand Down

0 comments on commit d145501

Please sign in to comment.