Rehype features

What rehype does well, what the ecosystem adds, and where another tool is a better fit. Each feature links to a deeper, tested walkthrough.

Standards-based parsing

rehype-parse uses parse5 to parse HTML according to the WHATWG HTML standard, the same rules browsers follow. Unclosed tags, misnested elements and missing <html> wrappers are repaired exactly as a browser would repair them. Parse errors can be reported as warnings.

How rehype works

The hast syntax tree

Documents become plain JavaScript objects in the hast format: elements with tagName, properties and children, plus text, comment and doctype nodes. Because the tree is plain data, you can inspect it, test it and transform it with ordinary code and shared utilities.

Parse and stringify

Plugins

A plugin is a function that receives the tree (and the file) and changes it. Plugins compose in order with .use(). Presets bundle several plugins with settings, as rehype-preset-minify does.

Choose plugins

Sanitizing

rehype-sanitize removes anything not on an allowlist. The default schema follows GitHub's rules for user content, and you can extend or restrict it.

Sanitize safely

Markdown in both directions

With remark-rehype you turn Markdown into HTML. With rehype-remark you turn HTML into Markdown. Both work through the shared unified interface.

rehype vs remark

Command line interface

rehype-cli processes files and folders, reads .rehyperc config files and rehype fields in package.json, supports ignore files, watch mode and syntax tree output.

CLI setup

Messages and reporting

Plugins report warnings on the virtual file (vfile) with line and column positions, so linting-style checks fit naturally into a pipeline.

Report problems

Typed

The packages ship TypeScript types, including types for hast nodes, which helps when writing plugins.

Tree basics

Use cases

Where rehype fits

TaskTypical pluginsLearn more
Render user-submitted HTML safelyrehype-sanitizeSanitize HTML safely
Docs and blog pipelines from Markdownremark-rehype, rehype-slug, rehype-autolink-headings, rehype-highlightMarkdown to safe HTML
Production HTML outputrehype-preset-minifyFormat and minify
Readable HTML for review or diffsrehype-formatHTML Formatter
Migrate HTML content to Markdownrehype-remark, remark-stringifyHTML to Markdown
MDX and Next.js contentany rehype plugin via rehypePluginsrehype with MDX
Custom checks (missing alt text, banned tags)your own pluginBuild a plugin

When rehype is not the right tool

  • Live DOM manipulation in the browser. rehype works on a tree in memory, not on the page's live DOM. For changing the current page, use DOM APIs. (The related rehype-dom packages parse and serialize with browser APIs, for smaller bundles when you trust the content.)
  • CSS or JavaScript processing. rehype handles HTML. Some minify plugins call CSS and JavaScript minifiers for inline code, but for stylesheets and scripts, use dedicated tools.
  • Scraping with selectors only. If you only need to query elements, a selector library may be simpler, although hast-util-select brings CSS selectors to hast.
  • CommonJS-only toolchains on old Node.js. rehype is ESM only. Node.js 20.19+ and 22.12+ can require() it, older versions cannot. See troubleshooting.

Try the features without installing anything

Every tool on this site runs a real rehype pipeline in your browser.

Use ↑ ↓ to move, Enter to open.