From b6a931b96e1422ec5ab2adc3b82f07aa16bfe128 Mon Sep 17 00:00:00 2001 From: DingJunyao Date: Wed, 27 Aug 2025 15:32:01 +0800 Subject: [PATCH 1/3] New Feature: exclude_hosts --- open_in_new_tab/js/open_in_new_tab.js | 5 ++++- open_in_new_tab/plugin.py | 13 ++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) 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 9bd0550..197e8b9 100644 --- a/open_in_new_tab/js/open_in_new_tab.js +++ b/open_in_new_tab/js/open_in_new_tab.js @@ -3,9 +3,12 @@ // 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) { + if(b.getAttribute("href") && b.host !== location.host && !exclude_hosts.includes(b.host)) { b.target = "_blank"; b.rel = "noopener"; } diff --git a/open_in_new_tab/plugin.py b/open_in_new_tab/plugin.py index 4aaddb4..b3ec2d9 100644 --- a/open_in_new_tab/plugin.py +++ b/open_in_new_tab/plugin.py @@ -15,6 +15,7 @@ class OpenInNewTabPluginConfig(Config): add_icon = Type(bool, default=False) + exclude_hosts = Type(list, default=[]) class OpenInNewTabPlugin(BasePlugin[OpenInNewTabPluginConfig]): @@ -36,7 +37,17 @@ def on_post_build(self, config): See https://www.mkdocs.org/user-guide/plugins/#on_post_build. """ site_dir = Path(config["site_dir"]) - self.copy_asset("js/open_in_new_tab.js", site_dir) + js_asset_path = "js/open_in_new_tab.js" + self.copy_asset(js_asset_path, site_dir) + if self.config.exclude_hosts: + dest_path = site_dir / js_asset_path + exclude_hosts_str = ", ".join([f'"{i}"' for i in self.config.exclude_hosts]) + dest_path.write_text( + dest_path.read_text().replace( + "const exclude_hosts = [];", + f"const exclude_hosts = [{exclude_hosts_str}];", + ) + ) if self.config.add_icon: self.copy_asset("css/open_in_new_tab.css", site_dir) From 9ab0bb8339b2143fc72196e50784a28f969904bc Mon Sep 17 00:00:00 2001 From: DingJunyao Date: Wed, 27 Aug 2025 15:32:40 +0800 Subject: [PATCH 2/3] add encoding in open() to fix setup err --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 0589c4d..3c4540c 100755 --- a/setup.py +++ b/setup.py @@ -2,19 +2,19 @@ def readme(): - with open("README.md") as f: + with open("README.md", encoding="utf-8") as f: return f.read() def import_requirements(): """Imports requirements from requirements.txt file.""" - with open("requirements.txt") as f: + with open("requirements.txt", encoding="utf-8") as f: return f.read().splitlines() def import_dev_requirements(): """Imports requirements from devdeps.txt file.""" - with open("devdeps.txt") as f: + with open("devdeps.txt", encoding="utf-8") as f: return f.read().splitlines() From 2d6bcdcb2e5f320d46df1d45a5870621fca57829 Mon Sep 17 00:00:00 2001 From: DingJunyao Date: Wed, 27 Aug 2025 15:42:14 +0800 Subject: [PATCH 3/3] add exclude_hosts conf in readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8c79167..c7be3d3 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ The plugin supports the following configuration option: - `add_icon:` (default: false) - If set to true, the plugin will add an icon next to external links. +- `exclude_hosts` (list, default: empty) + - If the host of the link is included in the list, it will not be treated as an external link. ## Testing