Skip to content

Commit e5a7ddb

Browse files
committed
Update markdown_controller.js
Don't handle links as external by default. Instead distiguish internal (relative) and external (absolute) links.
1 parent b8d5b83 commit e5a7ddb

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

assets/controllers/common/markdown_controller.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ export default class MarkdownController extends Controller {
5656
this.element.innerHTML = DOMPurify.sanitize(MarkdownController._marked.parse(this.unescapeHTML(raw)));
5757

5858
for(let a of this.element.querySelectorAll('a')) {
59-
//Mark all links as external
60-
a.classList.add('link-external');
61-
//Open links in new tag
62-
a.setAttribute('target', '_blank');
63-
//Dont track
64-
a.setAttribute('rel', 'noopener');
59+
// test if link is absolute
60+
var r = new RegExp('^(?:[a-z+]+:)?//', 'i');
61+
if (r.test(a.getAttribute('href'))) {
62+
//Mark all links as external
63+
a.classList.add('link-external');
64+
//Open links in new tag
65+
a.setAttribute('target', '_blank');
66+
//Dont track
67+
a.setAttribute('rel', 'noopener');
68+
}
6569
}
6670

6771
//Apply bootstrap styles to tables
@@ -108,4 +112,4 @@ export default class MarkdownController extends Controller {
108112
gfm: true,
109113
});
110114
}*/
111-
}
115+
}

0 commit comments

Comments
 (0)