From 408bc74029a35b90c0cf3866c920b1e406e800f2 Mon Sep 17 00:00:00 2001 From: Jakub Andrysek Date: Mon, 7 Oct 2024 21:22:49 +0200 Subject: [PATCH] Fix open new tab - different port, close #8, close #6 --- README.md | 47 ++++++++++++++----------- docs/README.md | 50 ++++++++++++++++----------- open_in_new_tab/js/open_in_new_tab.js | 46 +++++++++++++----------- open_in_new_tab/plugin.py | 2 +- setup.py | 2 +- 5 files changed, 84 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 569aebf..b1d94b1 100644 --- a/README.md +++ b/README.md @@ -61,34 +61,38 @@ Look at this source href.endsWith(ext))) { + links[eleLink].onclick = function() { + window.open(this.href); + return false; + } } } - } } function apply_rules() { @@ -97,14 +101,17 @@ function apply_rules() { } if (typeof document$ !== "undefined") { - // compatibility with mkdocs-material's instant loading feature - // based on code from https://github.com/timvink/mkdocs-charts-plugin - // Copyright (c) 2021 Tim Vink - MIT License - // fixes [Issue #2](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2) + // Compatibility with mkdocs-material's instant loading feature document$.subscribe(function() { apply_rules(); - }) + }); +} else { + // For browsers without mkdocs-material's instant loading feature + document.addEventListener("DOMContentLoaded", function() { + apply_rules(); + }); } + ```

diff --git a/docs/README.md b/docs/README.md index ed24014..75d1f49 100644 --- a/docs/README.md +++ b/docs/README.md @@ -15,6 +15,8 @@ This plugin adds JS code to open outgoing links and PDFs in a new tab. The automatic opening of links in a new tab is a common feature of modern websites. It is also a good practice for accessibility. However, it is not a default behavior of Markdown. This plugin adds a JavaScript code to your website that opens external links and PDFs in a new tab. Look at the [demo](https://newtab.kubaandrysek.cz/). +aasLook at the [demo](http://127.0.0.1:8000/). +aasLook at the [demo](http://127.0.0.1:1234/). ## Installation @@ -61,34 +63,38 @@ Look at this source
href.endsWith(ext))) { + links[eleLink].onclick = function() { + window.open(this.href); + return false; + } } } - } } function apply_rules() { @@ -97,15 +103,17 @@ function apply_rules() { } if (typeof document$ !== "undefined") { - // compatibility with mkdocs-material's instant loading feature - // based on code from https://github.com/timvink/mkdocs-charts-plugin - // Copyright (c) 2021 Tim Vink - MIT License - // fixes [Issue #2](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2) + // Compatibility with mkdocs-material's instant loading feature document$.subscribe(function() { apply_rules(); - console.log("Applying rules"); - }) + }); +} else { + // For browsers without mkdocs-material's instant loading feature + document.addEventListener("DOMContentLoaded", function() { + apply_rules(); + }); } + ```

diff --git a/open_in_new_tab/js/open_in_new_tab.js b/open_in_new_tab/js/open_in_new_tab.js index 56d786e..9bd0550 100644 --- a/open_in_new_tab/js/open_in_new_tab.js +++ b/open_in_new_tab/js/open_in_new_tab.js @@ -1,32 +1,36 @@ // Description: Open external links in a new tab and PDF links in a new tab -// Source: https://jekyllcodex.org/without-plugin/new-window-fix/ +// Based on: https://jekyllcodex.org/without-plugin/new-window-fix/ -//open external links in a new window +// Open external links in a new window function external_new_window() { - for(let c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) { + for(let c = document.getElementsByTagName("a"), a = 0; a < c.length; a++) { let b = c[a]; - if(b.getAttribute("href") && b.hostname !== location.hostname) { + if(b.getAttribute("href") && b.host !== location.host) { b.target = "_blank"; b.rel = "noopener"; } } } -//open PDF links in a new window -function pdf_new_window () -{ + +// Open PDF links in a new window +function pdf_new_window() { if (!document.getElementsByTagName) { - return false; + 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 ++) { - if ((links[eleLink].href.indexOf('.pdf') !== -1)||(links[eleLink].href.indexOf('.doc') !== -1)||(links[eleLink].href.indexOf('.json') !== -1)||(links[eleLink].href.indexOf('.docx') !== -1)) { - links[eleLink].onclick = - function() { - window.open(this.href); - return false; + + 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() { @@ -35,11 +39,13 @@ function apply_rules() { } if (typeof document$ !== "undefined") { - // compatibility with mkdocs-material's instant loading feature - // based on code from https://github.com/timvink/mkdocs-charts-plugin - // Copyright (c) 2021 Tim Vink - MIT License - // fixes [Issue #2](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2) + // Compatibility with mkdocs-material's instant loading feature document$.subscribe(function() { apply_rules(); - }) + }); +} else { + // For browsers without mkdocs-material's instant loading feature + document.addEventListener("DOMContentLoaded", function() { + apply_rules(); + }); } diff --git a/open_in_new_tab/plugin.py b/open_in_new_tab/plugin.py index ac6b9a6..8a17a75 100644 --- a/open_in_new_tab/plugin.py +++ b/open_in_new_tab/plugin.py @@ -21,7 +21,7 @@ def on_config(self, config, **kwargs): """ # Add pointer to open_in_new_tab.js file to extra_javascript # which is added to the output directory during on_post_build() event - config["extra_javascript"].append("js/open_in_new_tab.js") + config["extra_javascript"].append("/js/open_in_new_tab.js") def on_post_build(self, config): """ diff --git a/setup.py b/setup.py index 1edbf1c..bb1dd5b 100755 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ def import_dev_requirements(): # https://pypi.org/project/mkdocs-open-in-new-tab/ setup( name="mkdocs-open-in-new-tab", - version="1.0.5", + version="1.0.6", description="MkDocs plugin to open outgoing links and PDFs in new tab.", long_description=readme(), long_description_content_type="text/markdown",