using System;
using System.Text.RegularExpressions;
namespace Slang;
public static class TextMeshProFormatter
{
private const string CODE_COLOR = "#FFD700"; // Gold
private const string LINK_COLOR = "#0099FF"; // Blue
private const string QUOTE_COLOR = "#90EE90"; // Light Green
public static string FromMarkdown(string markdown)
{
if (string.IsNullOrEmpty(markdown))
return "";
// Normalize Line Endings
string text = markdown.Replace("\r\n", "\n");
// Process code blocks FIRST (``` ... ```)
text = Regex.Replace(
text,
@"```[^\n]*\n(.*?)\n```",
match =>
{
var codeContent = match.Groups[1].Value;
return $"{codeContent}";
},
RegexOptions.Singleline
);
// Process headers - check for 1-6 hashes
text = Regex.Replace(text, @"^#{1}\s+(.+)$", "$1", RegexOptions.Multiline);
text = Regex.Replace(text, @"^#{2}\s+(.+)$", "$1", RegexOptions.Multiline);
text = Regex.Replace(text, @"^#{3}\s+(.+)$", "$1", RegexOptions.Multiline);
text = Regex.Replace(text, @"^#{4}\s+(.+)$", "$1", RegexOptions.Multiline);
text = Regex.Replace(text, @"^#{5}\s+(.+)$", "$1", RegexOptions.Multiline);
text = Regex.Replace(text, @"^#{6}\s+(.+)$", "$1", RegexOptions.Multiline);
// Process markdown links [text](url)
text = Regex.Replace(
text,
@"\[([^\]]+)\]\(([^\)]+)\)",
$"$1"
);
// Process inline code (`code`)
text = Regex.Replace(text, @"`([^`]+)`", $"$1");
// Process bold (**text**)
text = Regex.Replace(text, @"\*\*(.+?)\*\*", "$1");
// Process italics (*text*)
text = Regex.Replace(text, @"(?$1");
// Process block quotes (> text)
text = Regex.Replace(
text,
@"^>\s+(.+)$",
$"$1",
RegexOptions.Multiline
);
// Process unordered lists (- items)
text = Regex.Replace(
text,
@"^-\s+(.+)$",
" • $1",
RegexOptions.Multiline
);
return text;
}
}