using System.Text.RegularExpressions; namespace API.Helpers; #nullable enable public static partial class StringHelper { #region Regex Source Generators [GeneratedRegex(@"\s?\(Source:\s*[^)]+\)")] private static partial Regex SourceRegex(); [GeneratedRegex(@"", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-US")] private static partial Regex BrStandardizeRegex(); [GeneratedRegex(@"(?:
\s*)+", RegexOptions.IgnoreCase | RegexOptions.Compiled, "en-US")] private static partial Regex BrMultipleRegex(); [GeneratedRegex(@"\s+")] private static partial Regex WhiteSpaceRegex(); [GeneratedRegex("@")] private static partial Regex HtmlEncodedAtSymbolRegex(); #endregion /// /// Used to squash duplicate break and new lines with a single new line. /// /// Test br br Test -> Test br Test /// /// public static string? SquashBreaklines(string? summary) { if (string.IsNullOrWhiteSpace(summary)) { return null; } // First standardize all br tags to
format summary = BrStandardizeRegex().Replace(summary, "
"); // Replace multiple consecutive br tags with a single br tag summary = BrMultipleRegex().Replace(summary, "
"); // Normalize remaining whitespace (replace multiple spaces with a single space) summary = WhiteSpaceRegex().Replace(summary, " ").Trim(); return summary.Trim(); } /// /// Removes the (Source: MangaDex) type of tags at the end of descriptions from AL /// /// /// public static string? RemoveSourceInDescription(string? description) { if (string.IsNullOrEmpty(description)) return description; return SourceRegex().Replace(description, string.Empty).Trim(); } /// /// Replaces some HTML encoded characters in urls with the proper symbol. This is common in People Description's /// /// /// public static string? CorrectUrls(string? description) { if (string.IsNullOrEmpty(description)) return description; return HtmlEncodedAtSymbolRegex().Replace(description, "@"); } }