Skip to content

API Reference

m4bwav edited this page Sep 29, 2026 · 2 revisions

API reference

The package exports one function, replacePlainLinks, as a named export and inside the default export ({replacePlainLinks}), from both the ES module and the CommonJS build. It has two forms: without a callback it returns a Promise (added in 2.0.0); with a callback it is the 1.x call.

Export Introduced in
replacePlainLinks(markdown, callback, template?) 1.x (the only form before 2.0.0)
replacePlainLinks(markdown, options?), returning a Promise 2.0.0
the timeout and signal options, and the callback form's fourth argument 2.0.0
the default export {replacePlainLinks} in the ES module build 2.0.0 (1.x was CommonJS only)
types ReplacePlainLinksOptions, ReplacePlainLinksCallback 2.0.0

replacePlainLinks(markdown, options?)

Returns a Promise of the new markdown.

Option Type Default What it does
template string "[{{title}}]({{url}})", *{{source}}*, with the title's markdown characters escaped The text that replaces each link (Templates). An empty string means the default.
timeout number 10000 Milliseconds to wait for each page, once for the image check and once for the title.
signal AbortSignal none Stops every lookup; the Promise rejects with the signal's reason.
const markdown = await replacePlainLinks('See https://example.com/.', {template: '<{{url}}> ({{title}}, {{source}})'});
console.log(markdown);
See <https://example.com/> (Example Domain, example.com).

A template may also come third, in 1.x's argument order, when there is no callback: replacePlainLinks(text, undefined, template) and replacePlainLinks(text, null, template) both use it. When both are given, options.template wins. Each line below is what the call resolved:

third argument, no callback (1.x order): resolves 'See [Example Domain](https://example.com/).'
null options with a template third: resolves 'See [Example Domain](https://example.com/).'
options.template wins over the third argument: resolves 'See Example Domain.'

replacePlainLinks(markdown, callback, template?, options?)

The 1.x form. It returns undefined and calls callback(newMarkdown) once. template is a template or undefined, null or '' for the default; options takes timeout and signal.

The callback runs after the call has returned, whatever the text holds:

replacePlainLinks('See https://example.com/', markdown => {
  console.log('callback:', markdown);
});
console.log('returned');
returned
callback: See "[Example Domain](https://example.com/)", *example.com*

The one exception is a falsy markdown, below. With a template and a 300 ms timeout, a page that takes 1.5 seconds to answer is left as it was:

replacePlainLinks('See https://example.com/slow?ms=1500 and https://example.com/', markdown => {
  console.log(markdown);
}, '[{{title}}]({{url}})', {timeout: 300});
See https://example.com/slow?ms=1500 and [Example Domain](https://example.com/)

An exception thrown inside your callback is your own uncaught exception, as with any callback; 1.1.16 turned it into an unhandled Promise rejection (CHANGELOG, 2.0.0).

A falsy markdown

A falsy markdown is answered with the same value and no request. The Promise form resolves it; the callback form calls back before the call returns, the only case where it does:

'' -> '' before return
null -> null before return
undefined -> undefined before return
0 -> 0 before return
false -> false before return

The README names '', null and undefined; 0, false and NaN are answered the same way. A String object is read as its string.

Errors

Both forms throw a TypeError at the call, before any request, for a bad argument; they never reject for one. A lookup never throws or rejects: a page that cannot be read leaves its link as it was (Edge cases and errors). Every message, as printed:

replacePlainLinks(42): throws TypeError: markdown must be a string, not 42
replacePlainLinks(['https://example.com']): throws TypeError: markdown must be a string, not an array
replacePlainLinks({}): throws TypeError: markdown must be a string, not an object
replacePlainLinks('x', 'yes'): throws TypeError: the second argument must be a callback or an options object, not "yes"
replacePlainLinks('x', {template: 42}): throws TypeError: template must be a string, not 42
replacePlainLinks('x', {template: '{{> header}}'}): throws TypeError: template: {{>...}} tags (partials, delimiters, blocks) are not supported
replacePlainLinks('x', {template: '{{=<% %>=}}'}): throws TypeError: template: {{=...}} tags (partials, delimiters, blocks) are not supported
replacePlainLinks('x', {template: '{{title'}): throws TypeError: template: the tag at position 0 is not closed with }}
replacePlainLinks('x', {template: '{{#title}}x'}): throws TypeError: template: the section {{#title}} is not closed
replacePlainLinks('x', {timeout: 0}): throws TypeError: timeout must be a positive number of milliseconds, not 0
replacePlainLinks('x', {timeout: '5000'}): throws TypeError: timeout must be a positive number of milliseconds, not "5000"
replacePlainLinks('x', {signal: {}}): throws TypeError: signal must be an AbortSignal
replacePlainLinks('x', callback, 42): throws TypeError: template must be a string, not 42
replacePlainLinks('x', callback, undefined, 'fast'): throws TypeError: options must be an object, not "fast"
replacePlainLinks('', {timeout: -1}): throws TypeError: timeout must be a positive number of milliseconds, not -1
replacePlainLinks(42, callback): throws TypeError: markdown must be a string, not 42

The options are checked even when the markdown is empty (the '' line above).

Stopping with a signal

An aborted signal rejects the Promise form with the signal's reason, even for a text with no links; the callback form gets the markdown unchanged instead. What each call gave:

already aborted: rejects AbortError: This operation was aborted
already aborted, no links: rejects AbortError: This operation was aborted
aborted with a reason while waiting: rejects Error: the user left
AbortSignal.timeout(300): rejects TimeoutError: The operation was aborted due to timeout
callback form, aborted: 'See https://example.com/never'

AbortSignal.timeout(ms) is a deadline for the whole call, where timeout is a limit per page; Recipes compares the two.

Clone this wiki locally