Documentation menu
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:
npm install --save-dev rehype-cli rehype-format rehype-preset-minifyRun 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:
npx rehype --helpFormat 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:
# 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<!doctype html>
<html>
<head>
<title>CLI</title>
</head>
<body>
<ul>
<li>One</li>
<li>Two</li>
</ul>
</body>
</html>
input.html > output.html: writtenThe 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:
npx rehype index.html --use rehype-format --outputUse pipes
Without a file argument, rehype reads standard input and writes to standard output, so it fits in shell pipelines:
# 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<p class=a>Hello <b>world</b>
<stdin>: no issues foundInspect 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:
# Print the hast syntax tree instead of HTML.
echo '<h1>Title</h1>' | npx rehype --inspect --no-colorroot[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:
npx rehype "public/**/*.html" --use rehype-preset-minify -oBy 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:
{
"scripts": {
"format:html": "rehype public -o"
},
"rehype": {
"plugins": ["rehype-format"]
}
}Then run npm run format:html.
Useful flags
| Flag | Does |
|---|---|
-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, --frail | Exit with code 1 on warnings (useful in CI). |
-q, --quiet / -S, --silent | Report only warnings and errors / only errors. |
-w, --watch | Reprocess when files change. |
--inspect | Print a formatted syntax tree. |
The authoritative list is the output of rehype --help and the readme linked below.