UNPKG

@backstage/plugin-techdocs

Version:

The Backstage plugin that renders technical documentation for your components

38 lines (36 loc) 1.18 kB
const rewriteDocLinks = () => { return (dom) => { const updateDom = (list, attributeName) => { Array.from(list).filter((elem) => elem.hasAttribute(attributeName)).forEach((elem) => { const elemAttribute = elem.getAttribute(attributeName); if (elemAttribute) { if (elemAttribute.match(/^https?:\/\//i)) { elem.setAttribute("target", "_blank"); } try { const normalizedWindowLocation = normalizeUrl( window.location.href ); elem.setAttribute( attributeName, new URL(elemAttribute, normalizedWindowLocation).toString() ); } catch (_e) { elem.replaceWith(elem.textContent || elemAttribute); } } }); }; updateDom(Array.from(dom.getElementsByTagName("a")), "href"); return dom; }; }; function normalizeUrl(input) { const url = new URL(input); if (!url.pathname.endsWith("/") && !url.pathname.endsWith(".html")) { url.pathname += "/"; } return url.toString(); } export { normalizeUrl, rewriteDocLinks }; //# sourceMappingURL=rewriteDocLinks.esm.js.map