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


当前回答

没有简单的方法来做到这一点,就像@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>

该代码与IE8+兼容,您可以将其添加到页面底部。注意,您需要更改“。Post-content (Post-content)指向你在帖子中使用的类。

如下所示:http://blog.hubii.com/target-_blank-for-links-on-ghost/

其他回答

没有简单的方法来做到这一点,就像@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>

该代码与IE8+兼容,您可以将其添加到页面底部。注意,您需要更改“。Post-content (Post-content)指向你在帖子中使用的类。

如下所示:http://blog.hubii.com/target-_blank-for-links-on-ghost/

我不认为有降价功能,尽管如果你想用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。

不是一个直接的答案,但可能会帮助一些人在这里结束。

如果你正在使用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"。

如果需要更多选项,请参考文档。

完成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';
        } 
    }
}

在《Laravel》中,我是这样解决的:

$post->text= Str::replace('<a ', '<a target="_blank"', $post->text);

Not适用于特定链接。编辑Markdown文本中的所有链接。(对我来说还好)