-
Notifications
You must be signed in to change notification settings - Fork 930
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+&@#/%=~_|$]")] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we don't use captured groups here, so we could add
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I looked at this regex more and I removed the group. I added flags to ignore case and culture. |
||
| public static partial Regex GenerateUrlRegEx(); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.