Rehype vs Remark: Key Differences and Use Cases

rehype handles HTML, remark handles Markdown. How they differ, and how to move content between them in one pipeline.

rehype and remark are sibling projects in the unified collective. They look alike because they share one engine, but they work on different languages. Picking the right one, and knowing how to combine them, saves a lot of confusion.

The short answer

remarkrehype
ProcessesMarkdownHTML
Tree formatmdast (headings, paragraphs, lists, links…)hast (elements with tag names and properties)
Parser / serializerremark-parse / remark-stringifyrehype-parse / rehype-stringify
Typical pluginsGFM tables, frontmatter, table of contents, linting Markdown stylesanitizing, syntax highlighting, heading anchors, minifying
Bridgeremark-rehype (Markdown → HTML)rehype-remark (HTML → Markdown)

Different trees for different languages

mdast describes Markdown concepts: a heading node with a depth, a link with a url, an emphasis. hast describes HTML: an element with tagName: 'h2' and properties. Markdown has no idea what a class attribute is, and HTML has no special node for “list item with a checkbox”. That is why plugins for one do not work on the other, and why some tasks belong naturally to one side:

  • Work on the Markdown side (remark) when the change is about Markdown syntax or document structure as written: enabling GFM, reading frontmatter, generating a table of contents from headings.
  • Work on the HTML side (rehype) when the change is about the output markup: attributes, classes, safety, highlighting, size.

Markdown to HTML: remark, then rehype

remark-rehype converts mdast into hast in the middle of a pipeline. Everything before it is a remark plugin; everything after it is a rehype plugin:

markdown-to-html.mjs
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeSanitize from 'rehype-sanitize'
import rehypeStringify from 'rehype-stringify'

const markdown = `# Release notes

Read the [docs](https://unifiedjs.com) or *skip ahead*.`

const file = await unified()
  .use(remarkParse) // markdown -> mdast
  .use(remarkRehype) // mdast -> hast
  .use(rehypeSanitize) // clean the HTML tree
  .use(rehypeStringify) // hast -> HTML
  .process(markdown)

console.log(String(file))
Output node markdown-to-html.mjs
<h1>Release notes</h1>
<p>Read the <a href="https://unifiedjs.com">docs</a> or <em>skip ahead</em>.</p>

This is the backbone of most Markdown-based blogs and docs sites. Note that raw HTML inside Markdown is dropped by default during the conversion; enabling it safely requires allowDangerousHtml, rehype-raw and rehype-sanitize.

HTML to Markdown: rehype, then remark

The other direction is useful for migrating content out of a CMS or saving web pages as Markdown. rehype-remark converts hast into mdast:

html-to-markdown.mjs
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeRemark from 'rehype-remark'
import remarkStringify from 'remark-stringify'

const html = `<h2>Changelog</h2>
<p>Fixed <strong>two</strong> bugs. See <a href="https://github.com/rehypejs/rehype">the repo</a>.</p>
<ul><li>Parser</li><li>CLI</li></ul>`

const file = await unified()
  .use(rehypeParse)
  .use(rehypeRemark)
  .use(remarkStringify)
  .process(html)

console.log(String(file))
Output node html-to-markdown.mjs
## Changelog

Fixed **two** bugs. See [the repo](https://github.com/rehypejs/rehype).

* Parser
* CLI

Try it with your own HTML in the HTML to Markdown converter, which also enables GFM tables.

Which one do I install?

  • Your source is HTML and your output is HTML: rehype only.
  • Your source is Markdown and your output is Markdown (for example, formatting .md files): remark only.
  • Your source is Markdown and your output is HTML: both, using unified with remark-parse, remark-rehype and rehype-stringify.
  • Your source is HTML and your output is Markdown: both, with rehype-parse, rehype-remark and remark-stringify.

When you combine them, import unified directly instead of the rehype or remark shortcut packages, as the examples do. The rehype readme itself recommends this when input or output is not HTML.

Sources and further reading

Use ↑ ↓ to move, Enter to open.