- Category
- Formatting & size
- Maintained by
- rehype team
- License
- MIT
- Module format
- ESM, default export
npm install rehype-formatWhat it does
rehype-format rewrites the whitespace between elements so the serialized HTML is indented and readable. It knows which elements are whitespace sensitive, so the contents of <pre>, <textarea>, <script> and <style> are left alone, and it avoids adding whitespace where that would change how inline content renders.
Example
import {rehype} from 'rehype'
import rehypeFormat from 'rehype-format'
const input = `<!doctype html><html lang=en><head><title>Demo</title></head>
<body><main><h1>Hello</h1><p>Formatted by <b>rehype</b>.</p></main></body></html>`
const file = await rehype().use(rehypeFormat).process(input)
console.log(String(file))<!doctype html>
<html lang="en">
<head>
<title>Demo</title>
</head>
<body>
<main>
<h1>Hello</h1>
<p>Formatted by <b>rehype</b>.</p>
</main>
</body>
</html>Options
| Option | Default | Meaning |
|---|---|---|
indent | 2 | A number of spaces, or a string such as '\t', per level. |
indentInitial | true | Whether to indent the first level (usually <head> and <body> inside <html>). |
blanks | [] | Tag names to separate with a blank line when they are siblings, for example ['head', 'body']. |
When to use it
- Making generated HTML readable for code review or snapshot tests.
- Normalizing HTML files in a repository with
rehype-cli. - The last step of a pipeline that builds HTML from Markdown.
It is the opposite of rehype-preset-minify; use one or the other. Try it in the HTML Formatter.
Official references
- rehype-format readme (API and all options) (opens in a new tab)
- rehype-format on npm (current version) (opens in a new tab)
Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.