An autolink is a bare URL or email address in text, which Comrak turns into a link without any markup around it. CommonMark’s own autolinks need angle brackets around the address (<https://example.com>); this extension recognises addresses without them. Use autolinks when you want a bare address to be clickable.

Autolinks originate in GitHub Flavored Markdown. This document describes Comrak’s autolinks, which can differ from GitHub’s. GitLab uses Comrak’s autolinks, so this document is also the canonical spec for GitLab Flavored Markdown URL auto-linking.

To enable autolinks, pass -e autolink (or --gfm) on the command line, or set extension.autolink in the options struct.

Here’s a paragraph with each kind of autolink:

Example 1
$ comrak -e autolink
Visit www.commonmark.org, read https://spec.commonmark.org/, or write to hello@example.com.

Syntax

Comrak recognises three kinds of autolink: www autolinks, URL autolinks, and email autolinks. It finds www and URL autolinks while it parses inline content, and email autolinks afterwards, in the text that remains.

Valid domains

A valid domain is one or more segments separated by periods (.). A segment is a sequence of characters that aren’t whitespace, punctuation, or symbols, except that underscores (_) and hyphens (-) are permitted. No underscore may appear in the last two segments:

Example 2
$ comrak -e autolink
www.a_b.c.com www.a.b_c.com www.a_b.com

Letters and digits outside ASCII are permitted:

Example 3
$ comrak -e autolink
www.bücher.de www.例え.jp

A domain with more than ten periods is valid even with underscores in its last two segments. This exception, inherited from cmark-gfm, avoids quadratic behaviour on long inputs:

Example 4
$ comrak -e autolink
www.a.b.c.d.e.f.g.h.i.j.k_l.m_n

A www autolink is the text www., followed by a valid domain that contains at least one period, followed by any further characters up to where the autolink ends.

The link’s destination is the autolink’s text with http:// in front:

Example 5
$ comrak -e autolink
www.commonmark.org
Example 6
$ comrak -e autolink
Visit www.commonmark.org/help for more information.

www. must be lowercase, and the domain after it must contain a period. None of the following are www autolinks:

Example 7
$ comrak -e autolink
WWW.commonmark.org www.com www.

A www autolink must begin at the start of a line, after whitespace, or after one of *, _, ~, or (:

Example 8
$ comrak -e autolink
(www.a.com) *www.b.com* _www.c.com_ ~www.d.com~

None of the following are www autolinks:

Example 9
$ comrak -e autolink
awww.a.com "www.b.com" .www.c.com

A URL autolink is one of the schemes http://, https://, or ftp://, followed by a valid domain, followed by any further characters up to where the autolink ends. The scheme is case-insensitive. The domain needn’t contain a period, but its first character must not be punctuation.

The link’s destination is the autolink’s text:

Example 10
$ comrak -e autolink
http://commonmark.org

(Visit https://encrypted.google.com/search?q=Markup+(business))

Anonymous FTP is available at ftp://foo.bar.baz.
Example 11
$ comrak -e autolink
HTTPS://example.com http://localhost:3000/admin

Other schemes aren’t recognised (but see Relaxed autolinks). None of the following are URL autolinks:

Example 12
$ comrak -e autolink
smb://a.com irc://a.com http:// http://-a.com

A URL autolink may begin anywhere, unless an ASCII letter comes directly before the scheme. That letter would be read as part of the scheme:

Example 13
$ comrak -e autolink
1http://a.com xhttp://a.com

An email autolink consists of:

The part after the @ must contain at least one period, each period must be followed by an ASCII letter or digit, and the last character must be an ASCII letter.

The link’s destination is the address with mailto: in front:

Example 14
$ comrak -e autolink
foo@bar.baz

+ may appear before the @, but not after it:

Example 15
$ comrak -e autolink
hello@mail+xyz.example isn't valid, but hello+xyz@mail.example is.

., -, and _ may appear on both sides of the @. A final . isn’t part of the address, and a final - or _ means there’s no email autolink:

Example 16
$ comrak -e autolink
a.b-c_d@a.b

a.b-c_d@a.b.

a.b-c_d@a.b-

a.b-c_d@a.b_

The address must end with a letter, and a second @ can’t follow the first:

Example 17
$ comrak -e autolink
a@b.c1 a@b@c.com

An email autolink may begin anywhere. The address starts just after the last character that can’t be part of it:

Example 18
$ comrak -e autolink
☃foo@bar.com x:foo@bar.com

With mailto: or xmpp: directly before the address, the prefix is part of the autolink, and no mailto: is added. An xmpp: address may also have a resource after a slash (/):

Example 19
$ comrak -e autolink
mailto:foo@bar.com xmpp:foo@bar.com/home foo@bar.com/home

The address must consist of literal characters. A backslash escape or an entity reference can’t form part of it:

Example 20
$ comrak -e autolink
foo\@bar.com foo&#64;bar.com

A www or URL autolink extends up to the next whitespace character or <. Comrak then removes characters from its end, one at a time, until the autolink no longer ends with any of these:

Trailing punctuation isn’t part of the autolink, but the same characters may appear inside it:

Example 21
$ comrak -e autolink
Visit www.commonmark.org.

Visit www.commonmark.org/a.b.
Example 22
$ comrak -e autolink
"https://a.com/x", 'https://b.com/y'!

An autolink keeps a ) that matches a ( within it, so it can sit inside parentheses:

Example 23
$ comrak -e autolink
www.google.com/search?q=Markup+(business)

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

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

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

Only a final ) is checked, so unbalanced parentheses inside the autolink are kept:

Example 24
$ comrak -e autolink
www.google.com/search?q=(business))+ok

A final &, ASCII letters, and ; — something that looks like an entity reference — aren’t part of the autolink. A ; on its own is removed too:

Example 25
$ comrak -e autolink
www.google.com/search?q=commonmark&hl=en

www.google.com/search?q=commonmark&hl;

www.google.com/search?q=commonmark&h1;

< ends an autolink immediately:

Example 26
$ comrak -e autolink
www.commonmark.org/he<lp

Brackets get no special treatment (but see Relaxed autolinks):

Example 27
$ comrak -e autolink
http://example.com/[abc]]...

Comrak checks the domain after removing these characters, so a trailing underscore doesn’t make the domain invalid:

Example 28
$ comrak -e autolink
www.a.com_ __http://b.com__

The autolink’s text is the source text as written. Entity references and backslash escapes in it aren’t interpreted:

Example 29
$ comrak -e autolink
www.a.com/?a=1&amp;b=2 www.a.com/a\_b

Autolinks aren’t recognised in link text, an image description, or a code span:

Example 30
$ comrak -e autolink
[www.a.com]/x ![foo@bar.com]/y.png `http://a.com`

Nor are they recognised inside brackets that don’t form a link (but see Relaxed autolinks):

Example 31
$ comrak -e autolink
[see www.a.com] [http://b.com] [foo@bar.com]

Emphasis delimiters around an autolink aren’t part of it:

Example 32
$ comrak -e autolink
*www.a.com* **http://b.com** _foo@bar.com_

Output

HTML output renders an autolink as an <a> element, just as it renders an inline link. The href is the destination described above, with the usual percent-encoding. The element’s content is the autolink’s text.

CommonMark output writes any link whose text equals its destination, ignoring a mailto: prefix, as a CommonMark autolink. URL and email autolinks therefore come out as <https://a.com> and <foo@bar.com>, but a www autolink comes out as an inline link, [www.a.com](http://www.a.com). With the extension enabled, CommonMark output escapes @ in other text (a\@b), to prevent reading the text back as an email autolink.

In XML output, an autolink is a link element with the destination in its destination attribute and an empty title.

In the AST, an autolink is a NodeValue::Link, like an inline link. Its NodeLink holds the destination and an empty title, and its only child is a text node. Nothing in the AST distinguishes an autolink from an inline link with the same text and destination.

An autolink’s source position covers its text:

Example 33
$ comrak -e autolink --sourcepos
x www.a.com y http://b.com z foo@bar.com

Options

With --relaxed-autolinks (parse.relaxed_autolinks), Comrak recognises autolinks in more places.

Autolinks are recognised inside brackets that don’t form a link:

Example 34
$ comrak -e autolink --relaxed-autolinks
[www.a.com] [http://b.com] [foo@bar.com]

A URL autolink may use any scheme of ASCII letters, or none at all:

Example 35
$ comrak -e autolink --relaxed-autolinks
smb:///Volumes/shared/foo.pdf irc://irc.freenode.net/git rdar://localhost.com/blah ://a.com

A www autolink’s domain needn’t contain a period, and a URL autolink’s domain may begin with punctuation, as in an IPv6 address:

Example 36
$ comrak -e autolink --relaxed-autolinks
www.a http://[::1]:80/

The rule for a final ) also applies to ] and }. Comrak removes a final ] if the autolink contains more ] than [, and likewise for }:

