rehype-cli: install and first commands

rehype-cli is a command line interface around rehype. It installs a command named rehype that reads HTML files (or standard input), runs plugins you name, and writes the result. It is built on unified-args, the same engine used by the remark CLI.

Install

Install the CLI in your project together with the plugins you plan to use:

Terminal
npm install --save-dev rehype-cli rehype-format rehype-preset-minify

Run it with npx rehype, or call rehype from package.json scripts, where local binaries are on the path. A local install pins the version per project; that matters because plugins are loaded from your project's node_modules.

Check that it works:

Terminal
npx rehype --help

Format a file

The --use (-u) flag loads a plugin. The CLI resolves short names, so --use format would also find rehype-format; we use full names for clarity. --output (-o) with a path writes there:

cli-format.sh
# Create a messy HTML file to work with.
printf '<!doctype html><html><head><title>CLI</title></head><body><ul><li>One<li>Two</ul></body></html>' > input.html

# Format it with rehype-format and write the result to output.html.
npx rehype input.html --use rehype-format --output output.html

cat output.html
Output bash cli-format.sh
<!doctype html>
<html>
  <head>
    <title>CLI</title>
  </head>
  <body>
    <ul>
      <li>One</li>
      <li>Two</li>
    </ul>
  </body>
</html>
input.html > output.html: written

The last line is the CLI's report, printed to standard error: which file was processed, where it went, and any warnings. With --output and no path, the input file is overwritten in place, which is what the official readme shows:

Terminal
npx rehype index.html --use rehype-format --output

Use pipes

Without a file argument, rehype reads standard input and writes to standard output, so it fits in shell pipelines:

cli-minify-pipe.sh
# Pipe HTML through rehype-cli: read stdin, write stdout.
echo '<p class="a  a">  Hello   <b>world</b>  </p>' | npx rehype --use rehype-preset-minify
Output bash cli-minify-pipe.sh
<p class=a>Hello <b>world</b>
<stdin>: no issues found

Inspect the syntax tree

--inspect prints the hast tree instead of HTML, with positions. This is the quickest way to see what a plugin will receive:

cli-inspect.sh
# Print the hast syntax tree instead of HTML.
echo '<h1>Title</h1>' | npx rehype --inspect --no-color
Output bash cli-inspect.sh
root[1] (1:1-2:1, 0-15)
│ data: {"quirksMode":true}
└─0 element<html>[2]
    │ properties: {}
    ├─0 element<head>[0]
    │     properties: {}
    └─1 element<body>[2]
        │ properties: {}
        ├─0 element<h1>[1] (1:1-1:15, 0-14)
        │   │ properties: {}
        │   └─0 text "Title" (1:5-1:10, 4-9)
        └─1 text "\n" (1:15-2:1, 14-15)
<stdin>: no issues found

--tree-out prints the tree as JSON instead, and --tree-in reads JSON trees.

Process many files

Give a folder or glob, and add -o to rewrite every matched file in place:

Terminal
npx rehype "public/**/*.html" --use rehype-preset-minify -o

By default, the CLI looks for .html, .htm, .xht and .xhtml files. Quote globs so the CLI expands them rather than your shell. Paths listed in a .rehypeignore file are skipped.

Save settings in a config file

Instead of repeating --use, list plugins in a rehype field in package.json (or in a .rehyperc file). The CLI finds the configuration automatically:

package.json
{
  "scripts": {
    "format:html": "rehype public -o"
  },
  "rehype": {
    "plugins": ["rehype-format"]
  }
}

Then run npm run format:html.

Useful flags

FlagDoes
-u, --use <plugin>Load a plugin (repeatable). Options: --use 'rehype-format=indent:4'.
-o, --output [path]Write output to a path, or in place when no path is given.
-s, --setting <settings>Parser and serializer settings, for example -s fragment:true.
-f, --frailExit with code 1 on warnings (useful in CI).
-q, --quiet / -S, --silentReport only warnings and errors / only errors.
-w, --watchReprocess when files change.
--inspectPrint a formatted syntax tree.

The authoritative list is the output of rehype --help and the readme linked below.

Sources and further reading

Use ↑ ↓ to move, Enter to open.