using System.Text.RegularExpressions; namespace Slang; public static class TextMeshProFormatter { private const string CODE_COLOR = "#FFD700"; public static string FromMarkdown(string markdown) { if (string.IsNullOrEmpty(markdown)) return ""; // 1. Normalize Line Endings string text = markdown.Replace("\r\n", "\n"); // 2. Handle Code Blocks (```) text = Regex.Replace( text, @"```\s*(.*?)\s*```", match => { var codeContent = match.Groups[1].Value; return $"{codeContent}"; // Gold color for code }, RegexOptions.Singleline ); text = Regex.Replace( text, @"^\s*##\s+(.+)$", "$1", RegexOptions.Multiline ); // 3. Handle # Headers SECOND (General) text = Regex.Replace( text, @"^\s*#\s+(.+)$", "$1", RegexOptions.Multiline ); // 4. Handle Inline Code (`code`) text = Regex.Replace(text, @"`([^`]+)`", $"$1"); // 5. Handle Bold (**text**) text = Regex.Replace(text, @"\*\*(.+?)\*\*", "$1"); // 6. Handle Italics (*text*) text = Regex.Replace(text, @"\*(.+?)\*", "$1"); // 7. Convert Newlines to TMP Line Breaks // Stationpedia needs
or explicit newlines. // Often just ensuring \n is preserved is enough, but
is safer for HTML-like parsers. text = text.Replace("\n", "
"); return text; } }