fix: internal links

This commit is contained in:
Philipinho
2026-05-21 17:01:20 +01:00
parent 13a7f1372f
commit f010f6a83a
@@ -17,14 +17,26 @@ import ChatToolGroup from "./chat-tool-group";
import classes from "../styles/chat-message.module.css"; import classes from "../styles/chat-message.module.css";
import CopyTextButton from "@/components/common/copy.tsx"; import CopyTextButton from "@/components/common/copy.tsx";
const PAGE_PATH_RE = /\/s\/[^/?#]+\/p\/[^/?#]+/;
const chatSanitizer = DOMPurify(); const chatSanitizer = DOMPurify();
chatSanitizer.addHook("afterSanitizeAttributes", (node) => { chatSanitizer.addHook("afterSanitizeAttributes", (node) => {
if (node.tagName === "A") { if (node.tagName !== "A") return;
const href = node.getAttribute("href") || ""; const href = node.getAttribute("href") || "";
if (href.startsWith("http://") || href.startsWith("https://")) {
node.setAttribute("target", "_blank"); // Recover the canonical /s/{slug}/p/{slugId} path if the model wrapped it
node.setAttribute("rel", "noopener noreferrer"); // 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");
} }
}); });