Html 我可以在 Markdown 中创建带有 'target="_blank"' 的链接吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4425198/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I create links with 'target="_blank"' in Markdown?
提问by ma11hew28
Is there a way to create a link in Markdown that opens in a new window? If not, what syntax do you recommend to do this. I'll add it to the markdown compiler I use. I think it should be an option.
有没有办法在 Markdown 中创建一个在新窗口中打开的链接?如果没有,您建议使用什么语法来执行此操作。我会将它添加到我使用的 Markdown 编译器中。我认为这应该是一个选择。
采纳答案by Matchu
As far as the Markdown syntax is concerned, if you want to get that detailed, you'll just have to use HTML.
就 Markdown 语法而言,如果您想获得详细信息,您只需要使用 HTML。
<a href="http://example.com/" target="_blank">Hello, world!</a>
Most Markdown engines I've seen allow plain old HTML, just for situations like this where a generic text markup system just won't cut it. (The StackOverflow engine, for example.) They then run the entire output through an HTML whitelist filter, regardless, since even a Markdown-only document can easily contain XSS attacks. As such, if you or your users want to create _blank
links, then they probably still can.
我见过的大多数 Markdown 引擎都允许使用普通的旧 HTML,只是在这种情况下,通用文本标记系统不会削减它。(例如 StackOverflow 引擎。)然后他们通过 HTML 白名单过滤器运行整个输出,无论如何,因为即使是仅 Markdown 的文档也很容易包含 XSS 攻击。因此,如果您或您的用户想要创建_blank
链接,那么他们可能仍然可以。
If that's a feature you're going to be using often, it might make sense to create your own syntax, but it's generally not a vital feature. If I want to launch that link in a new window, I'll ctrl-click it myself, thanks.
如果这是您将经常使用的功能,那么创建您自己的语法可能是有意义的,但它通常不是一个重要的功能。如果我想在新窗口中启动该链接,我会自己按住 ctrl 单击它,谢谢。
回答by farnoy
回答by alex
I don't think there is a markdown feature.
我不认为有降价功能。
Though there may be other options available if you want to open links which point outside your own site automatically with JavaScript.
如果您想使用 JavaScript 打开指向您自己网站之外的链接,可能还有其他选项可用。
var links = document.links;
for (var i = 0, linksLength = links.length; i < linksLength; i++) {
if (links[i].hostname != window.location.hostname) {
links[i].target = '_blank';
}
}
If you're using jQuery it's a tad simpler...
如果你使用 jQuery,它会更简单一点......
$(document.links).filter(function() {
return this.hostname != window.location.hostname;
}).attr('target', '_blank');
回答by meduvigo
With Markdown-2.5.2, you can use this:
使用 Markdown-2.5.2,你可以使用这个:
[link](url){:target="_blank"}
回答by Peter Danshov
I'm using Grav CMS and this works perfectly:
我正在使用 Grav CMS,这非常有效:
Body/Content:Some text[1]
正文/内容:Some text[1]
Body/Reference:[1]: http://somelink.com/?target=_blank
正文/参考:[1]: http://somelink.com/?target=_blank
Just make sure that the target attribute is passed first, if there are additional attributes in the link, copy/paste them to the end of the reference URL.
只需确保首先传递目标属性,如果链接中有其他属性,请将它们复制/粘贴到引用 URL 的末尾。
Also work as direct link:[Go to this page](http://somelink.com/?target=_blank)
也可用作直接链接:[Go to this page](http://somelink.com/?target=_blank)
回答by aholub
One global solution is to put <base target="_blank">
into your page's <head>
element. That effectively adds a default target to every anchor element. I use markdown to create content on my Wordpress-based web site, and my theme customizer will let me inject that code into the top of every page. If your theme doesn't do that, there's a plug-in
一种全局解决方案是放入<base target="_blank">
页面的<head>
元素中。这有效地为每个锚元素添加了一个默认目标。我使用 Markdown 在基于 Wordpress 的网站上创建内容,我的主题定制器将让我将该代码注入每个页面的顶部。如果您的主题不这样做,则有一个插件
回答by coatless
So, it isn't quite true that you cannot add link attributes to a Markdown URL. To add attributes, check with the underlying markdown parser being used and what their extensions are.
因此,不能将链接属性添加到 Markdown URL 是不正确的。要添加属性,请检查正在使用的底层 Markdown 解析器及其扩展名。
In particular, pandoc
has an extension to enable link_attributes
, which allow markup in the link. e.g.
特别是,pandoc
具有 enable 的扩展link_attributes
,它允许在链接中进行标记。例如
[Hello, world!](http://example.com/){target="_blank"}
- For those coming from R(e.g. using
rmarkdown
,bookdown
,blogdown
and so on), this is the syntax you want. - For those not using R, you may need to enable the extensionin the call to
pandoc
with+link_attributes
- 对于那些从未来[R (例如,使用
rmarkdown
,bookdown
,blogdown
等),这是你想要的语法。 - 对于那些不使用[R ,你可能需要启用扩展在调用
pandoc
与+link_attributes
Note:This is different than the kramdown
parser's support, which is one the accepted answers above. In particular, note that kramdown differs from pandoc since it requires a colon -- :
-- at the start of the curly brackets -- {}
, e.g.
注意:这与kramdown
解析器的支持不同,后者是上述公认的答案之一。特别要注意,kramdown 与 pandoc 不同,因为它需要一个冒号 -- :
-- 在大括号的开头 -- {}
,例如
[link](http://example.com){:hreflang="de"}
In particular:
特别是:
# Pandoc
{ attribute1="value1" attribute2="value2"}
# Kramdown
{: attribute1="value1" attribute2="value2"}
^
^ Colon
回答by guya
For ghost markdown use:
对于幽灵降价使用:
[Google](https://google.com" target="_blank)
Found it here: https://cmatskas.com/open-external-links-in-a-new-window-ghost/
在这里找到:https: //cmatskas.com/open-external-links-in-a-new-window-ghost/
回答by user5036455
回答by midudev
There's no easy way to do it, and like @alex has noted you'll need to use JavaScript. His answer is the best solution but in order to optimize it, you might want to filter only to the post-content links.
没有简单的方法可以做到这一点,就像@alex 指出的那样,您需要使用 JavaScript。他的回答是最好的解决方案,但为了优化它,您可能只想过滤到帖子内容链接。
<script>
var links = document.querySelectorAll( '.post-content a' );
for (var i = 0, length = links.length; i < length; i++) {
if (links[i].hostname != window.location.hostname) {
links[i].target = '_blank';
}
}
</script>
The code is compatible with IE8+ and you can add it to the bottom of your page. Note that you'll need to change the ".post-content a" to the class that you're using for your posts.
该代码与 IE8+ 兼容,您可以将其添加到页面底部。请注意,您需要将“.post-content a”更改为您用于帖子的类。
As seen here: http://blog.hubii.com/target-_blank-for-links-on-ghost/
如下所示:http: //blog.hubii.com/target-_blank-for-links-on-ghost/