npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@stackline/rehype-parse

v1.0.2

Published

rehype plugin to parse HTML

Readme

@stackline/rehype-parse

rehype plugin to parse HTML.

npm version license GitHub repository Docs Reddit community

Documentation | npm | Issues | Repository

Current package version: 1.0.2


Why this package?

@stackline/rehype-parse is the Stackline-maintained distribution of [email protected]. It is an independent continuation of rehype-parse; original authors and licenses remain credited below.

Compatibility

| Item | Value | | :--- | :--- | | Package | @stackline/[email protected] | | API target | [email protected] | | Supported Node.js | See supported framework requirements | | License | MIT | | Module type | module | | Main entry | index.js | | Types | index.d.ts | | Runtime dependencies | parse5, unified, @types/hast, hast-util-from-parse5 |

Installation

npm install @stackline/rehype-parse

Preserve existing imports and plugin resolution with an npm alias:

npm install rehype-parse@npm:@stackline/rehype-parse

Usage and API reference

rehype-parse

rehype plugin to add support for parsing HTML input.

Contents

What is this?

This package is a unified (rehype) plugin that defines how to take HTML as input and turn it into a syntax tree. When it’s used, HTML can be parsed and other rehype plugins can be used after it.

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

When should I use this?

This plugin adds support to unified for parsing HTML. You can alternatively use rehype instead, which combines unified, this plugin, and rehype-stringify.

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

This plugin is built on parse5 and hast-util-from-parse5, which deal with HTML-compliant tokenizing, parsing, and creating nodes. rehype focusses on making it easier to transform content by abstracting such internals away.

Install

This package is ESM only. In Node.js (version 12.20+, 14.14+, or 16.0+), install with npm:

npm install @stackline/rehype-parse

In Deno with esm.sh:

import rehypeParse from 'https://esm.sh/rehype-parse@8'

In browsers with esm.sh:

<script type="module">
  import rehypeParse from 'https://esm.sh/rehype-parse@8?bundle'
</script>

Use

Say we have the following module example.js:

import {unified} from 'unified'
import rehypeParse from '@stackline/rehype-parse'
import rehypeRemark from 'rehype-remark'
import remarkStringify from 'remark-stringify'

main()

async function main() {
  const file = await unified()
    .use(rehypeParse)
    .use(rehypeRemark)
    .use(remarkStringify)
    .process('<h1>Hello, world!</h1>')

  console.log(String(file))
}

…running that with node example.js yields:

# Hello, world!

API

This package exports no identifiers. The default export is rehypeParse.

unified().use(rehypeParse[, options])

Add support for parsing HTML input.

options

Configuration (optional).

options.fragment

Specify whether to parse as a fragment (boolean, default: false). The default is to expect a whole document. In document mode, unopened html, head, and body elements are opened.

options.space

Which space the document is in ('svg' or 'html', default: 'html').

When an <svg> element is found in the HTML space, rehype-parse already automatically switches to and from the SVG space when entering and exiting it.

👉 Note: rehype is not an XML parser. It supports SVG as embedded in HTML. It does not support the features available in XML. Passing SVG files might break but fragments of modern SVG should be fine.

👉 Note: make sure to set fragment: true if space: 'svg'.

options.emitParseErrors

Emit HTML parse errors as warning messages (boolean, default: false).

Specific rules can be turned off by setting their IDs in options to false (or 0). The default, when emitParseErrors: true, is true (or 1), and means that rules emit as warnings. Rules can also be configured with 2, to turn them into fatal errors.

The list of parse errors:

options.verbose

Add extra positional info (boolean, default: false).

Examples

Example: fragment versus document

The following example shows the difference between parsing as a document and parsing as a fragment:

import {unified} from 'unified'
import rehypeParse from '@stackline/rehype-parse'
import rehypeStringify from 'rehype-stringify'

main()

async function main() {
  const doc = '<title>Hi!</title><h1>Hello!</h1>'

  console.log(
    String(
      await unified()
        .use(rehypeParse, {fragment: true})
        .use(rehypeStringify)
        .process(doc)
    )
  )

  console.log(
    String(
      await unified()
        .use(rehypeParse, {fragment: false})
        .use(rehypeStringify)
        .process(doc)
    )
  )
}

…yields:

<title>Hi!</title><h1>Hello!</h1>
<html><head><title>Hi!</title></head><body><h1>Hello!</h1></body></html>

👉 Note: observe that when a whole document is expected (second example), missing elements are opened and closed.

Example: whitespace around and inside <html>

The following example shows how whitespace is handled when around and directly inside the <html> element:

import {unified} from 'unified'
import rehypeParse from '@stackline/rehype-parse'
import rehypeStringify from 'rehype-stringify'

main(`<!doctype html>
<html lang=en>
  <head>
    <title>Hi!</title>
  </head>
  <body>
    <h1>Hello!</h1>
  </body>
</html>`)

async function main(doc) {
  console.log(
    String(await unified().use(rehypeParse).use(rehypeStringify).process(doc))
  )
}

…yields (where ␠ represents a space character):

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

👉 Note: observe that the line ending before <html> is ignored, the line ending and two spaces before <head> is moved inside it, and the line ending after </body> is moved before it.

This behavior is described by the HTML standard (see the section 13.2.6.4.1 “The ‘initial’ insertion mode” and adjacent states) which rehype follows.

The changes to this meaningless whitespace should not matter, except when formatting markup, in which case rehype-format can be used to improve the source code.

Example: parse errors

The following example shows how HTML parse errors can be enabled and configured:

import {reporter} from 'vfile-reporter'
import {unified} from 'unified'
import rehypeParse from '@stackline/rehype-parse'
import rehypeStringify from 'rehype-stringify'

main()

async function main() {
  const file = await unified()
    .use(rehypeParse, {
      emitParseErrors: true, // Emit all.
      missingWhitespaceBeforeDoctypeName: 2, // Mark one as a fatal error.
      nonVoidHtmlElementStartTagWithTrailingSolidus: false // Ignore one.
    })
    .use(rehypeStringify)
    .process(`<!doctypehtml>
<title class="a" class="b">Hello…</title>
<h1/>World!</h1>`)

  console.log(reporter(file))
}

…yields:

  1:10-1:10  error    Missing whitespace before doctype name  missing-whitespace-before-doctype-name  parse-error
  2:23-2:23  warning  Unexpected duplicate attribute          duplicate-attribute                     parse-error

2 messages (✖ 1 error, ⚠ 1 warning)

🧑‍🏫 Info: messages in unified are warnings instead of errors. Other linters (such as ESLint) almost always use errors. Why? Those tools only check code style. They don’t generate, transform, and format code, which is what rehype and unified focus on, too. Errors in unified mean the same as an exception in your JavaScript code: a crash. That’s why we use warnings instead, because we continue checking more HTML and continue running more plugins.

Syntax

HTML is parsed according to WHATWG HTML (the living standard), which is also followed by browsers such as Chrome and Firefox.

Syntax tree

The syntax tree format used in rehype is hast.

Types

This package is fully typed with TypeScript. The extra types Options, ErrorCode, and ErrorSeverity are exported.

Compatibility

Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 12.20+, 14.14+, and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.

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!

License

MIT © Titus Wormer

Credits and original authors

Original copyright, license notices and contributor acknowledgements remain part of this distribution. Stackline maintenance does not replace authorship of the original work.

Community and Links

Use this repository's issue tracker for reproducible bugs and feature requests. Join r/Stackline for examples, usage questions and release discussions.