-
Notifications
You must be signed in to change notification settings - Fork 715
Automatically convert URLs in values into links in the dashboard #6176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,20 +12,22 @@ public static partial class UrlParser | |
| { | ||
| private static readonly Regex s_urlRegEx = GenerateUrlRegEx(); | ||
|
|
||
| public static bool TryParse(string? text, [NotNullWhen(true)] out string? modifiedText) | ||
| public static bool TryParse(string? text, Func<string, string>? nonMatchFragmentCallback, [NotNullWhen(true)] out string? modifiedText) | ||
| { | ||
| if (text is not null) | ||
| { | ||
| var urlMatch = s_urlRegEx.Match(text); | ||
|
|
||
| var builder = new StringBuilder(text.Length * 2); | ||
| StringBuilder? builder = null; | ||
|
|
||
| var nextCharIndex = 0; | ||
| while (urlMatch.Success) | ||
| { | ||
| builder ??= new StringBuilder(text.Length * 2); | ||
|
|
||
| if (urlMatch.Index > 0) | ||
| { | ||
| builder.Append(text[(nextCharIndex)..urlMatch.Index]); | ||
| AppendNonMatchFragment(builder, nonMatchFragmentCallback, text[(nextCharIndex)..urlMatch.Index]); | ||
| } | ||
|
|
||
| var urlStart = urlMatch.Index; | ||
|
|
@@ -36,11 +38,11 @@ public static bool TryParse(string? text, [NotNullWhen(true)] out string? modifi | |
| urlMatch = urlMatch.NextMatch(); | ||
| } | ||
|
|
||
| if (builder.Length > 0) | ||
| if (builder?.Length > 0) | ||
| { | ||
| if (nextCharIndex < text.Length) | ||
| { | ||
| builder.Append(text[(nextCharIndex)..]); | ||
| AppendNonMatchFragment(builder, nonMatchFragmentCallback, text[(nextCharIndex)..]); | ||
| } | ||
|
|
||
| modifiedText = builder.ToString(); | ||
|
|
@@ -50,17 +52,29 @@ public static bool TryParse(string? text, [NotNullWhen(true)] out string? modifi | |
|
|
||
| modifiedText = null; | ||
| return false; | ||
|
|
||
| static void AppendNonMatchFragment(StringBuilder stringBuilder, Func<string, string>? nonMatchFragmentCallback, string text) | ||
| { | ||
| if (nonMatchFragmentCallback != null) | ||
| { | ||
| text = nonMatchFragmentCallback(text); | ||
| } | ||
|
|
||
| stringBuilder.Append(text); | ||
| } | ||
| } | ||
|
|
||
| // Regular expression that detects http/https URLs in a log entry | ||
| // Based on the RegEx used in Windows Terminal for the same purpose, but limited | ||
| // to only http/https URLs | ||
| // | ||
| // Explanation: | ||
| // /b - Match must start at a word boundary (after whitespace or at the start of the text) | ||
| // (?<=m|\\b) - Match must start at a word boundary (after whitespace or at the start of the text) or "m". | ||
| // "m" is a trailing character of ANSI escape sequences. | ||
| // Required because URLs are matched before processing ANSI escape sequences. | ||
| // https?:// - http:// or https:// | ||
| // [-A-Za-z0-9+&@#/%?=~_|$!:,.;]* - Any character in the list, matched zero or more times. | ||
| // [A-Za-z0-9+&@#/%=~_|$] - Any character in the list, matched exactly once | ||
| [GeneratedRegex("\\bhttps?://[-A-Za-z0-9+&@#/%?=~_|$!:,.;]*[A-Za-z0-9+&@#/%=~_|$]")] | ||
| private static partial Regex GenerateUrlRegEx(); | ||
| [GeneratedRegex("(?<=m|\\b)https?://[-A-Za-z0-9+&@#/%?=~_|$!:,.;]*[A-Za-z0-9+&@#/%=~_|$]")] | ||
|
||
| public static partial Regex GenerateUrlRegEx(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net; | ||
| using Aspire.Dashboard.ConsoleLogs; | ||
| using Xunit; | ||
|
|
||
|
|
@@ -15,7 +16,7 @@ public class UrlParserTests | |
| [InlineData("This is some text without any urls")] | ||
| public void TryParse_NoUrl_ReturnsFalse(string? input) | ||
| { | ||
| var result = UrlParser.TryParse(input, out var _); | ||
| var result = UrlParser.TryParse(input, WebUtility.HtmlEncode, out var _); | ||
|
|
||
| Assert.False(result); | ||
| } | ||
|
|
@@ -26,7 +27,7 @@ public void TryParse_NoUrl_ReturnsFalse(string? input) | |
| [InlineData("This is some text with a https://bing.com/ in the middle", true, "This is some text with a <a target=\"_blank\" href=\"https://bing.com/\">https://bing.com/</a> in the middle")] | ||
| public void TryParse_ReturnsCorrectResult(string input, bool expectedResult, string? expectedOutput) | ||
| { | ||
| var result = UrlParser.TryParse(input, out var modifiedText); | ||
| var result = UrlParser.TryParse(input, WebUtility.HtmlEncode, out var modifiedText); | ||
|
|
||
| Assert.Equal(expectedResult, result); | ||
| Assert.Equal(expectedOutput, modifiedText); | ||
|
|
@@ -42,7 +43,7 @@ public void TryParse_ReturnsCorrectResult(string input, bool expectedResult, str | |
| [InlineData("http://bing", "<a target=\"_blank\" href=\"http://bing\">http://bing</a>")] | ||
| public void TryParse_SupportedUrlFormats(string input, string? expectedOutput) | ||
| { | ||
| var result = UrlParser.TryParse(input, out var modifiedText); | ||
| var result = UrlParser.TryParse(input, WebUtility.HtmlEncode, out var modifiedText); | ||
|
|
||
| Assert.True(result); | ||
| Assert.Equal(expectedOutput, modifiedText); | ||
|
|
@@ -54,8 +55,30 @@ public void TryParse_SupportedUrlFormats(string input, string? expectedOutput) | |
| [InlineData("ftp://user:[email protected]/")] | ||
| public void TryParse_UnsupportedUrlFormats(string input) | ||
| { | ||
| var result = UrlParser.TryParse(input, out var _); | ||
| var result = UrlParser.TryParse(input, WebUtility.HtmlEncode, out var _); | ||
|
|
||
| Assert.False(result); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("http://localhost:8080</url>", "<a target=\"_blank\" href=\"http://localhost:8080\">http://localhost:8080</a></url>")] | ||
| [InlineData("http://localhost:8080\"", "<a target=\"_blank\" href=\"http://localhost:8080\">http://localhost:8080</a>"")] | ||
| public void TryParse_ExcludeInvalidTrailingChars(string input, string? expectedOutput) | ||
| { | ||
| var result = UrlParser.TryParse(input, WebUtility.HtmlEncode, out var modifiedText); | ||
| Assert.True(result); | ||
|
|
||
| Assert.Equal(expectedOutput, modifiedText); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("http://www.localhost:8080")] | ||
| [InlineData("mhttp://www.localhost:8080")] | ||
| [InlineData(" http://www.localhost:8080")] | ||
| public void GenerateUrlRegEx_MatchUrlAfterContent(string content) | ||
| { | ||
| var regex = UrlParser.GenerateUrlRegEx(); | ||
| var match = regex.Match(content); | ||
| Assert.Equal("http://www.localhost:8080", match.Value); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.