unified

Project:rehypejs/rehype-dom

Package:rehype-dom-stringify@2.0.1

  1. Dependencies:3·Dependents:2
  2. rehype plugin to use browser APIs to stringify HTML
  1. unified 135
  2. html 100
  3. rehype 70
  4. tree 40
  5. syntax 30
  6. ast 27
  7. stringify 16
  8. abstract 10
  9. dom 9
  10. compile 9

rehype-dom-stringify

Build Coverage Downloads Size Sponsors Backers Chat

rehype plugin to use browser APIs to stringify HTML. Compiler for unified. Used in the rehype-dom processor.

If you don’t care about bundle size, or if you don’t trust content, or if you’re not in a (modern) browser environment, use rehype-stringify instead.

As rehype-dom-stringify is designed for browser use, it defaults to parsing in fragment mode, whereas rehype-stringify defaults to document mode!

Install

yarn:

yarn add rehype-dom-stringify

npm:

npm install rehype-dom-stringify

Use

import unified from 'unified';
import createStream from 'unified-stream';
import parse from 'rehype-dom-parse';
import stringify from 'rehype-dom-stringify';

const processor = unified()
  .use(parse)
  .use(stringify)
  .data('settings', { fragment: true })

processor.process('<p>text, <b>hyper', (err, file) => {
  if (err) {
    console.error(err);
  } else {
    console.log(String(file));
  }
});

Yields:

<p>text, <b>hyper</b></p>

API

processor.use(stringify[, options])

Configure processor to take hast as input and stringify it to HTML.

options
options.fragment

Specify whether to stringify a fragment (boolean, default: true), instead of a complete document. In document mode, an html element is added to a fragment when needed.

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