From e37e090ab293e4106700685d49dc1ae9fac3f75d Mon Sep 17 00:00:00 2001 From: Moepchi Date: Sun, 4 Oct 2026 10:29:05 +0000 Subject: [PATCH] feat: render BBCode in chat messages Server, channel and private chat now render [b] [i] [u] [s] [color] [url] (and [img] as a plain link), and bare http(s) URLs become links - the welcome message's [URL]...[/URL] showed up as raw text before. Built as React elements, so message text can't inject markup; only http(s) links are allowed and [color] only takes a name or hex value. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- web/src/App.css | 5 ++ web/src/App.tsx | 7 +-- web/src/lib/bbcode.tsx | 101 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 web/src/lib/bbcode.tsx diff --git a/web/src/App.css b/web/src/App.css index 83227e7..ff9262e 100644 --- a/web/src/App.css +++ b/web/src/App.css @@ -2452,6 +2452,11 @@ word-break: break-word; } +.ts-chat-bubble a { + color: var(--accent); + text-decoration: underline; +} + .ts-chat-from { font-weight: 600; color: var(--accent); diff --git a/web/src/App.tsx b/web/src/App.tsx index e2b6adb..be25a23 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -143,6 +143,7 @@ import { GreenteaSpeakChrome, isGreenteaSpeakTheme, } from "./themes/greenteaspeak"; +import { renderBbcode } from "./lib/bbcode"; import { parseChannelName, spacerDisplayName } from "./lib/spacer"; import { applyParkedGatewayEvent, @@ -11646,7 +11647,7 @@ function AppInner() { className={`ts-chat-line${entry.from === ownClient?.name ? " ts-chat-line-self" : ""}`} > {entry.from}:{" "} - {entry.message} + {renderBbcode(entry.message)} )) : activeTab === "server" @@ -11661,7 +11662,7 @@ function AppInner() { className={`ts-chat-line${entry.from === ownClient?.name ? " ts-chat-line-self" : ""}`} > {entry.from}:{" "} - {entry.message} + {renderBbcode(entry.message)} ) ) @@ -11670,7 +11671,7 @@ function AppInner() { {entry.fromSelf ? t("chat.you") : pmThreads[activeTab].partnerName}: {" "} - {entry.message} + {renderBbcode(entry.message)} ))}
diff --git a/web/src/lib/bbcode.tsx b/web/src/lib/bbcode.tsx new file mode 100644 index 0000000..72e3837 --- /dev/null +++ b/web/src/lib/bbcode.tsx @@ -0,0 +1,101 @@ +import { Fragment, type ReactNode } from "react"; + +// TeamSpeak chat BBCode -> React elements (never HTML strings, so message text +// can't inject markup). Unknown tags stay as literal text, unclosed tags close +// at the end of the message, and bare URLs are linked too. +// ponytail: only the inline tags people actually send; [list]/[table]/[img] +// render as plain text ([img] as a link, so it never loads a remote image). + +const TAG_REGEX = /\[(\/?)(b|i|u|s|url|img|color)(?:=([^\]]*))?\]/gi; +const BARE_URL_REGEX = /https?:\/\/[^\s<>"\[\]]+/g; +const COLOR_REGEX = /^(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z]+)$/i; + +interface Node { + tag: string; + arg?: string; + children: (Node | string)[]; +} + +function safeHref(raw: string): string | null { + const url = raw.trim(); + return /^https?:\/\//i.test(url) ? url : /^www\./i.test(url) ? `https://${url}` : null; +} + +function link(href: string, children: ReactNode, key: number) { + return ( + + {children} + + ); +} + +function linkify(text: string, key: number): ReactNode { + const parts: ReactNode[] = []; + let last = 0; + for (const m of text.matchAll(BARE_URL_REGEX)) { + if (m.index > last) parts.push(text.slice(last, m.index)); + parts.push(link(m[0], m[0], m.index)); + last = m.index + m[0].length; + } + if (last === 0) return text; + if (last < text.length) parts.push(text.slice(last)); + return {parts}; +} + +function textOf(node: Node): string { + return node.children.map((c) => (typeof c === "string" ? c : textOf(c))).join(""); +} + +function render(node: Node, key: number): ReactNode { + const kids = () => node.children.map((c, i) => (typeof c === "string" ? linkify(c, i) : render(c, i))); + switch (node.tag) { + case "b": + return {kids()}; + case "i": + return {kids()}; + case "u": + return {kids()}; + case "s": + return {kids()}; + case "color": + return node.arg && COLOR_REGEX.test(node.arg) ? ( + + {kids()} + + ) : ( + {kids()} + ); + case "url": + case "img": { + const href = safeHref(node.arg ?? textOf(node)); + // Plain-text label: a linkified URL or nested tag inside would nest links. + const label = textOf(node); + return href ? link(href, label, key) : {label}; + } + default: + return {kids()}; + } +} + +export function renderBbcode(text: string): ReactNode { + const root: Node = { tag: "", children: [] }; + const stack = [root]; + let last = 0; + for (const m of text.matchAll(TAG_REGEX)) { + const top = stack[stack.length - 1]; + if (m.index > last) top.children.push(text.slice(last, m.index)); + last = m.index + m[0].length; + const tag = m[2].toLowerCase(); + if (m[1]) { + const at = stack.findLastIndex((n) => n.tag === tag); + if (at > 0) stack.length = at; + else top.children.push(m[0]); + } else { + const node: Node = { tag, arg: m[3], children: [] }; + top.children.push(node); + stack.push(node); + } + } + if (last < text.length) stack[stack.length - 1].children.push(text.slice(last)); + return render(root, 0); +}