Files
atakanozbancom/Models/classes/DescriptionFormatter.cs
T

34 lines
1.0 KiB
C#

using System.Text.RegularExpressions;
using System.Web;
namespace atakanozbancom.Models.classes
{
public static class DescriptionFormatter
{
private static readonly Regex LinkPattern = new Regex(
@"\[([^\]\r\n]{1,200})\]\((https?://[^\s)]{1,2000})\)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static string ToSafeHtml(string raw)
{
if (string.IsNullOrWhiteSpace(raw))
return string.Empty;
var encoded = HttpUtility.HtmlEncode(raw);
var withLinks = LinkPattern.Replace(encoded, m =>
{
var label = m.Groups[1].Value;
var url = m.Groups[2].Value;
return $"<a href=\"{url}\" class=\"text-secondary text-decoration-none\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">{label}</a>";
});
var withBreaks = withLinks
.Replace("\r\n", "\n")
.Replace("\n", "<br />");
return withBreaks;
}
}
}