## Converts a (small, practical) subset of Markdown to Godot's RichTextLabel ## BBCode: fenced code blocks, inline code, bold, italic, headers, bullets. ## Not a full CommonMark implementation — just enough for chat responses. class_name ClaudeGodotMarkdown extends RefCounted static func to_bbcode(text: String) -> String: var fence_re := RegEx.new() fence_re.compile("```([a-zA-Z0-9_+-]*)\\n?([\\s\\S]*?)```") var out := "" var pos := 0 for m in fence_re.search_all(text): out += _inline_to_bbcode(text.substr(pos, m.get_start() - pos)) var lang := m.get_string(1) var code := m.get_string(2) out += "\n" + ClaudeGodotSyntaxHighlight.highlight(code, lang) + "\n" pos = m.get_end() out += _inline_to_bbcode(text.substr(pos)) return out static func _inline_to_bbcode(text: String) -> String: # Escape stray brackets first so only tags we insert below are live BBCode. text = text.replace("[", "[lb]") text = _sub(text, "(?m)^#{1,6}\\s+(.*)$", "[b]$1[/b]") text = _sub(text, "(?m)^\\s*[-*]\\s+(.*)$", "• $1") text = _sub(text, "\\*\\*([^*]+)\\*\\*", "[b]$1[/b]") text = _sub(text, "`([^`]+)`", "[code]$1[/code]") text = _sub(text, "\\*([^*]+)\\*", "[i]$1[/i]") return text static func _sub(text: String, pattern: String, replacement: String) -> String: var re := RegEx.new() if re.compile(pattern) != OK: return text return re.sub(text, replacement, true)