Utils/MarkdownUtils.cs
using Ominous.Extensions;
namespace Ominous.Utils; public sealed class MarkdownUtils { public static readonly char[] ESCAPABLE_CHRS = new char[] { '`', '*', '_', '{', '}', '[', ']', '<', '>', '(', ')', '#', '+', '-', '.', '!', '|' }; public static void Escape(ref string s) => Escape(ref s, ESCAPABLE_CHRS); public static void Escape(ref string s, params char[] chars) { foreach (char ch in chars) { s = s.Replace($"{ch}", $"\\{ch}"); } } public static void HtmlEncode(ref string s, params char[] chars) { s = s.Replace("\r", ""); if (chars.Length == 0) { chars = new char[] { '\n', '\t', ' ', '|', '-', '<', '>', '[', ']' }; } foreach (char ch in chars) { switch (ch) { case '\n': s = s.Replace("\n", "<br>"); break; case '\t': s = s.Replace("\t", " ".Repeat(3)); break; case ' ': s = s.Replace(" ", " "); break; case '|': s = s.Replace("|", "|"); break; case '-': s = s.Replace("-", "‐"); break; case '<': s = s.Replace("<", "<"); break; case '>': s = s.Replace("<", ">"); break; case '[': s = s.Replace("[", "["); break; case ']': s = s.Replace("]", "]"); break; default: break; } } } } |