From bcc01dbbcba263409694f4a9d41d5256507be7e6 Mon Sep 17 00:00:00 2001 From: Florian Haas Date: Mon, 30 Nov 2026 08:53:56 +0100 Subject: [PATCH] feat: Open external links in new tab Ensure that only internal links are opened in the same tab, and a new tab opens for any link pointing to a different hostname. Mkdocs has a plugin for this, which however isn't compatible with Zensical. Thus, to ensure a smooth Zensical transition, include the Javascript from that plugin directly (as recommended in the Zensical docs). References: https://zensical.org/docs/compatibility/mkdocs/plugins/#open-in-new-tab https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/blob/main/open_in_new_tab/js/open_in_new_tab.js --- mkdocs.yml | 2 + theme/assets/js/open_in_new_tab.js | 59 ++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 theme/assets/js/open_in_new_tab.js diff --git a/mkdocs.yml b/mkdocs.yml index 7d86898fb0..8b0a6f6756 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -57,6 +57,8 @@ extra: extra_css: - assets/stylesheets/cleura-palette.css - assets/stylesheets/banner.css +extra_javascript: + - assets/js/open_in_new_tab.js markdown_extensions: - admonition - attr_list diff --git a/theme/assets/js/open_in_new_tab.js b/theme/assets/js/open_in_new_tab.js new file mode 100644 index 0000000000..6e0c6ce248 --- /dev/null +++ b/theme/assets/js/open_in_new_tab.js @@ -0,0 +1,59 @@ +// Description: Open external links in a new tab and PDF links in a new tab +// Based on: https://jekyllcodex.org/without-plugin/new-window-fix/ + +// Open external links in a new window +function external_new_window() { + + const exclude_hosts = []; + + for(let c = document.getElementsByTagName("a"), a = 0; a < c.length; a++) { + let b = c[a]; + if(b.getAttribute("href") && b.host !== location.host && !exclude_hosts.includes(b.host)) { + b.target = "_blank"; + b.rel = "noopener"; + } + } +} + +// Open PDF links in a new window +function pdf_new_window() { + if (!document.getElementsByTagName) { + return false; + } + + const extensions = ['.pdf', '.doc', '.docx', '.json', '.xls', '.xlsx', '.ppt', '.pptx', '.zip', '.rar', '.tar', '.gz', '.7z', '.bz2', '.xz', '.tgz', '.tar.gz']; + let links = document.getElementsByTagName("a"); + + for (let eleLink = 0; eleLink < links.length; eleLink++) { + let href = links[eleLink].href.toLowerCase(); // Convert href to lowercase for case-insensitive matching + + if (extensions.some(ext => href.endsWith(ext))) { + links[eleLink].onclick = function() { + window.open(this.href); + return false; + } + } + } +} + +function apply_rules() { + external_new_window(); + pdf_new_window(); +} + +if (typeof document$ !== "undefined") { + // Compatibility with mkdocs-material's instant loading feature + document$.subscribe(function() { + apply_rules(); + }); +} else if (typeof InstantClick !== "undefined") { + // Compatibility with InstantClick.js + InstantClick.on('change', function() { + apply_rules(); + }); +} else { + // For browsers without mkdocs-material's instant loading feature + document.addEventListener("DOMContentLoaded", function() { + apply_rules(); + }); +}