using System.Net; using Remora.Discord.Extensions.Formatting; namespace Octobot.Extensions; public static class StringExtensions { private const string ZeroWidthSpace = "​"; /// /// 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("```", $"{ZeroWidthSpace}`{ZeroWidthSpace}`{ZeroWidthSpace}`{ZeroWidthSpace}"); } /// /// Sanitizes a string for use in when "language" is "diff" by /// prepending a zero-width space before the input string to prevent Discord from applying syntax highlighting. /// /// This does not call , you have to do so yourself if needed. /// The string to sanitize. /// The sanitized string that can be safely used in with "diff" as the language. public static string SanitizeForDiffBlock(this string s) { return $"{ZeroWidthSpace}{s}"; } /// /// 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('+', ' '); } }