Skip to content

Commit

Permalink
Fix open new tab - different port, close #8, close #6
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubAndrysek committed Oct 7, 2024
1 parent d853bc9 commit 408bc74
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 63 deletions.
47 changes: 27 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,38 @@ Look at this source <a href="https://github.com/JakubAndrysek/mkdocs-open-in-new

```js
// 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('.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() {
Expand All @@ -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();
});
}
```
</p>
</details>
Expand Down
50 changes: 29 additions & 21 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -61,34 +63,38 @@ Look at this source <a href="https://github.com/JakubAndrysek/mkdocs-open-in-new

```js
// 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('.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() {
Expand All @@ -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();
});
}
```
</p>
</details>
Expand Down
46 changes: 26 additions & 20 deletions open_in_new_tab/js/open_in_new_tab.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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();
});
}
2 changes: 1 addition & 1 deletion open_in_new_tab/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 408bc74

Please sign in to comment.