using System.Net; using Remora.Discord.Extensions.Formatting; namespace Octobot.Extensions; public static class StringExtensions { /// /// Sanitizes a string for use in by inserting zero-width spaces in between /// symbols used to format the string with block code. /// /// The string to sanitize. /// The sanitized string that can be safely used in . private static string SanitizeForBlockCode(this string s) { return s.Replace("```", "​`​`​`​"); } /// /// Sanitizes a string (see ) and formats the string to use Markdown Block Code /// formatting with a specified /// language for syntax highlighting. /// /// The string to sanitize and format. /// /// /// The sanitized string formatted to use Markdown Block Code with a specified /// language for syntax highlighting. /// public static string InBlockCode(this string s, string language = "") { s = s.SanitizeForBlockCode(); return $"```{language}\n{s.SanitizeForBlockCode()}{(s.EndsWith("`", StringComparison.Ordinal) || string.IsNullOrWhiteSpace(s) ? " " : "")}```"; } public static string Localized(this string key) { return Messages.ResourceManager.GetString(key, Messages.Culture) ?? key; } /// /// Encodes a string to allow its transmission in request headers. /// /// Used when encountering "Request headers must contain only ASCII characters". /// The string to encode. /// An encoded string with spaces kept intact. public static string EncodeHeader(this string s) { return WebUtility.UrlEncode(s).Replace('+', ' '); } }