agent-zero/webui/js/html-links.js
Alessandro c2e14b6cd1 sanitize plugin markdown rendering with shared helper
Add a shared safe markdown pipeline for plugin READMEs and docs.

- vendor DOMPurify and introduce a shared safe-markdown helper
- centralize GitHub README link/image rebasing, including repo routes like `releases`
- sanitize rendered HTML before all plugin-related x-html sinks
- apply the shared renderer to Plugin Hub README, installed plugin README, and markdown modal docs
- preserve target/rel handling for external links
2026-03-28 19:29:26 +01:00

27 lines
777 B
JavaScript

export function addBlankTargetsToLinks(str) {
const doc = new DOMParser().parseFromString(str, "text/html");
doc.querySelectorAll("a").forEach((anchor) => {
const href = anchor.getAttribute("href") || "";
if (
href.startsWith("#") ||
href.trim().toLowerCase().startsWith("javascript")
) {
return;
}
if (
!anchor.hasAttribute("target") ||
anchor.getAttribute("target") === ""
) {
anchor.setAttribute("target", "_blank");
}
const rel = (anchor.getAttribute("rel") || "").split(/\s+/).filter(Boolean);
if (!rel.includes("noopener")) rel.push("noopener");
if (!rel.includes("noreferrer")) rel.push("noreferrer");
anchor.setAttribute("rel", rel.join(" "));
});
return doc.body.innerHTML;
}