Skip to content
Autolinks
step 1/5

Reading — step 1 of 5

Learn

~3 min readInline Elements

Autolinks

The links lesson listed four bracket shapes and handed you three of them. The fourth was <https://example.com> — the autolink, where the URL is both the destination and the text. It looks like the least interesting row in that table, and it is the one whose grammar people get wrong most often.

What counts as an autolink?

A <, an absolute URI, a >. Absolute means it starts with a scheme: an ASCII letter, then one to thirty-one more letters, digits, +, . or -, then a colon. After the colon, anything except a space, a < or a >.

python

The spec is deliberately generous here: it does not check that a scheme is registered or that the URI is well formed, so a+b+c:d links. What it does check is the length, which is why m:abc — a one-character scheme — is text. And note that MAILTO: matches the URI rule, so it is a URI autolink whose href is its own text; no second mailto: gets glued on.

What is not an autolink

InputResult
<[email protected]>link, with mailto: prepended to the href
<https://a.b c>a space ends it — plain text
<foo.bar.baz>no scheme, no colon — plain text
<>plain text
https://a.bbare URL — plain text in core CommonMark

Everything in the reject column comes out as escaped text, &lt; and &gt;, which is why the scan cannot simply skip a < it does not like: it emits the escaped bracket and keeps going from the very next character, so a rejected candidate's own contents still get scanned.

One more rule that surprises people: backslash escapes do not work inside an autolink. The destination is taken verbatim.

The ampersand rule

The URI appears twice in the output — once as an attribute value, once as text — and both copies need escaping. A query string is the common case:

python

Bare URLs, and why they need an extension

Core CommonMark leaves https://example.com as text. GitHub Flavored Markdown adds autolink literals, which link bare URLs starting with http://, https:// or www., plus bare email addresses. The hard part is deciding where the URL stops, because a sentence puts punctuation right after it:

python

Trailing ., ,, !, ? and an unbalanced ) are trimmed. The real rule also trims a trailing entity reference such as &amp;, and counts parentheses so a URL with brackets in it keeps them. This is an extension, not core — if your parser links bare URLs, say so.

Your exercise

Recognise autolinks, escape everything else.

The mistake the grader catches is advancing past a rejected candidate. A hidden test feeds <m:abc> and <> and < https://x.y >; if a failed match makes you jump to the >, you swallow the text in between. On rejection emit &lt; and advance exactly one character.

The second is the order of the two rules. <MAILTO:[email protected]> contains an @ and looks like an email, but it has a scheme, so the URI rule claims it first and the href is the text unchanged. Test the URI pattern before the email pattern.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…

Autolinks — Build a Markdown to HTML Converter