有没有办法在Markdown中创建一个在新窗口中打开的链接?如果不是,您建议使用什么语法来完成此操作?我将把它添加到我使用的markdown编译器中。我认为这应该是一个选择。


当前回答

幽灵降价使用:

[Google](https://google.com" target="_blank)

在这里找到它: https://cmatskas.com/open-external-links-in-a-new-window-ghost/

其他回答

我正在使用Grav CMS,这是完美的:

阀体/内容: 一些文本[1]

车身/参考: [1]: http://somelink.com/?target=_blank

只要确保目标属性首先被传递,如果链接中有其他属性,将它们复制/粘贴到引用URL的末尾。

也可以作为直接链接: [进入本页](http://somelink.com/?target=_blank)

所以,这不是完全正确的,你不能添加链接属性Markdown URL。要添加属性,请检查正在使用的底层markdown解析器及其扩展。

特别是,pandoc有一个扩展来启用link_attributes,它允许在链接中进行标记。如。

[Hello, world!](http://example.com/){target="_blank"}

对于那些来自R的(例如使用rmarkdown, bookdown, blogdown等等),这是你想要的语法。 对于那些不使用R的用户,你可能需要在调用pandoc时使用+link_attributes来启用扩展

注意:这与kramdown解析器的支持不同,后者是上面接受的答案之一。特别要注意的是,kramdown与pandoc不同,因为它需要一个冒号——:——在花括号的开头——{},例如。

[link](http://example.com){:hreflang="de"}

特别是:

# Pandoc
{ attribute1="value1" attribute2="value2"}

# Kramdown
{: attribute1="value1" attribute2="value2"}
 ^
 ^ Colon

一个全局解决方案是放置<base target="_blank"> 到页面的<head>元素中。这有效地将默认目标添加到每个锚元素。我使用markdown在我的基于wordpress的网站上创建内容,我的主题定制器将允许我将该代码注入到每个页面的顶部。如果您的主题不能做到这一点,可以使用插件

我在尝试使用PHP实现markdown时遇到了这个问题。

由于用户生成的链接创建markdown需要在一个新的选项卡中打开,但网站链接需要留在选项卡中,我改变了markdown,只生成在一个新的选项卡中打开的链接。所以不是所有的链接在页面上链接,只是那些使用markdown。

在markdown我改变了所有的链接输出为<a target='_blank' href="…>,这很容易使用查找/替换。

完成alex回答(12月13日至10日)

一个更聪明的注入目标可以用下面的代码完成:

/*
 * For all links in the current page...
 */
$(document.links).filter(function() {
    /*
     * ...keep them without `target` already setted...
     */
    return !this.target;
}).filter(function() {
    /*
     * ...and keep them are not on current domain...
     */
    return this.hostname !== window.location.hostname ||
        /*
         * ...or are not a web file (.pdf, .jpg, .png, .js, .mp4, etc.).
         */
        /\.(?!html?|php3?|aspx?)([a-z]{0,3}|[a-zt]{0,4})$/.test(this.pathname);
/*
 * For all link kept, add the `target="_blank"` attribute. 
 */
}).attr('target', '_blank');

您可以通过在(?!html?|php3?|aspx?)组构造中添加更多扩展来更改regexp异常(请在这里了解这个regexp: https://regex101.com/r/sE6gT9/3)。

对于没有jQuery的版本,检查下面的代码:

var links = document.links;
for (var i = 0; i < links.length; i++) {
    if (!links[i].target) {
        if (
            links[i].hostname !== window.location.hostname || 
            /\.(?!html?)([a-z]{0,3}|[a-zt]{0,4})$/.test(links[i].pathname)
        ) {
            links[i].target = '_blank';
        } 
    }
}