🔨 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
Provenance
- Commit
3d834e2- Tree
a202a01- Author
Sebastián Ramírez <tiangolo@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
19bd650
2 files changed+91 added−11 removed
modified:docs/css/custom.css+62 added-10 removed
25 unmodified lines26272829303132333430313233343536373837383940413940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989939 unmodified lines25 unmodified lines.termy .linenos { display: none;}
a.external-link::after { /* \00A0 is a non-breaking space to make the mark be on the same line as the link */ content: "\00A0[↪]";/* External links: detected by JS comparing origin to site origin JS sets data-external-link on links pointing outside the site Skip image links, .no-link-icon, and .announce-link */a[data-external-link]:not(:has(img)):not(.no-link-icon):not(.announce-link) { /* For right to left languages */ direction: ltr; display: inline-block;}
a.internal-link::after { /* \00A0 is a non-breaking space to make the mark be on the same line as the link */ content: "\00A0↪";a[data-external-link]:not(:has(img)):not(.no-link-icon):not(.announce-link)::after { content: ""; display: inline-block; width: 0.75em; height: 0.75em; margin-left: 0.25em; vertical-align: middle; opacity: 0.55; background: currentColor; -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6'/%3E%3Cpolyline points='15 3 21 3 21 9'/%3E%3Cline x1='10' y1='14' x2='21' y2='3'/%3E%3C/svg%3E"); -webkit-mask-size: contain; -webkit-mask-repeat: no-repeat; mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6'/%3E%3Cpolyline points='15 3 21 3 21 9'/%3E%3Cline x1='10' y1='14' x2='21' y2='3'/%3E%3C/svg%3E"); mask-size: contain; mask-repeat: no-repeat;}
a[data-external-link]:not(:has(img)):not(.no-link-icon):not(.announce-link):hover::after { opacity: 0.85;}
/* Internal links opening in new tab: same-origin links with target=_blank JS sets data-internal-link on links pointing to the same site origin Skip image links, .no-link-icon, and .announce-link */a[data-internal-link][target="_blank"]:not(:has(img)):not(.no-link-icon):not(.announce-link) { /* For right to left languages */ direction: ltr; display: inline-block;}
a[data-internal-link][target="_blank"]:not(:has(img)):not(.no-link-icon):not(.announce-link)::after { content: ""; display: inline-block; width: 0.75em; height: 0.75em; margin-left: 0.25em; vertical-align: middle; opacity: 0.55; background: currentColor; -webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='7' width='14' height='14' rx='2'/%3E%3Cpath d='M7 3h14v14'/%3E%3C/svg%3E"); -webkit-mask-size: contain; -webkit-mask-repeat: no-repeat; mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='currentColor' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='7' width='14' height='14' rx='2'/%3E%3Cpath d='M7 3h14v14'/%3E%3C/svg%3E"); mask-size: contain; mask-repeat: no-repeat;}
a[data-internal-link][target="_blank"]:not(:has(img)):not(.no-link-icon):not(.announce-link):hover::after { opacity: 0.85;}
/* Disable link icons in footer and header nav */.md-footer a::after,.md-header a::after { content: none !important;}
.shadow { box-shadow: 5px 5px 10px #999;}
39 unmodified linesmodified:docs/js/custom.js+29 added-1 removed
107 unmodified lines108 createTermynals();109 loadVisibleTermynals();110 }111 112+function 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+}138+139 async function main() {113- setupTermynal()140+ setupTermynal();141+ openLinksInNewTab();142 }143 144 document$.subscribe(() => {145 main()1 unmodified line