diff --git a/web/src/App.tsx b/web/src/App.tsx index be25a23..3798cc4 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -143,7 +143,7 @@ import { GreenteaSpeakChrome, isGreenteaSpeakTheme, } from "./themes/greenteaspeak"; -import { renderBbcode } from "./lib/bbcode"; +import { BARE_URL_REGEX, renderBbcode, wrapUrls } from "./lib/bbcode"; import { parseChannelName, spacerDisplayName } from "./lib/spacer"; import { applyParkedGatewayEvent, @@ -575,8 +575,6 @@ function loadCollectedUrls(): CollectedUrl[] { } } -const URL_REGEX = /https?:\/\/[^\s<>"]+/g; - type LogLevel = "critical" | "error" | "warning" | "info" | "debug"; interface ClientLogEntry { @@ -6962,7 +6960,7 @@ function AppInner() { }; const recordUrlsFromMessage = (message: string, sender: string) => { - const found = message.match(URL_REGEX); + const found = message.match(BARE_URL_REGEX); if (!found) return; const now = Date.now(); setCollectedUrls((prev) => { @@ -8991,7 +8989,7 @@ function AppInner() { }, [chatInput, designTheme, connected]); const handleSendChat = () => { - const message = chatInput.trim(); + const message = wrapUrls(chatInput.trim()); if (!message) return; if (activeTab === "channel") { socketRef.current?.send(JSON.stringify({ type: "sendChatMessage", message })); @@ -9408,7 +9406,7 @@ function AppInner() { }; const handleSendOfflineMessage = (clientUid: string, subject: string, message: string) => { - socketRef.current?.send(JSON.stringify({ type: "sendOfflineMessage", clientUid, subject, message })); + socketRef.current?.send(JSON.stringify({ type: "sendOfflineMessage", clientUid, subject, message: wrapUrls(message) })); }; const handleClientContextMenu = ( diff --git a/web/src/lib/bbcode.tsx b/web/src/lib/bbcode.tsx index 72e3837..79bc701 100644 --- a/web/src/lib/bbcode.tsx +++ b/web/src/lib/bbcode.tsx @@ -7,7 +7,9 @@ import { Fragment, type ReactNode } from "react"; // 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; +export const BARE_URL_REGEX = /https?:\/\/[^\s<>"\[\]]+/g; +// An already-tagged link (left alone) or a bare URL (wrapped). +const WRAP_REGEX = new RegExp(`(\\[(url|img)(?:=[^\\]]*)?\\][\\s\\S]*?\\[\\/\\2\\])|${BARE_URL_REGEX.source}`, "gi"); const COLOR_REGEX = /^(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z]+)$/i; interface Node { @@ -99,3 +101,8 @@ export function renderBbcode(text: string): ReactNode { if (last < text.length) stack[stack.length - 1].children.push(text.slice(last)); return render(root, 0); } + +// Outgoing: native clients send links as [URL]...[/URL] and only link those. +export function wrapUrls(text: string): string { + return text.replace(WRAP_REGEX, (m, tagged) => (tagged ? m : `[URL]${m}[/URL]`)); +}