unified

Project: rehypejs/rehype-dom

Package: rehype-dom-parse@4.0.2

  1. Dependents: 0
  2. rehype plugin to use browser APIs to parse HTML
  1. unified 181
  2. html 124
  3. rehype 91
  4. tree 44
  5. ast 37
  6. syntax 28
  7. parse 24
  8. abstract 10
  9. dom 9

rehype-dom-parse

Build Coverage Downloads Size Sponsors Backers Chat

rehype plugin to add support for parsing HTML input in browsers.

Contents

What is this?

This is like rehype-parse but for browsers. This plugin uses DOM APIs to do its work, which makes it smaller in browsers, at the cost of not supporting positional info on nodes.

When should I use this?

Use this package when you want to use rehype-parse solely in browsers. See the monorepo readme for info on when to use rehype-dom.

This plugin is built on hast-util-from-dom, which is a low level tool to turn DOM nodes into hast syntax trees. 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 rehype-dom-parse

In Deno with esm.sh:

import rehypeDomParse from 'https://esm.sh/rehype-dom-parse@4'

In browsers with esm.sh:

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

Use

Say our page example.html looks as follows:

<!doctype html>
<title>Example</title>
<body>
<script type="module">
  import {unified} from 'https://esm.sh/unified@10?bundle'
  import rehypeDomParse from 'https://esm.sh/rehype-dom-parse@4?bundle'
  import rehypeRemark from 'https://esm.sh/rehype-remark@8?bundle'
  import remarkStringify from 'https://esm.sh/remark-stringify@10?bundle'

  const file = await unified()
    .use(rehypeDomParse)
    .use(rehypeRemark)
    .use(remarkStringify)
    .process(`<h1>Hi</h1>
<p><em>Hello</em>, world!</p>`)

  console.log(String(file))
</script>

Now running open example.html prints the following to the console:

# Hi

*Hello*, world!

API

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

unified.use(rehypeDomParse[, options])

Add support for parsing HTML input.

options

Configuration (optional).

options.fragment

Specify whether to parse a fragment (boolean, default: true), instead of a complete document. In document mode, unopened html, head, and body elements are opened in just the right places.

👉 Note: the default of the fragment option is true in this package, which is different from the value in rehype-parse, because it makes more sense in browsers.

Types

This package is fully typed with TypeScript. The extra type Options is 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

Use of rehype-dom-parse itself is safe but see rehype, rehype-dom, or rehype-dom-stringify for more information on potential security problems.

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, organisation, or community you agree to abide by its terms.

License

ISC © Keith McKnight