Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion open_in_new_tab/js/open_in_new_tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
13 changes: 12 additions & 1 deletion open_in_new_tab/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

class OpenInNewTabPluginConfig(Config):
add_icon = Type(bool, default=False)
exclude_hosts = Type(list, default=[])


class OpenInNewTabPlugin(BasePlugin[OpenInNewTabPluginConfig]):
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down