有没有办法在Markdown中创建一个在新窗口中打开的链接?如果不是,您建议使用什么语法来完成此操作?我将把它添加到我使用的markdown编译器中。我认为这应该是一个选择。
当前回答
在我的项目中,我这样做,它工作得很好:
[Link](https://example.org/ "title" target="_blank")
Link
但并非所有解析器都允许这样做。
其他回答
幽灵降价使用:
[Google](https://google.com" target="_blank)
在这里找到它: https://cmatskas.com/open-external-links-in-a-new-window-ghost/
您可以使用{[attr]="[prop]"}添加任何属性
例如[谷歌](http://www.google.com){target="_blank"}
我不认为有降价功能,尽管如果你想用JavaScript自动打开指向你自己网站以外的链接,可能有其他可用的选项。
Array.from(javascript.links)
.filter(link => link.hostname != window.location.hostname)
.forEach(link => link.target = '_blank');
jsFiddle。
如果你正在使用jQuery:
$(document.links).filter(function() {
return this.hostname != window.location.hostname;
}).attr('target', '_blank');
jsFiddle。
使用MUI v5“markdown-to-jsx”
这似乎对我很管用:
import Markdown from 'markdown-to-jsx';
...
const MarkdownLink = ({ children, ...props }) => (
<Link {...props}>{children}</Link>
);
...
<Markdown
options={{
forceBlock: true,
overrides: {
a: {
component: MarkdownLink,
props: {
target: '_blank',
},
},
},
}}
>
{description}
</Markdown>
不是一个直接的答案,但可能会帮助一些人在这里结束。
如果你正在使用GatsbyJS,有一个插件可以自动在markdown中添加target="_blank"到外部链接。
它被称为gatsby-remark-external-links,用法如下:
Yarn添加gatsby-remark-external-links
plugins: [
{
resolve: `gatsby-transformer-remark`,
options: {
plugins: [{
resolve: "gatsby-remark-external-links",
options: {
target: "_blank",
rel: "noopener noreferrer"
}
}]
}
},
它还负责rel="noopener noreferrer"。
如果需要更多选项,请参考文档。
推荐文章
- 使伸缩项目正确浮动
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 我如何用javascript编程点击链接?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?