Skip to content

Recipes

m4bwav edited this page Sep 30, 2026 · 2 revisions

Each recipe ran against the published 2.0.0 on Node 24.18.0, with local copies of the pages it links to.

Convert a file

markdown-plain-link-replacer -i answer.md > answer-linked.md

The repository keeps two sample texts in test/fixtures/. hogansample.md is a question with a picture and one link; with the README's 1.x template, only the link line changes:

$ markdown-plain-link-replacer -i answer.md -t "[{{title}}]({{url}})"
![In-game picture of Madagascar][1]

The country of Madagascar is only reachable through sea. This means that if the country closes its shipyard before I reach it there is no way I can possibly win… or is there?

How can I work around this? 

[Pandemic 2 and Other Free Internet Games @ CrazyMonkeyGames.com](http://www.crazymonkeygames.com/Pandemic-2.html)
  [1]: http://i.stack.imgur.com/0hCCA.png

The picture and the reference definition [1]: are left alone. The page was served with the title the repository's recorded output shows. Run through the library with the template [{{title}}]({{url}}) from {{source}}, the changed line was:

[Pandemic 2 and Other Free Internet Games @ CrazyMonkeyGames.com](http://www.crazymonkeygames.com/Pandemic-2.html) from crazymonkeygames.com

and the whole result equalled test/fixtures/hogansample-output.md line for line. wikialistsample.md, a longer answer ending in one link, with the default template changed only its last line, again equal to the recorded wikialistsample-output.md:

"[TranLang III communications module](http://starwars.wikia.com/wiki/TranLang_III_communications_module)", *wikia.com*

Both recorded outputs come from 1.x; 2.0.0 gave the same lines for these two texts. The output ends with one extra line break (Commands).

Every markdown file in a folder

A bash loop that rewrites each file in place, through a temporary file:

for file in docs/*.md; do
  markdown-plain-link-replacer -i "$file" > "$file.tmp" && mv "$file.tmp" "$file"
done
cat docs/*.md

With docs/a.md holding A: https://example.com/ and docs/b.md holding B: https://docs.example.com/guide, the loop (run in Git Bash) printed:

A: "[Example Domain](https://example.com/)", *example.com*

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

The blank lines are the extra line break each file gained.

Other link shapes

A plain markdown link:

console.log(await replacePlainLinks('Read https://docs.example.com/guide first.', {template: '[{{title}}]({{url}})'}));
Read [Getting started](https://docs.example.com/guide) first.

The title with the address still visible:

console.log(await replacePlainLinks('Read https://docs.example.com/guide first.', {template: '{{title}} (<{{url}}>)'}));
Read Getting started (<https://docs.example.com/guide>) first.

Reference-style links, with the definitions collected at the end:

const input = 'Read https://docs.example.com/guide and https://en.wikipedia.org/wiki/Bespin.';
const found = [];
const body = await replacePlainLinks(input, {template: '[{{title}}][{{url}}]'});
for (const [, url] of body.matchAll(/\]\[(https?:[^\]]+)\]/g)) {
  found.push(url);
}

console.log(`${body}\n\n${found.map(url => `[${url}]: ${url}`).join('\n')}`);
Read [Getting started][https://docs.example.com/guide] and [Bespin][https://en.wikipedia.org/wiki/Bespin].

[https://docs.example.com/guide]: https://docs.example.com/guide
[https://en.wikipedia.org/wiki/Bespin]: https://en.wikipedia.org/wiki/Bespin

A template you write escapes HTML in {{title}}; use {{{title}}} for the raw title (Templates).

One deadline for a whole text

timeout limits each page; a signal limits the whole call. With one page that never answers, a 500 ms signal rejects the call, so nothing is replaced, not even the page that answered at once:

const readme = 'Docs: https://docs.example.com/guide\nSlow: https://example.com/never\n';
const markdown = await replacePlainLinks(readme, {signal: AbortSignal.timeout(500)}).catch(error => {
  console.log(error.name);
  return readme;
});
console.log(markdown);
TimeoutError
Docs: https://docs.example.com/guide
Slow: https://example.com/never

A 500 ms timeout instead gives up on the slow page alone and keeps the rest:

console.log(await replacePlainLinks(readme, {timeout: 500}));
Docs: "[Getting started](https://docs.example.com/guide)", *example.com*
Slow: https://example.com/never

Behind a proxy

The lookups use the runtime's fetch. On Node 24.18.0, fetch ignores HTTP_PROXY and HTTPS_PROXY unless Node starts with NODE_USE_ENV_PROXY=1 (or --use-env-proxy):

NODE_USE_ENV_PROXY=1 HTTPS_PROXY=http://proxy.example:8080 node example.mjs

With the variables but without NODE_USE_ENV_PROXY, the Home example came back unchanged (no request reached the proxy or anywhere else); with it, the link was replaced, and the proxy received a CONNECT en.wikipedia.org:443 for each of the two requests. Node tunnels http: links through the proxy with CONNECT too, so the proxy must allow CONNECT to port 80.

Node 20.20.2 has no NODE_USE_ENV_PROXY: the same command left the link unchanged. On Node 20, install undici and set its proxy agent before the first lookup:

// use-proxy.mjs
import {setGlobalDispatcher, EnvHttpProxyAgent} from 'undici';

setGlobalDispatcher(new EnvHttpProxyAgent());
HTTPS_PROXY=http://proxy.example:8080 node --import ./use-proxy.mjs example.mjs

That replaced the link on Node 20.20.2 and on 24.18.0 (undici 7.30.0). Deno 2.9.6 and Bun 1.4.2 read HTTP_PROXY and HTTPS_PROXY without any setting; both replaced the Home example's link through the proxy.

These variables are how the runs for this wiki trusted their test certificate authority: NODE_EXTRA_CA_CERTS for Node and Bun, DENO_CERT for Deno. Without the certificate, https links were left unchanged, without an error (Edge cases and errors).

Clone this wiki locally