rehype vs remark vs unified
Markdown and HTML Processing Libraries
rehyperemarkunifiedSimilar Packages:

Markdown and HTML Processing Libraries

Rehype, Remark, and Unified are powerful libraries in the JavaScript ecosystem designed for processing HTML and Markdown content. They provide a unified interface for transforming and manipulating content, allowing developers to parse, modify, and serialize documents efficiently. These libraries are built on the concept of a processor that can handle various transformations, making them highly extensible and suitable for a wide range of applications, from static site generation to content management systems.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
rehype02,20313.3 kB22 years agoMIT
remark08,81315.7 kB83 years agoMIT
unified04,965146 kB12 years agoMIT

Feature Comparison: rehype vs remark vs unified

Processing Focus

  • rehype:

    Rehype is focused on HTML processing, providing tools to parse, manipulate, and serialize HTML documents. It supports various plugins for tasks like sanitization, minification, and transformation, making it a robust choice for web applications that rely heavily on HTML content.

  • remark:

    Remark is dedicated to Markdown processing. It allows for parsing Markdown into an abstract syntax tree (AST) and transforming it into different formats, such as HTML. Remark's ecosystem includes numerous plugins for linting, formatting, and extending Markdown capabilities, making it ideal for Markdown-centric applications.

  • unified:

    Unified serves as a framework that unifies the processing of different formats, including Markdown and HTML. It allows developers to create custom processors that can handle multiple types of content, providing a flexible approach to content transformation.

Extensibility

  • rehype:

    Rehype is highly extensible through its plugin system, allowing developers to add custom functionality or modify existing behavior. You can easily integrate third-party plugins or create your own to enhance HTML processing capabilities, making it adaptable to various project requirements.

  • remark:

    Remark also features a rich plugin ecosystem, enabling developers to extend its functionality for Markdown processing. You can use existing plugins for linting, formatting, or even creating custom transformations, providing flexibility in how Markdown content is handled.

  • unified:

    Unified is designed to be extensible at its core, allowing for the creation of custom processors that can handle different formats and transformations. Its architecture supports combining multiple processors, making it a powerful tool for complex content workflows.

Learning Curve

  • rehype:

    Rehype has a moderate learning curve, especially for those familiar with HTML. Understanding its plugin system and how to manipulate the DOM structure may require some initial investment, but the documentation is comprehensive and helpful for new users.

  • remark:

    Remark is relatively easy to learn for developers familiar with Markdown. Its API is straightforward, and the ability to create custom transformations is well-documented, making it accessible for those looking to enhance Markdown processing.

  • unified:

    Unified has a steeper learning curve due to its abstract nature and the need to understand how to compose processors. However, once grasped, it offers immense power and flexibility for handling various content types, making it worthwhile for complex projects.

Performance

  • rehype:

    Rehype is optimized for performance when processing HTML documents. Its design allows for efficient manipulation of the DOM, and with the right plugins, it can handle large HTML files without significant performance degradation.

  • remark:

    Remark is designed to efficiently parse and transform Markdown content. Its use of an AST allows for quick transformations, and performance is generally good, even with larger Markdown files, especially when using optimized plugins.

  • unified:

    Unified's performance can vary depending on the complexity of the processors used. While it can handle large documents effectively, the performance will depend on how well the processors are optimized and composed, making it essential to consider performance when designing workflows.

Use Cases

  • rehype:

    Rehype is ideal for applications that require extensive HTML manipulation, such as static site generators, content management systems, or any web application that needs to sanitize and transform HTML content before rendering.

  • remark:

    Remark is best suited for applications that focus on Markdown content, such as blogging platforms, documentation sites, or any project that requires Markdown editing and rendering capabilities.

  • unified:

    Unified is perfect for projects that need to handle multiple content formats, allowing for seamless integration of Markdown and HTML processing. It is suitable for complex applications that require a combination of both formats in their workflows.

