diff --git a/apps/client/src/ee/ai-chat/components/chat-message.tsx b/apps/client/src/ee/ai-chat/components/chat-message.tsx index dd790a713..3eae42e7b 100644 --- a/apps/client/src/ee/ai-chat/components/chat-message.tsx +++ b/apps/client/src/ee/ai-chat/components/chat-message.tsx @@ -17,14 +17,26 @@ import ChatToolGroup from "./chat-tool-group"; import classes from "../styles/chat-message.module.css"; import CopyTextButton from "@/components/common/copy.tsx"; +const PAGE_PATH_RE = /\/s\/[^/?#]+\/p\/[^/?#]+/; + const chatSanitizer = DOMPurify(); chatSanitizer.addHook("afterSanitizeAttributes", (node) => { - if (node.tagName === "A") { - const href = node.getAttribute("href") || ""; - if (href.startsWith("http://") || href.startsWith("https://")) { - node.setAttribute("target", "_blank"); - node.setAttribute("rel", "noopener noreferrer"); - } + if (node.tagName !== "A") return; + const href = node.getAttribute("href") || ""; + + // Recover the canonical /s/{slug}/p/{slugId} path if the model wrapped it + // in a fabricated host (https://s/..., https://yoursite.com/s/..., //s/...). + const m = href.match(PAGE_PATH_RE); + if (m) { + node.setAttribute("href", m[0]); + node.removeAttribute("target"); + node.removeAttribute("rel"); + return; + } + + if (href.startsWith("http://") || href.startsWith("https://")) { + node.setAttribute("target", "_blank"); + node.setAttribute("rel", "noopener noreferrer"); } });