0

I am using RichTextbox of Radzen to add comment from UI

I want to show link as clickable

enter image description here

it look like this after render enter image description here

I am using below function as convert this string to render ready

Actual string stored in DB and coming for render

Test comment from UI<div><span style="font-family: var(--mud-typography-body1-family); font-size: var(--mud-typography-body1-size); font-weight: var(--mud-typography-body1-weight); letter-spacing: var(--mud-typography-body1-letterspacing); text-transform: var(--mud-typography-body1-text-transform); color: var(--mud-palette-text-primary);">Adding data from UI&nbsp;</span><br></div><div><br>https://www.google.com/search?q=sample+link&amp;rlz=1C1CHWA_enIN1032IN1032&amp;sxsrf=APwXEde-ZwfaBWMiBW7hUGv8aGEEgQyi-Q:1682440201890&amp;ei=CQBIZMn6NYqRseMP0LubEA&amp;start=10&amp;sa=N&amp;ved=2ahUKEwiJo9uUusX-AhWKSGwGHdDdBgIQ8NMDegQIBhAW&amp;biw=1745&amp;bih=881&amp;dpr=1.1<br><br>https://crypto.stanford.edu/cs142/lectures/url.html<br>Thank you&nbsp;</div>

UI code in blazor

@((MarkupString)(TicketHistory.Message.ConvertTextUrlToLink()))

extension method

public static string ConvertTextUrlToLink(this string arg)
    {
        string originalString = arg;
        try
        {
            //arg.Replace("https://www")
            Regex urlregex = new Regex(@"(^|[\n ])(?<url>(www|ftp)\.[^ ,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            arg = urlregex.Replace(arg, " <a href=\"http://${url}\" target=\"&#95;blank\"><u>${url}</u></a>");
            Regex httpurlregex = new Regex(@"(^|[\n ])(?<url>(http://www\.|https://www\.|http://|https://)[^ ,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            arg = httpurlregex.Replace(arg, " <a href=\"${url}\" target=\"&#95;blank\"><u>${url}</u></a>");
            Regex emailregex = new Regex(@"(?<url>[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+\s)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            arg = emailregex.Replace(arg, " <a href=\"mailto:${url}\"><u>${url}</u></a> ");

            if (originalString.Equals(arg, StringComparison.OrdinalIgnoreCase))
            {
                Regex urlRegex1 = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
                                 RegexOptions.IgnoreCase | RegexOptions.Compiled);
                arg = urlRegex1.Replace(arg,
                             "<a href=\"$1\" target=\"_blank\">$1</a>");

                //string regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
                //Regex filterRegex = new Regex(regex, RegexOptions.IgnoreCase);
                //arg = filterRegex.Replace(arg, "<a href=\"$1\" title=\"$1\" target=\"&#95;blank\"><u>$1</u></a>").Replace("href=\"www", "href=\"http://www");
            }

            return arg;
        }
        catch (Exception)
        {
            return originalString;
        }
    }

0 Answers0