How to Choose: rehype vs remark vs unified

  • rehype:

    Choose Rehype if your primary focus is on processing HTML. It is optimized for transforming HTML documents, enabling you to manipulate the structure and content of HTML easily. Rehype is ideal for projects that require HTML sanitization, transformation, or rendering.

  • remark:

    Choose Remark if you are specifically working with Markdown. It excels in parsing and transforming Markdown content into various formats, such as HTML. Remark is perfect for projects that involve Markdown content creation, editing, or rendering, especially in documentation or blogging platforms.

  • unified:

    Choose Unified if you need a versatile solution that can handle both Markdown and HTML. Unified provides a framework for composing processors that can work with multiple formats, allowing for complex transformations and integrations. It is suitable for projects that require a mix of Markdown and HTML processing.

README for rehype

rehype

Build Coverage Downloads Size Sponsors Backers Chat

unified processor to add support for parsing from HTML and serializing to HTML.

Contents

What is this?

This package is a unified processor with support for parsing HTML as input and serializing HTML as output by using unified with rehype-parse and rehype-stringify.

See the monorepo readme for info on what the rehype ecosystem is.

When should I use this?

You can use this package when you want to use unified, have HTML as input, and want HTML as output. This package is a shortcut for unified().use(rehypeParse).use(rehypeStringify). When the input isn’t HTML (meaning you don’t need rehype-parse) or the output is not HTML (you don’t need rehype-stringify), it’s recommended to use unified directly.

When you’re in a browser, trust your content, don’t need positional info on nodes or formatting options, and value a smaller bundle size, you can use rehype-dom instead.

When you want to inspect and format HTML files in a project on the command line, you can use rehype-cli.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install rehype

In Deno with esm.sh:

import {rehype} from 'https://esm.sh/rehype@13'

In browsers with esm.sh:

<script type="module">
  import {rehype} from 'https://esm.sh/rehype@13?bundle'
</script>

Use

Say we have the following module example.js:

import {rehype} from 'rehype'
import rehypeFormat from 'rehype-format'

const file = await rehype().use(rehypeFormat).process(`<!doctype html>
        <html lang=en>
<head>
    <title>Hi!</title>
  </head>
  <body>
    <h1>Hello!</h1>

</body></html>`)

console.error(String(file))

…running that with node example.js yields:

<!doctype html>
<html lang="en">
  <head>
    <title>Hi!</title>
  </head>
  <body>
    <h1>Hello!</h1>
  </body>
</html>

API

This package exports the identifier rehype. There is no default export.

rehype()

Create a new unified processor that already uses rehype-parse and rehype-stringify.

You can add more plugins with use. See unified for more information.

Examples

Example: passing options to rehype-parse, rehype-stringify

When you use rehype-parse or rehype-stringify manually you can pass options directly to them with use. Because both plugins are already used in rehype, that’s not possible. To define options for them, you can instead pass options to data:

import {rehype} from 'rehype'
import {reporter} from 'vfile-reporter'

const file = await rehype()
  .data('settings', {
    emitParseErrors: true,
    fragment: true,
    preferUnquoted: true
  })
  .process('<div title="a" title="b"></div>')

console.error(reporter(file))
console.log(String(file))

…yields:

1:21-1:21 warning Unexpected duplicate attribute duplicate-attribute hast-util-from-html

⚠ 1 warning
<div title=a></div>

Syntax

HTML is parsed and serialized according to WHATWG HTML (the living standard), which is also followed by all browsers.

Syntax tree

The syntax tree format used in rehype is hast.

Types

This package is fully typed with TypeScript. It exports no additional types.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, rehype@^13, compatible with Node.js 16.

Security

As rehype works on HTML, and improper use of HTML can open you up to a cross-site scripting (XSS) attack, use of rehype can also be unsafe. Use rehype-sanitize to make the tree safe.

Use of rehype plugins could also open you up to other attacks. Carefully assess each plugin and the risks involved in using them.

For info on how to submit a report, see our security policy.

Contribute

See contributing.md in rehypejs/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

Sponsor

Support this effort and give back by sponsoring on OpenCollective!

Vercel

Motif

HashiCorp

GitBook

Gatsby

Netlify

Coinbase

ThemeIsle

Expo

Boost Note

Markdown Space

Holloway


You?

License

MIT © Titus Wormer