Skip to content

Getting Started

m4bwav edited this page Sep 30, 2026 · 2 revisions

Install

npm install markdown-plain-link-replacer

The package needs Node 20 or later ("engines": {"node": ">=20"}). It installs four packages of its own: get-title-at-url 3.0.0 reads each page's title, is-an-image-url 2.0.0 checks whether a link is an image, replace-string-at-position 2.0.0 puts the new text in place, and tldts (7.4.16 on 2026-09-29) names the site.

Other package managers were run the same way, each in an empty project, followed by the ES module example below:

Tool Version run Command Example
pnpm (through corepack) 12.6.0 pnpm add markdown-plain-link-replacer ran, same output
Yarn 1 (through corepack) 1.22.22 yarn add markdown-plain-link-replacer ran, same output
Yarn 4, Plug'n'Play 4.18.1 yarn add markdown-plain-link-replacer, then yarn node example.mjs ran, same output
Bun 1.4.2 bun add markdown-plain-link-replacer ran, same output

With Yarn 4's Plug'n'Play there is no node_modules, so run your script through yarn node.

ES module

import {replacePlainLinks} from 'markdown-plain-link-replacer';

const markdown = await replacePlainLinks('Source: https://en.wikipedia.org/wiki/Bespin');
console.log(markdown);

node example.mjs prints (the page at that address was titled Bespin - Wikipedia):

Source: "[Bespin](https://en.wikipedia.org/wiki/Bespin)", *wikipedia.org*

The default export is an object holding the same function, {replacePlainLinks}, which is what require() returned in 1.x.

CommonJS

require() gives both the named function and that default object. The callback form is the 1.x call, and still works:

const linkReplacer = require('markdown-plain-link-replacer');

linkReplacer.replacePlainLinks('  http://starwars.wikia.com/wiki/Bespin  ', newMarkdown => {
  console.log(newMarkdown);
}, '[{{title}}]({{url}}) from {{source}}');

With a page titled Bespin | Wookieepedia | Fandom, it prints the text with its spaces kept:

  [Bespin](http://starwars.wikia.com/wiki/Bespin) from wikia.com

Without a callback it returns a Promise, as in an ES module:

const {replacePlainLinks} = require('markdown-plain-link-replacer');

replacePlainLinks('See https://docs.example.com/guide.', {template: '[{{title}}]({{url}})'})
  .then(markdown => console.log(markdown));
See [Getting started](https://docs.example.com/guide).

The ES module and CommonJS builds are two separate copies: require('markdown-plain-link-replacer').replacePlainLinks is not the same function object as the one import gives. That matters only if you compare them. A deep import such as markdown-plain-link-replacer/dist/index.mjs fails with ERR_PACKAGE_PATH_NOT_EXPORTED; import the package by its name.

TypeScript

The package ships its own types for both builds, ReplacePlainLinksOptions and ReplacePlainLinksCallback among them. With --strict --module nodenext (TypeScript 6.0.3), this compiles without errors:

import {replacePlainLinks, type ReplacePlainLinksOptions} from 'markdown-plain-link-replacer';

const options: ReplacePlainLinksOptions = {template: '[{{title}}]({{url}})', timeout: 5000};
const markdown: string = await replacePlainLinks('Source: https://example.com', options);

replacePlainLinks('Source: https://example.com', (newMarkdown: string) => {
  console.log(newMarkdown, markdown);
});

A wrong option type is an error at the call, reported against each of the three overloads; the useful line is the one under the Promise overload:

Type 'string' is not assignable to type 'number'.

The answer's type follows the markdown's: a string | undefined argument gives Promise<string | undefined>, because a falsy markdown comes back as it was (API reference). Accessing .length on that answer fails with TS18048: 'answer' is possibly 'undefined'. In a CommonJS TypeScript file, import linkReplacer = require('markdown-plain-link-replacer') compiles, with both linkReplacer.replacePlainLinks(...) and linkReplacer.default.replacePlainLinks(...).

Deno

Deno 2.9.6 runs it from npm with an npm: specifier and needs network permission:

import {replacePlainLinks} from 'npm:markdown-plain-link-replacer@2.0.0';

const markdown = await replacePlainLinks('Source: https://en.wikipedia.org/wiki/Bespin');
console.log(markdown);

deno run --allow-net example.mjs prints:

Source: "[Bespin](https://en.wikipedia.org/wiki/Bespin)", *wikipedia.org*

Without --allow-net, and with no terminal to ask in, Deno refuses the requests, the package treats each page as unreadable, and the text comes back unchanged with no error:

Source: https://en.wikipedia.org/wiki/Bespin

Bun

Bun 1.4.2 runs both the ES module example and the CommonJS callback example above unchanged (bun example.mjs), with the same output as Node.

Browsers

Not tested. The package's own tests cover Node, Bun and Deno only.

Clone this wiki locally