diff --git a/frontend/src/components/chat/MentionText.vue b/frontend/src/components/chat/MentionText.vue index 21ab9c11..82540613 100644 --- a/frontend/src/components/chat/MentionText.vue +++ b/frontend/src/components/chat/MentionText.vue @@ -1,6 +1,7 @@ @@ -33,4 +43,14 @@ const tokens = computed(() => tokenizeMentionText(props.content, props.mentions border-radius: 4px; background: rgba(22, 135, 88, 0.14); } + +.message-link { + color: #006aa6; + text-decoration: none; + overflow-wrap: anywhere; +} + +.message-link:hover { + text-decoration: underline; +} diff --git a/frontend/src/message-text.js b/frontend/src/message-text.js new file mode 100644 index 00000000..63be0861 --- /dev/null +++ b/frontend/src/message-text.js @@ -0,0 +1,100 @@ +import { tokenizeMentionText } from "./mentions.js"; + +const WEB_LINK_PATTERN = /(?:https?:\/\/|www\.)[^\s<>"',。!?、:;]+/giu; +const SIMPLE_TRAILING_PUNCTUATION = new Set([ + ".", + ",", + "!", + "?", + ";", + ":", + ",", + "。", + "!", + "?", + "、", + ":", + ";", +]); +const CLOSING_BRACKETS = new Map([ + [")", "("], + ["]", "["], + ["}", "{"], +]); + +function isLinkBoundary(character) { + return !character || /[\s([{"',。!?、:;]/u.test(character); +} + +function countCharacter(value, character) { + let count = 0; + for (const current of value) { + if (current === character) count += 1; + } + return count; +} + +function trimLinkEnd(value) { + let end = value.length; + while (end > 0) { + const lastCharacter = value[end - 1]; + if (SIMPLE_TRAILING_PUNCTUATION.has(lastCharacter)) { + end -= 1; + continue; + } + + const openingBracket = CLOSING_BRACKETS.get(lastCharacter); + if (!openingBracket) break; + const candidate = value.slice(0, end); + if ( + countCharacter(candidate, lastCharacter) <= + countCharacter(candidate, openingBracket) + ) { + break; + } + end -= 1; + } + return value.slice(0, end); +} + +function linkHref(text) { + return text.toLowerCase().startsWith("www.") ? `https://${text}` : text; +} + +function isWebLink(text) { + try { + const url = new URL(linkHref(text)); + return ["http:", "https:"].includes(url.protocol) && Boolean(url.hostname); + } catch { + return false; + } +} + +export function tokenizeWebLinks(text) { + const tokens = []; + let cursor = 0; + + for (const match of text.matchAll(WEB_LINK_PATTERN)) { + const offset = match.index; + if (!isLinkBoundary(text[offset - 1])) continue; + + const linkText = trimLinkEnd(match[0]); + if (!isWebLink(linkText)) continue; + if (offset > cursor) { + tokens.push({ type: "text", text: text.slice(cursor, offset) }); + } + tokens.push({ type: "link", text: linkText, href: linkHref(linkText) }); + cursor = offset + linkText.length; + } + + if (cursor < text.length) { + tokens.push({ type: "text", text: text.slice(cursor) }); + } + return tokens; +} + +export function tokenizeMessageText(content, mentions) { + return tokenizeMentionText(content, mentions).flatMap((token) => + token.type === "text" ? tokenizeWebLinks(token.text) : token, + ); +} diff --git a/frontend/src/message-text.ts b/frontend/src/message-text.ts new file mode 100644 index 00000000..989c2658 --- /dev/null +++ b/frontend/src/message-text.ts @@ -0,0 +1,11 @@ +import type { MentionTextToken } from "./mentions.ts"; + +export type MessageLinkToken = { + type: "link"; + text: string; + href: string; +}; + +export type MessageTextToken = MentionTextToken | MessageLinkToken; + +export { tokenizeMessageText, tokenizeWebLinks } from "./message-text.js"; diff --git a/test/frontend-message-links.test.js b/test/frontend-message-links.test.js new file mode 100644 index 00000000..cecfc5e0 --- /dev/null +++ b/test/frontend-message-links.test.js @@ -0,0 +1,58 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import test from "node:test"; + +import { + tokenizeMessageText, + tokenizeWebLinks, +} from "../frontend/src/message-text.js"; + +const mentionTextComponent = readFileSync( + new URL("../frontend/src/components/chat/MentionText.vue", import.meta.url), + "utf8", +).replaceAll("\r\n", "\n"); + +test("消息正文识别网页链接并保留句末标点", () => { + const content = "文档:https://example.com/docs?q=chat。备用 www.example.org/path_(v2)。"; + const tokens = tokenizeWebLinks(content); + + assert.equal(tokens.map((token) => token.text).join(""), content); + assert.deepEqual( + tokens.filter((token) => token.type === "link"), + [ + { + type: "link", + text: "https://example.com/docs?q=chat", + href: "https://example.com/docs?q=chat", + }, + { + type: "link", + text: "www.example.org/path_(v2)", + href: "https://www.example.org/path_(v2)", + }, + ], + ); +}); + +test("消息链接与结构化提及可以同时分段", () => { + const content = "@alice 看 https://example.com,javascript:alert(1) 保持为文本"; + const tokens = tokenizeMessageText(content, [{ userId: 2, username: "alice" }]); + + assert.equal(tokens.map((token) => token.text).join(""), content); + assert.deepEqual(tokens.map((token) => token.type), ["mention", "text", "link", "text"]); + assert.equal(tokens.some((token) => token.type === "link" && token.href.startsWith("javascript:")), false); +}); + +test("残缺网址保持为普通文本而不是站内相对链接", () => { + for (const content of ["www..", "https://.", "prefixwww.example.com"]) { + assert.deepEqual(tokenizeWebLinks(content), [{ type: "text", text: content }]); + } +}); + +test("消息链接使用新标签页并保留浏览器原生右键菜单", () => { + assert.match(mentionTextComponent, /class="message-link"/); + assert.match(mentionTextComponent, /target="_blank"/); + assert.match(mentionTextComponent, /rel="noopener noreferrer"/); + assert.match(mentionTextComponent, /@pointerdown\.stop/); + assert.match(mentionTextComponent, /@contextmenu\.stop/); +});