mirror of
https://github.com/docmost/docmost.git
synced 2026-05-22 01:32:55 +08:00
fix: internal links
This commit is contained in:
@@ -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");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user