I am using Docusaurus and would like to open external links in new tabs.
I would prefer to use code for this rather than the solution of writing html code in my markdown document as per this answer.
I have tried this answer:
[link](url){:target="_blank"}
As well as this answer:
[Google](https://google.com" target="_blank)
I think this post would probably do what I want, I tried adding the code to _index.js, but Docusaurus stopped building.
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
I uploaded the JavaScript code to GitHub and added it to siteConfig.js
:
// Add custom scripts here that would be placed in <script> tags.
scripts: [
"https://buttons.github.io/buttons.js",
"https://msdcanderson.github.io/docusaurus-open-link-new-tab.github.io/open-link-new-tab.js"
],
The script seems to load, but doesn't work as expected.
I expect a link written in Markdown to open in a new browser tab:
[the rmarkdown website](https://rmarkdown.rstudio.com)
Any ideas would be great - thanks