r语言 - RMarkdown html _site.yml 导航栏 href 链接,在新选项卡中打开,目标= "_blank"



与r markdown html _site.yml页面挣扎。在我的导航栏中,我有HREF指向不同的网站链接,我无法弄清楚我们的位置包括

target="_blank"

语句因此在新选项卡中打开链接。HREF链接不遵循rmarkDowns _site.yml文件中的普通HTML格式。

    - text: "TWITTER"
      icon: fa-twitter
      href: https://twitter.com
    - text: "GITHUB"
      icon: fa-github
      href: https://github.com
    - text: "SLACK"
      icon: fa-slack
      href: https://slack.com

基本上最好的操作是创建一个JS代码,该代码自动将target="_blank"添加到任何外部链接中。并考虑到rmarkdown生成的内部链接是相对的事实。我们可以轻松地创建一个正则表达式,该表达式测试外部链接的存在,然后如果找到附加target="_blank"

首先创建:links.js

(function() {
  for (const link of document.getElementsByTagName('a')) {
    if (/^(https?:)?///.test(link.getAttribute('href'))) link.target = '_blank';
  }
})();

将脚本标签添加到include_footer.html

<script type="text/javascript" src="links.js"></script>

include include_footer in _site.yml

output:
  html_document:
    includes:
      after_body: include_footer.html

您可以为每个这样的页面创建一个重定向页面:

    - text: "TWITTER"
  icon: fa-twitter
  href: twitter_redirect.html

,然后为twitter_redirect.html(etc(,使用window.popen。

<!DOCTYPE html>
<html>
<head>
</head>
<body onload="myFunction()">
  <script>
    function myFunction() {
    window.open('https://twitter.com/', '_blank');
    window.history.back();
    }
  </script>
</body>

从@abdessabour mtk,

参考JScode
---  
title: "BookMark"  
output: html_document  
---  
```{r setup, include=FALSE}  
knitr::opts_chunk$set(echo = TRUE)  
library(knitr)  
```  
[google](https://www.google.com)  
...
```{js, echo=FALSE}  
(function() {  
  for (const link of document.getElementsByTagName('a')) {  
    if (/^(https?:)?///.test(link.getAttribute('href'))) link.target = '_blank';  
  }  
})();  
```