Skip to content

Commit

Permalink
ported domaindrivendev#2392 Replace <see href="link">text</see> with …
Browse files Browse the repository at this point in the history
…Markdown link format
  • Loading branch information
Havunen committed May 5, 2024
1 parent 2a1b5c3 commit afef631
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ namespace DotSwashbuckle.AspNetCore.SwaggerGen
{
public static class XmlCommentsTextHelper
{
private static Regex RefTagPattern = new Regex(@"<(see|paramref) (name|cref|langword)=""([TPF]{1}:)?(?<display>.+?)"" ?/>");
private static Regex CodeTagPattern = new Regex(@"<c>(?<display>.+?)</c>");
private static Regex RefTagPattern = new Regex(@"<(see|paramref) (name|cref|langword)=""([TPF]{1}:)?(?<display>.+?)"" ?/>", RegexOptions.Compiled);
private static Regex CodeTagPattern = new Regex(@"<c>(?<display>.+?)</c>", RegexOptions.Compiled);
private static Regex MultilineCodeTagPattern = new Regex(@"<code>(?<display>.+?)</code>", RegexOptions.Singleline);
private static Regex ParaTagPattern = new Regex(@"<para>(?<display>.+?)</para>", RegexOptions.Singleline);
private static Regex HrefPattern = new Regex(@"<see href=\""(.*)\"">(.*)<\/see>", RegexOptions.Compiled);

public static string Humanize(string text)
{
Expand All @@ -22,6 +23,7 @@ public static string Humanize(string text)
return text
.NormalizeIndentation()
.HumanizeRefTags()
.HumanizeHrefTags()
.HumanizeCodeTags()
.HumanizeMultilineCodeTags()
.HumanizeParaTags()
Expand Down Expand Up @@ -92,6 +94,11 @@ private static string HumanizeRefTags(this string text)
return RefTagPattern.Replace(text, (match) => match.Groups["display"].Value);
}

private static string HumanizeHrefTags(this string text)
{
return HrefPattern.Replace(text, m => $"[{m.Groups[2].Value}]({m.Groups[1].Value})");
}

private static string HumanizeCodeTags(this string text)
{
return CodeTagPattern.Replace(text, (match) => "`" + match.Groups["display"].Value + "`");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Misplaced Tab Indentation
[InlineData("<para>This is a paragraph</para>.", "<br>This is a paragraph.")]
[InlineData("GET /Todo?iscomplete=true&amp;owner=mike", "GET /Todo?iscomplete=true&owner=mike")]
[InlineData(@"Returns a <see langword=""null""/> item.", "Returns a null item.")]
[InlineData(@"<see href=""https://www.iso.org/iso-4217-currency-codes.html"">ISO currency code</see>", "[ISO currency code](https://www.iso.org/iso-4217-currency-codes.html)")]
public void Humanize_HumanizesInlineTags(
string input,
string expectedOutput)
Expand Down

0 comments on commit afef631

Please sign in to comment.