Update markdown_controller.js

Don't handle links as external by default. Instead distiguish internal (relative) and external (absolute) links.
This commit is contained in:
d-buchmann 2025-05-21 16:47:09 +02:00
parent b8d5b83eee
commit e5a7ddb833

View file

@ -56,12 +56,16 @@ export default class MarkdownController extends Controller {
this.element.innerHTML = DOMPurify.sanitize(MarkdownController._marked.parse(this.unescapeHTML(raw))); this.element.innerHTML = DOMPurify.sanitize(MarkdownController._marked.parse(this.unescapeHTML(raw)));
for(let a of this.element.querySelectorAll('a')) { for(let a of this.element.querySelectorAll('a')) {
//Mark all links as external // test if link is absolute
a.classList.add('link-external'); var r = new RegExp('^(?:[a-z+]+:)?//', 'i');
//Open links in new tag if (r.test(a.getAttribute('href'))) {
a.setAttribute('target', '_blank'); //Mark all links as external
//Dont track a.classList.add('link-external');
a.setAttribute('rel', 'noopener'); //Open links in new tag
a.setAttribute('target', '_blank');
//Dont track
a.setAttribute('rel', 'noopener');
}
} }
//Apply bootstrap styles to tables //Apply bootstrap styles to tables
@ -108,4 +112,4 @@ export default class MarkdownController extends Controller {
gfm: true, gfm: true,
}); });
}*/ }*/
} }