我正在使用 Docusaurus,并且想在新选项卡中打开外部链接。
我更愿意使用代码来实现此目的,而不是根据此答案在我的 markdown 文档中编写 html 代码的解决方案。
我已经尝试过这个答案:
[link](url){:target="_blank"}
以及这个答案:
[Google](https://google.com" target="_blank)
我认为这篇文章可能会做我想要的,我尝试将代码添加到 _index.js,但 Docusaurus 停止构建。
(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';
}
}
})();
Run Code Online (Sandbox Code Playgroud)
我将 JavaScript 代码上传到GitHub并将其添加到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"
],
Run Code Online (Sandbox Code Playgroud)
该脚本似乎已加载,但未按预期工作。
我希望用 Markdown 编写的链接能够在新的浏览器选项卡中打开:
[the rmarkdown website](https://rmarkdown.rstudio.com) …Run Code Online (Sandbox Code Playgroud)