rehype-format

Pretty-prints HTML: consistent indentation and line breaks, while respecting whitespace-sensitive elements.

Category
Formatting & size
Maintained by
rehype team
License
MIT
Module format
ESM, default export
Install
npm install rehype-format

What 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

format-document.mjs
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))
Output node format-document.mjs
<!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

OptionDefaultMeaning
indent2A number of spaces, or a string such as '\t', per level.
indentInitialtrueWhether 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

Facts on this page were checked against the package readme on November 11, 2026. Options not covered here are documented upstream.

Use ↑ ↓ to move, Enter to open.