Skip to content
Myr

🔨 Handle external links `target=_blank` and CSS automatically in JS and CSS (#1622)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sebastián Ramírez · committed by GitHub · parent 19bd650 · Browse files · 2 files+91 added−11 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+91 added−11 removed

  • other:+91 added−11 removed

2 files+91 added−11 removed

modified:docs/css/custom.css+62 added-10 removed
modified:docs/js/custom.js+29 added-1 removed
107 unmodified lines
108 createTermynals();109 loadVisibleTermynals();110}111112function openLinksInNewTab() {113 const siteUrl = document.querySelector("link[rel='canonical']")?.href114 || window.location.origin;115 const siteOrigin = new URL(siteUrl).origin;116 document.querySelectorAll(".md-content a[href]").forEach(a => {117 if (a.getAttribute("target") === "_self") return;118 const href = a.getAttribute("href");119 if (!href) return;120 try {121 const url = new URL(href, window.location.href);122 // Skip same-page anchor links (only the hash differs)123 if (url.origin === window.location.origin124 && url.pathname === window.location.pathname125 && url.search === window.location.search) return;126 if (!a.hasAttribute("target")) {127 a.setAttribute("target", "_blank");128 a.setAttribute("rel", "noopener");129 }130 if (url.origin !== siteOrigin) {131 a.dataset.externalLink = "";132 } else {133 a.dataset.internalLink = "";134 }135 } catch (_) {}136 });137}138139async function main() {113 setupTermynal()140 setupTermynal();141 openLinksInNewTab();142}143144document$.subscribe(() => {145 main()
1 unmodified line