html - RMarkdown html _site.yml navbar href 链接,在新选项卡中打开 target="_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
解决方案
基本上最好的做法是创建一个自动添加target="_blank"
到任何外部链接的 js 代码。并考虑到生成的内部链接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>
在 _site.yml 中包含 include_footer
output:
html_document:
includes:
after_body: include_footer.html
推荐阅读
- pyspark - 什么是 Apache Spark (SQL) 中的催化剂优化器?
- javascript - 如何避免 thunk 中对象的突变
- javascript - 从 Web 解析 XML 并在 google map api 上添加多个标记
- spring - Spring批处理不释放内存(内存不足问题)
- python - 如何集成和使用复杂结构的 GIT 存储库同步并存储在 PYTHONPATH 的文件夹中
- javascript - 正则表达式数字格式
- react-native - 向上滚动屏幕时如何隐藏底部标签导航器?
- ios - 将字符串数组附加到正文的 Swift 多部分/表单数据上传(问题:数组作为字符串到服务器)
- python - 在图中水平移动点
- node.js - 如何在 nodejs /es2017 中访问窗口或文档对象