Skip to content

Commands

m4bwav edited this page Sep 30, 2026 · 2 revisions

The package installs one command, markdown-plain-link-replacer. It reads a markdown text from an argument, a file or standard input, replaces its plain links as the library does (How links are replaced), and prints the result to standard output. The examples ran the published 2.0.0's dist/cli.mjs on Node 24.18.0.

$ markdown-plain-link-replacer "Source: https://example.com"
Source: "[Example Domain](https://example.com)", *example.com*

Installed in a project, npx markdown-plain-link-replacer "Source: https://example.com" printed the same. The README's npm install --global and npx without a local install were not tested.

Help

$ markdown-plain-link-replacer --help

  Replace plain links in markdown with titled links: each page is requested,
  and its title and site replace the bare URL.

  Usage
    $ markdown-plain-link-replacer "<markdown>"
    $ markdown-plain-link-replacer -i notes.md > notes-linked.md
    $ cat notes.md | markdown-plain-link-replacer

  Options
    -i, --input <file>      Read the markdown from a file ("-" for stdin)
    -t, --template <text>   A mustache template for each link, with {{title}},
                            {{url}} and {{source}} (HTML-escaped; {{{title}}}
                            is not). Default: "[{{title}}]({{url}})", *{{source}}*
    --timeout <ms>          Wait this long for each page (default 10000)
    -h, --help              Show this help
    -v, --version           Show the version

  With no markdown argument and no -i, the markdown is read from stdin.
  The result goes to stdout. Links whose page fails or has no title are
  left as they are.

  Exit codes
    0  the markdown was printed
    1  bad arguments, or the input could not be read

  Example
    $ markdown-plain-link-replacer "Source: https://example.com"
    Source: "[Example Domain](https://example.com)", *example.com*

-h prints the same text. Help goes to standard output with exit code 0.

$ markdown-plain-link-replacer --version
2.0.0

-v is the same.

Where the markdown comes from

The first argument is the markdown. Any further arguments are ignored without a warning, as in 1.x:

$ markdown-plain-link-replacer "first https://example.com/" "second https://example.com/new"
first "[Example Domain](https://example.com/)", *example.com*

-i <file> reads a file. Here notes.md holds a page link and an image link:

$ markdown-plain-link-replacer -i notes.md
From a file: "[Example Domain](https://example.com/)", *example.com*

And an image: https://example.com/cat.png

With no argument, or with - or -i -, it reads standard input. Each of these printed the same line for the input From stdin: https://example.com/:

$ printf "From stdin: https://example.com/" | markdown-plain-link-replacer
From stdin: "[Example Domain](https://example.com/)", *example.com*

An empty standard input prints an empty line and exits 0. With no argument and a terminal as standard input, it stops with error: give the markdown as an argument, a file with -i, or on stdin (from the source; not run, since the script had no terminal).

The output ends with a newline

The result is printed with console.log, which adds a line break. A file that already ends with one gains a blank line: notes.md above ends with one line break, and markdown-plain-link-replacer -i notes.md > notes-linked.md wrote a file ending in two. Nothing else in the text changes.

Templates

-t takes a template, as the library's template option does (Templates):

$ markdown-plain-link-replacer "Source: https://example.com" -t "[{{title}}]({{url}})"
Source: [Example Domain](https://example.com)
$ markdown-plain-link-replacer -i notes.md -t "<{{url}}> {{title}}"
From a file: <https://example.com/> Example Domain

And an image: https://example.com/cat.png

The help calls "[{{title}}]({{url}})", *{{source}}* the default, but that text given with -t is not the default: in a template you write, {{title}} escapes HTML, while the built-in default writes the title unescaped and backslash-escapes markdown characters instead. For a page titled Tom &amp; Jerry:

$ markdown-plain-link-replacer "Tom: https://example.com/amp"
Tom: "[Tom & Jerry](https://example.com/amp)", *example.com*
$ markdown-plain-link-replacer "Tom: https://example.com/amp" -t "\"[{{title}}]({{url}})\", *{{source}}*"
Tom: "[Tom &amp; Jerry](https://example.com/amp)", *example.com*

Leave -t out to get the default.

Timeout

--timeout <ms> is the time allowed for each page, as --timeout 300 or --timeout=300. Against a page that takes 1.5 seconds to answer, 300 ms leaves the link and 3000 ms replaces it:

$ markdown-plain-link-replacer "https://example.com/slow?ms=1500" --timeout 300
https://example.com/slow?ms=1500
$ markdown-plain-link-replacer "https://example.com/slow?ms=1500" --timeout=3000
"[Slow Page](https://example.com/slow?ms=1500)", *example.com*

Exit codes and errors

It exits 0 whenever it printed the markdown, including when every link was left unchanged. It exits 1 with a message on standard error for a bad argument or an input it cannot read:

$ markdown-plain-link-replacer -i missing.md
error: cannot read missing.md: ENOENT
$ markdown-plain-link-replacer -i
error: Option '-i, --input <value>' argument missing
Run markdown-plain-link-replacer --help for the usage.
$ markdown-plain-link-replacer --timeout soon x
error: --timeout takes a positive number of milliseconds, not "soon"
$ markdown-plain-link-replacer --timeout 0 x
error: --timeout takes a positive number of milliseconds, not "0"
$ markdown-plain-link-replacer x -t "{{> partial}}"
TypeError: template: {{>...}} tags (partials, delimiters, blocks) are not supported

An unknown option exits 1 too, with Node's own message:

$ markdown-plain-link-replacer --foo x
error: Unknown option '--foo'. To specify a positional argument starting with a '-', place it at the end of the command after '--', as in '-- "--foo"
Run markdown-plain-link-replacer --help for the usage.

A text the library would leave alone is printed as it is, with exit 0; 1.1.16 crashed on this one:

$ markdown-plain-link-replacer www.example.com/page
www.example.com/page

Clone this wiki locally