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