项目

自动链接扩展(Markdig 规范文档)

本节描述了支持的不同扩展:

自动链接

自动链接会将以下字符串格式化为 HTML 链接:

  • http://https://
  • ftp://
  • mailto:
  • tel:
  • www.
这是一个 <a href="http://www.google.com">http://www.google.com</a> URL 和 <a href="https://www.google.com">https://www.google.com</a>
这是一个 [ftp://test.com](/docs/zh-Hans/net-lib-docs/master/Markdig/Specs/ftp://test.com)
以及 [email@toto.com](/docs/zh-Hans/net-lib-docs/master/Markdig/Specs/mailto:email@toto.com)
和 [+1555123456](/docs/zh-Hans/net-lib-docs/master/Markdig/Specs/tel:+1555123456)
还有纯文本的 <a href="http://www.google.com">www.google.com</a>
.
<p>This is a <a href="http://www.google.com">http://www.google.com</a> URL and <a href="https://www.google.com">https://www.google.com</a>
This is a [ftp://test.com](/docs/zh-Hans/net-lib-docs/master/Markdig/Specs/ftp://test.com)
And a [email@toto.com](/docs/zh-Hans/net-lib-docs/master/Markdig/Specs/mailto:email@toto.com)
And a [+1555123456](/docs/zh-Hans/net-lib-docs/master/Markdig/Specs/tel:+1555123456)
And a plain <a href="http://www.google.com">www.google.com</a></p>
`````````````````````````````````

但不完整的链接不会匹配:

```````````````````````````````` example
这不是一个 <p>This is not a http:/www.google.com URL and https:/www.google.com
This is not a ftp:/test.com
And not a mailto:emailtoto.com
And not a plain www. or a www.x
And not a tel:</p>
`````````````````````````````````

上一个字符必须是标点符号或有效的空格(制表符、空格或换行符):

```````````````````````````````` example
这不是 nhttp://www.google.com 的 URL,但 this is (<a href="https://www.google.com">https://www.google.com</a>)
.
<p>This is not a nhttp://www.google.com URL but this is (<a href="https://www.google.com">https://www.google.com</a>)</p>
`````````````````````````````````

自动链接不应干扰 HTML 内联 `<a>` 标签:

```````````````````````````````` example
这是 HTML 内联 <a href="http://www.google.com">http://www.google.com</a> 的链接
.
<p>This is an HTML <a href="http://www.google.com">http://www.google.com</a> link</p>
`````````````````````````````````
或甚至在强调文本中:

```````````````````````````````` example
这是 HTML 内联 <a href="http://www.google.com"> **http://www.google.com** </a> 的链接
.
<p>This is an HTML <a href="http://www.google.com"> <strong>http://www.google.com</strong> </a> link</p>
`````````````````````````````````

自动链接不应干扰 Markdown 链接:

```````````````````````````````` example
这是一个 HTML 内联 [http://www.google.com](http://www.google.com) 的链接
.
<p>This is an HTML <a href="http://www.google.com">http://www.google.com</a> link</p>
`````````````````````````````````

如果匹配的链接末尾有字符,则包裹在待处理强调内的链接应使强调优先:

```````````````````````````````` example
检查 <strong><a href="http://www.a.com">http://www.a.com</a></strong> 或 <strong><a href="http://www.b.com">http://www.b.com</a></strong>
.
<p>Check <strong><a href="http://www.a.com">http://www.a.com</a></strong> 或 <strong><a href="http://www.b.com">http://www.b.com</a></strong></p>
`````````````````````````````````

规范中未提及,但空电子邮件地址不会匹配(仅支持 RFC2368 的子集):

```````````````````````````````` example
mailto:email@test.com 是有效的,但 mailto:@test.com 不是
.
<p>[email@test.com](/docs/zh-Hans/net-lib-docs/master/Markdig/Specs/mailto:email@test.com) 是有效的,但 mailto:@test.com 不是</p>
`````````````````````````````````

### GFM 支持

摘自 [GFM 自动链接扩展规范](https://github.github.com/gfm/#autolinks-extension-):

```````````````````````````````` example
www.commonmark.org
.
<p><a href="http://www.commonmark.org">www.commonmark.org</a></p>
````````````````````````````````

```````````````````````````````` example
访问 <a href="http://www.commonmark.org/help">www.commonmark.org/help</a> 以获取更多信息。
.
<p>Visit <a href="http://www.commonmark.org/help">www.commonmark.org/help</a> for more information.</p>
````````````````````````````````

```````````````````````````````` example
访问 <a href="http://www.commonmark.org">www.commonmark.org</a>。

访问 <a href="http://www.commonmark.org/a.b">www.commonmark.org/a.b</a>。
.
<p>Visit <a href="http://www.commonmark.org">www.commonmark.org</a>.</p>
<p>Visit <a href="http://www.commonmark.org/a.b">www.commonmark.org/a.b</a>.</p>
````````````````````````````````


```````````````````````````````` example
www.google.com/search?q=Markup+(business)

(www.google.com/search?q=Markup+(business))
.
<p><a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a></p>
<p>(<a href="http://www.google.com/search?q=Markup+(business)">www.google.com/search?q=Markup+(business)</a>)</p>
````````````````````````````````


```````````````````````````````` example
www.google.com/search?q=commonmark&hl=en

www.google.com/search?q=commonmark&hl;
.
<p><a href="http://www.google.com/search?q=commonmark&amp;hl=en">www.google.com/search?q=commonmark&amp;hl=en</a></p>
<p><a href="http://www.google.com/search?q=commonmark">www.google.com/search?q=commonmark</a>&amp;hl;</p>
````````````````````````````````


```````````````````````````````` example
www.commonmark.org/he<lp
.
<p><a href="http://www.commonmark.org/he">www.commonmark.org/he</a>&lt;lp</p>
````````````````````````````````

```````````````````````````````` example
http://commonmark.org

(访问 <a href="https://encrypted.google.com/search?q=Markup+(business)">https://encrypted.google.com/search?q=Markup+(business)</a>)

匿名 FTP 可在 [ftp://foo.bar.baz](/docs/zh-Hans/net-lib-docs/master/Markdig/Specs/ftp://foo.bar.baz) 处使用。
.
<p><a href="http://commonmark.org">http://commonmark.org</a></p>
<p>(访问 <a href="https://encrypted.google.com/search?q=Markup+(business)">https://encrypted.google.com/search?q=Markup+(business)</a>)</p>
<p>匿名 FTP 可在 [ftp://foo.bar.baz](/docs/zh-Hans/net-lib-docs/master/Markdig/Specs/ftp://foo.bar.baz) 处使用。</p>
````````````````````````````````

### 有效域测试

具有空段的域名不会被匹配

```````````````````````````````` example
www..
www..com
http://test
在本文档中