Example 37
$ comrak -e autolink --relaxed-autolinks
{https://foo.com} http://example.com/[abc]]...

Comrak takes a www or URL autolink that runs into ]( to be the text of a link, and doesn’t recognise it:

Example 38
$ comrak -e autolink --relaxed-autolinks
[http://foo.com/]url [www.foo.com/]url

Interactions

--gfm enables autolinks, along with the other GitHub Flavored Markdown extensions:

Example 39
$ comrak --gfm
www.commonmark.org

Autolinks work in table cells, and a cell’s pipes aren’t part of them:

Example 40
$ comrak -e autolink -e table
| site | contact |
| --- | --- |
|www.a.com|foo@bar.com|

With strikethrough, the tildes aren’t part of the autolink:

Example 41
$ comrak -e autolink -e strikethrough
~~www.a.com~~ ~https://b.com~

With task lists, an autolink straight after a task list item marker means the item isn’t a task list item:

Example 42
$ comrak -e autolink -e tasklist
- [ ] www.a.com
- [x]https://b.com

With --smart, quotation marks around an autolink become curly, and aren’t part of it:

Example 43
$ comrak -e autolink --smart
'foo@bar.com' "https://a.com"

With wikilinks, a wikilink takes precedence over an autolink inside it:

Example 44
$ comrak -e autolink -e wikilinks-title-after-pipe
[[www.a.com]] [[Docs|https://b.com]]

Differences from cmark-gfm

cmark-gfm, the reference implementation of GitHub Flavored Markdown, uses the same autolink design as Comrak. It has no equivalent of Relaxed autolinks, and doesn’t support the wikilinks or footnotes extensions, so those examples aren’t comparable. On every other example above, the two implementations agree on HTML output, except that cmark-gfm:

Regressions

The following examples guard against regressions.

Unicode isolates around a URL aren’t part of the autolink (#654). This example has an invisible U+2068 before the URL and an invisible U+2069 after it:

Example 45
$ comrak -e autolink
⁨https://www.example.com/⁩

A relaxed email autolink in what looks like a footnote definition (#677):

Example 46
$ comrak -e autolink -e footnotes --relaxed-autolinks
[^a@b.c
A]:

Source positions of email autolinks in a table cell, around escaped pipes:

Example 47
$ comrak -e autolink -e table --sourcepos
| a@b.cd\|e@f.gh\|i |
| - |

Source positions of an email autolink after smart punctuation:

Example 48
$ comrak -e autolink --smart --sourcepos
a--foo@bar.com