Skip to content

Commit e3ed8e2

Browse files
Merge pull request #3 from JakubAndrysek/mkdocs-instant
2 parents 6911f32 + 7109e50 commit e3ed8e2

11 files changed

+101
-36
lines changed

.idea/mkdocs-open-in-new-tab.iml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+21-11
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<img src="https://img.shields.io/github/forks/JakubAndrysek/mkdocs-open-in-new-tab?style=flat-square">
99
<img src="https://img.shields.io/github/issues/JakubAndrysek/mkdocs-open-in-new-tab?style=flat-square">
1010
<img src="https://static.pepy.tech/personalized-badge/mkdocs-open-in-new-tab?period=month&units=international_system&left_color=black&right_color=orange&left_text=Downloads">
11-
12-
1311
</p>
1412

1513
This plugin adds JS code to open outgoing links and PDFs in a new tab.
@@ -67,8 +65,8 @@ Look at this source <a href="https://github.com/JakubAndrysek/mkdocs-open-in-new
6765
6866
//open external links in a new window
6967
function external_new_window() {
70-
for(var c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) {
71-
var b = c[a];
68+
for(let c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) {
69+
let b = c[a];
7270
if(b.getAttribute("href") && b.hostname !== location.hostname) {
7371
b.target = "_blank";
7472
b.rel = "noopener";
@@ -78,9 +76,11 @@ function external_new_window() {
7876
//open PDF links in a new window
7977
function pdf_new_window ()
8078
{
81-
if (!document.getElementsByTagName) return false;
82-
var links = document.getElementsByTagName("a");
83-
for (var eleLink=0; eleLink < links.length; eleLink ++) {
79+
if (!document.getElementsByTagName) {
80+
return false;
81+
}
82+
let links = document.getElementsByTagName("a");
83+
for (let eleLink=0; eleLink < links.length; eleLink ++) {
8484
if ((links[eleLink].href.indexOf('.pdf') !== -1)||(links[eleLink].href.indexOf('.doc') !== -1)||(links[eleLink].href.indexOf('.docx') !== -1)) {
8585
links[eleLink].onclick =
8686
function() {
@@ -91,16 +91,26 @@ function pdf_new_window ()
9191
}
9292
}
9393
94-
window.addEventListener("DOMContentLoaded", function() {
94+
function apply_rules() {
9595
external_new_window();
9696
pdf_new_window();
97-
});
97+
}
98+
99+
if (typeof document$ !== "undefined") {
100+
// compatibility with mkdocs-material's instant loading feature
101+
// based on code from https://github.com/timvink/mkdocs-charts-plugin
102+
// Copyright (c) 2021 Tim Vink - MIT License
103+
// fixes [Issue #2](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2)
104+
document$.subscribe(function() {
105+
apply_rules();
106+
})
107+
}
98108
```
99109
</p>
100110
</details>
101111

102-
## Known issues
103-
This extension does not work with mkdocs-material [navigation.instant](https://squidfunk.github.io/mkdocs-material/setup/setting-up-navigation/#instant-loading). JS could not be loaded when the page is loaded instantly. If you know how to fix it, please let me know. Issue is [here](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2).
112+
<!-- ## Known issues
113+
This extension does not work with mkdocs-material [navigation.instant](https://squidfunk.github.io/mkdocs-material/setup/setting-up-navigation/#instant-loading). JS could not be loaded when the page is loaded instantly. If you know how to fix it, please let me know. Issue is [here](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2). -->
104114

105115
## License
106116

RelativeLink.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Relative Link
22

3-
This is a relative link back to the [README](README.md).
3+
This is a relative link back to the [README](README.md).
4+
5+
This is outgoing link to [Github](https://github.com/).

docs/README.md

+22-11
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
<img src="https://img.shields.io/github/forks/JakubAndrysek/mkdocs-open-in-new-tab?style=flat-square">
99
<img src="https://img.shields.io/github/issues/JakubAndrysek/mkdocs-open-in-new-tab?style=flat-square">
1010
<img src="https://static.pepy.tech/personalized-badge/mkdocs-open-in-new-tab?period=month&units=international_system&left_color=black&right_color=orange&left_text=Downloads">
11-
12-
1311
</p>
1412

1513
This plugin adds JS code to open outgoing links and PDFs in a new tab.
@@ -67,8 +65,8 @@ Look at this source <a href="https://github.com/JakubAndrysek/mkdocs-open-in-new
6765
6866
//open external links in a new window
6967
function external_new_window() {
70-
for(var c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) {
71-
var b = c[a];
68+
for(let c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) {
69+
let b = c[a];
7270
if(b.getAttribute("href") && b.hostname !== location.hostname) {
7371
b.target = "_blank";
7472
b.rel = "noopener";
@@ -78,9 +76,11 @@ function external_new_window() {
7876
//open PDF links in a new window
7977
function pdf_new_window ()
8078
{
81-
if (!document.getElementsByTagName) return false;
82-
var links = document.getElementsByTagName("a");
83-
for (var eleLink=0; eleLink < links.length; eleLink ++) {
79+
if (!document.getElementsByTagName) {
80+
return false;
81+
}
82+
let links = document.getElementsByTagName("a");
83+
for (let eleLink=0; eleLink < links.length; eleLink ++) {
8484
if ((links[eleLink].href.indexOf('.pdf') !== -1)||(links[eleLink].href.indexOf('.doc') !== -1)||(links[eleLink].href.indexOf('.docx') !== -1)) {
8585
links[eleLink].onclick =
8686
function() {
@@ -91,16 +91,27 @@ function pdf_new_window ()
9191
}
9292
}
9393
94-
window.addEventListener("DOMContentLoaded", function() {
94+
function apply_rules() {
9595
external_new_window();
9696
pdf_new_window();
97-
});
97+
}
98+
99+
if (typeof document$ !== "undefined") {
100+
// compatibility with mkdocs-material's instant loading feature
101+
// based on code from https://github.com/timvink/mkdocs-charts-plugin
102+
// Copyright (c) 2021 Tim Vink - MIT License
103+
// fixes [Issue #2](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2)
104+
document$.subscribe(function() {
105+
apply_rules();
106+
console.log("Applying rules");
107+
})
108+
}
98109
```
99110
</p>
100111
</details>
101112

102-
## Known issues
103-
This extension does not work with mkdocs-material [navigation.instant](https://squidfunk.github.io/mkdocs-material/setup/setting-up-navigation/#instant-loading). JS could not be loaded when the page is loaded instantly. If you know how to fix it, please let me know. Issue is [here](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2).
113+
<!-- ## Known issues
114+
This extension does not work with mkdocs-material [navigation.instant](https://squidfunk.github.io/mkdocs-material/setup/setting-up-navigation/#instant-loading). JS could not be loaded when the page is loaded instantly. If you know how to fix it, please let me know. Issue is [here](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2). -->
104115

105116
## License
106117

docs/RelativeLink.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
# Relative Link
22

3-
This is a relative link back to the [README](README.md).
3+
This is a relative link back to the [README](README.md).
4+
5+
This is outgoing link to [Github](https://github.com/).

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ theme:
2626
- navigation.tracking
2727
- navigation.tabs
2828
- navigation.expand
29+
# - navigation.instant for testing purposes
2930
icon:
3031
repo: fontawesome/brands/github
3132

open_in_new_tab/js/open_in_new_tab.js

+19-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
//open external links in a new window
55
function external_new_window() {
6-
for(var c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) {
7-
var b = c[a];
6+
for(let c = document.getElementsByTagName("a"), a = 0;a < c.length;a++) {
7+
let b = c[a];
88
if(b.getAttribute("href") && b.hostname !== location.hostname) {
99
b.target = "_blank";
1010
b.rel = "noopener";
@@ -14,9 +14,11 @@ function external_new_window() {
1414
//open PDF links in a new window
1515
function pdf_new_window ()
1616
{
17-
if (!document.getElementsByTagName) return false;
18-
var links = document.getElementsByTagName("a");
19-
for (var eleLink=0; eleLink < links.length; eleLink ++) {
17+
if (!document.getElementsByTagName) {
18+
return false;
19+
}
20+
let links = document.getElementsByTagName("a");
21+
for (let eleLink=0; eleLink < links.length; eleLink ++) {
2022
if ((links[eleLink].href.indexOf('.pdf') !== -1)||(links[eleLink].href.indexOf('.doc') !== -1)||(links[eleLink].href.indexOf('.docx') !== -1)) {
2123
links[eleLink].onclick =
2224
function() {
@@ -27,7 +29,17 @@ function pdf_new_window ()
2729
}
2830
}
2931

30-
window.addEventListener("DOMContentLoaded", function() {
32+
function apply_rules() {
3133
external_new_window();
3234
pdf_new_window();
33-
});
35+
}
36+
37+
if (typeof document$ !== "undefined") {
38+
// compatibility with mkdocs-material's instant loading feature
39+
// based on code from https://github.com/timvink/mkdocs-charts-plugin
40+
// Copyright (c) 2021 Tim Vink - MIT License
41+
// fixes [Issue #2](https://github.com/JakubAndrysek/mkdocs-open-in-new-tab/issues/2)
42+
document$.subscribe(function() {
43+
apply_rules();
44+
})
45+
}

open_in_new_tab/plugin.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
# PyPI: https://pypi.org/project/mkdocs-open-in-new-tab/
77
# Inspired by: https://github.com/timvink/mkdocs-charts-plugin/tree/main
88

9-
import open_in_new_tab
109
from mkdocs.plugins import BasePlugin
1110
from mkdocs.utils import copy_file
1211
import os
1312

1413
HERE = os.path.dirname(os.path.abspath(__file__))
1514

15+
1616
class OpenInNewTabPlugin(BasePlugin):
1717
def on_config(self, config, **kwargs):
1818
"""
@@ -23,8 +23,6 @@ def on_config(self, config, **kwargs):
2323
# which is added to the output directory during on_post_build() event
2424
config["extra_javascript"].append("js/open_in_new_tab.js")
2525

26-
27-
2826
def on_post_build(self, config):
2927
"""
3028
Event trigger on post build.
@@ -37,4 +35,4 @@ def on_post_build(self, config):
3735
copy_file(
3836
os.path.join(os.path.join(package, "js"), "open_in_new_tab.js"),
3937
js_file_path,
40-
)
38+
)

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def readme():
88
# https://pypi.org/project/mkdocs-open-in-new-tab/
99
setup(
1010
name='mkdocs-open-in-new-tab',
11-
version='1.0.2',
11+
version='1.0.3',
1212
description='MkDocs plugin to open outgoing links and PDFs in new tab.',
1313
long_description=readme(),
1414
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)