Skip to content

How Links Are Replaced

m4bwav edited this page Sep 30, 2026 · 2 revisions

For a text, replacePlainLinks does four things in order: it finds the plain links, checks each distinct address for an image, requests the title of each one that is not an image, and writes the template in place of each link that got a title, from the end of the text backwards. Everything else in the text is left byte for byte.

The examples below show an input line, then => and what the call returned with the default template. The local test server titled an ordinary page Page <path>.

Where a link ends

A link is an http:// or https:// address. Punctuation that ends a sentence (., ,, ;, :, !, ?), a closing quote, *, ~ and a backtick stay in the text after the link, and so does a closing bracket the link did not open. A bracket pair inside the address stays in it.

See https://example.com/page.
=> See "[Page /page](https://example.com/page)", *example.com*.

Is it https://example.com/page?
=> Is it "[Page /page](https://example.com/page)", *example.com*?

(see https://example.com/page).
=> (see "[Page /page](https://example.com/page)", *example.com*).

https://en.wikipedia.org/wiki/Bent_(band)
=> "[Page /wiki/Bent\_(band)](https://en.wikipedia.org/wiki/Bent_(band))", *wikipedia.org*

A link followed by a double quote is read as an attribute value and left alone, even in running text:

"https://example.com/page", she said
=> "https://example.com/page", she said

A port is part of the link, and emphasis around a link stays around the replacement:

*https://example.com/page*
=> *"[Page /page](https://example.com/page)", *example.com**

Port http://example.com:8080/page
=> Port "[Page /page](http://example.com:8080/page)", *example.com*

Links that are never requested

None of these makes a request, and each comes back as written:

[docs](https://example.com/page)
=> [docs](https://example.com/page)

![logo](https://example.com/logo)
=> ![logo](https://example.com/logo)

<https://example.com/page>
=> <https://example.com/page>

[1]: https://example.com/page
=> [1]: https://example.com/page

<a href="https://example.com/page">x</a>
=> <a href="https://example.com/page">x</a>

`https://example.com/page`
=> `https://example.com/page`

www.example.com/page and //example.com/page
=> www.example.com/page and //example.com/page

ftp://example.com/file and mailto:someone@example.com
=> ftp://example.com/file and mailto:someone@example.com

https://user:secret@example.com/page
=> https://user:secret@example.com/page

https://example.com/cat.png
=> https://example.com/cat.png

That is: markdown links and images, autolinks, reference definitions, HTML attributes, code spans (and fenced and indented code blocks, per the README and the golden replay on Versions and upgrading), addresses without a scheme, other schemes, addresses with a user name or password, and addresses whose path ends in an image extension. A single-label host such as http://intranet/page is not read as a link either: it made no request and came back unchanged.

Links that are requested and still left

A page that is an image by its Content-Type, answers with an error status, is not HTML, or has no <title> leaves its link as it was:

Image by content type: https://example.com/logo
=> Image by content type: https://example.com/logo

Not found: https://example.com/missing
=> Not found: https://example.com/missing

Server error: https://example.com/down
=> Server error: https://example.com/down

Not HTML: https://example.com/plain and https://example.com/json
=> Not HTML: https://example.com/plain and https://example.com/json

No title: https://example.com/notitle
=> No title: https://example.com/notitle

A page that does not answer within timeout (10 seconds by default) is left too, and so is every link when the requests cannot be made at all (Deno without --allow-net, a proxy Node does not use). Nothing says which: see Edge cases and errors.

What is requested

Each distinct address is requested once to see whether it is an image (is-an-image-url), skipping addresses whose path ends in an image extension, and once more for its title (get-title-at-url) if it is not an image. For a text with five links, two of them the same and two of them images, the call returned this, and the server saw these requests, in this order:

"[Page /a](https://example.com/a)", *example.com* "[Page /b](https://example.com/b)", *example.com* "[Page /a](https://example.com/a)", *example.com* https://example.com/pic.png https://example.com/logo

GET https://example.com/a
GET https://example.com/b
GET https://example.com/logo
GET https://example.com/a
GET https://example.com/b

The input was https://example.com/a https://example.com/b https://example.com/a https://example.com/pic.png https://example.com/logo. All image checks come first; the title lookups start 100 ms apart (the source's STAGGER), so one site does not get every request at once, and that call took at least 100 ms. A link that appears twice is replaced in both places from one lookup.

The requests are made with the runtime's fetch, from wherever the code runs, to whatever host the text names, localhost and private addresses included. Run it on text you trust.

How the title is read

The title comes from get-title-at-url 3.0.0. A site name after a spaced - or | is removed, a colon is not a separator, <title> wins over an <h1>, a declared character set and gzip are decoded, line breaks become spaces, and a redirect is followed while the link keeps its written address:

https://docs.example.com/guide
=> "[Getting started](https://docs.example.com/guide)", *example.com*

https://example.com/colon
=> "[Release notes: version 2](https://example.com/colon)", *example.com*

https://example.com/heading
=> "[Doc Title](https://example.com/heading)", *example.com*

https://example.com/latin1
=> "[Café au lait](https://example.com/latin1)", *example.com*

https://example.com/gzip
=> "[Gzipped Title](https://example.com/gzip)", *example.com*

https://example.com/newline
=> "[First line second line](https://example.com/newline)", *example.com*

https://example.com/old
=> "[New Home](https://example.com/old)", *example.com*

https://example.com/contains-url
=> "[See https://example.com/other for more](https://example.com/contains-url)", *example.com*

The titles served were Getting started - Example Docs, Release notes: version 2, Doc Title beside an <h1>The Article Heading</h1>, Café au lait in ISO-8859-1, Gzipped Title gzipped, First line, a line break and second line, a 301 from /old to a page titled New Home, and a title containing an address. The address inside a title is not replaced again.

The site: {{source}}

{{source}} is the registrable domain from the Public Suffix List, private suffixes included, or the host name where there is none. With the template {{source}}:

https://www.example.co.uk/page
=> example.co.uk

https://someone.github.io/page
=> someone.github.io

https://docs.example.com/guide
=> example.com

http://192.0.2.10/page
=> 192.0.2.10

http://localhost/page
=> localhost

http://intranet/page
=> http://intranet/page

http://www.example.notatld/page
=> example.notatld

The intranet line is the single-label host that is not read as a link.

Templates

A template is mustache with three values: {{title}}, {{url}} (the link as written in the text) and {{source}}. {{name}} escapes HTML; {{{name}}} and {{&name}} do not. Sections, inverted sections and comments work; an unknown name renders as nothing. For a page titled Tom &amp; Jerry (read as Tom & Jerry), each template followed by what it wrote:

[{{title}}]({{url}})
=> [Tom &amp; Jerry](https://example.com/amp)

[{{{title}}}]({{url}})
=> [Tom & Jerry](https://example.com/amp)

[{{&title}}]({{url}})
=> [Tom & Jerry](https://example.com/amp)

{{title}} ({{source}})
=> Tom &amp; Jerry (example.com)

{{#title}}has a title{{/title}}
=> has a title

{{^nope}}no nope{{/nope}}
=> no nope

{{! a comment }}{{title}}
=> Tom &amp; Jerry

{{nope}}|{{title}}
=> |Tom &amp; Jerry

fixed text
=> fixed text

Partials ({{> name}}) and delimiter changes ({{=<% %>=}}) throw a TypeError, as does a tag or section left open (API reference).

The default template

The default, "[{{title}}]({{url}})", *{{source}}*, writes the title without HTML escaping and puts a backslash before the characters that would break the link or format it: \, `, *, _, [, ], < and >. Mustache tags inside a title are written as text, not rendered:

https://example.com/tricky
=> "[Array\[0\] \*star\* \_under\_ \`tick\` \<b\>](https://example.com/tricky)", *example.com*

https://example.com/amp
=> "[Tom & Jerry](https://example.com/amp)", *example.com*

https://example.com/mustache
=> "[{{url}} and {{{title}}}](https://example.com/mustache)", *example.com*

The tricky page's title was Array[0] *star* _under_ `tick` <b>.

Clone this wiki locally