rehype-dom-stringify
!Buildbuild-badgebuild
!Coveragecoverage-badgecoverage
!Downloadsdownloads-badgedownloads
!Sizesize-badgesize
!Sponsorssponsors-badgecollective
!Backersbackers-badgecollective
!Chatchat-badgechatrehype plugin to add support for serializing HTML in browsers.
Contents
* [`unified().use(rehypeDomStringify[, options])`](#unifieduserehypedomstringify-options)
What is this?
This is likerehype-stringify
rehype-stringify but for browsers.
This plugin uses DOM APIs to do its work, which makes it smaller in browsers, at
the cost of not supporting formatting options.When should I use this?
Use this package when you want to userehype-stringify
solely in browsers.
See the monorepo readmerehype-dom for info on when to use rehype-dom
.This plugin is built on
hast-util-to-dom
hast-util-to-dom, which is a low
level tool to turn hast syntax trees into DOM 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 rehype-dom-stringify
In Deno with Skypack:
import rehypeDomStringify from 'https://cdn.skypack.dev/rehype-dom-stringify@3?dts'
In browsers with Skypack:
<script type="module">
import rehypeDomStringify from 'https://cdn.skypack.dev/rehype-dom-stringify@3?min'
</script>
Use
Say our pageexample.html
looks as follows:<!doctype html>
<title>Example</title>
<body>
<script type="module">
import {unified} from 'https://cdn.skypack.dev/unified@10?min'
import remarkParse from 'https://cdn.skypack.dev/remark-parse@10?min'
import remarkRehype from 'https://cdn.skypack.dev/remark-rehype@10?min'
import rehypeDomStringify from 'https://cdn.skypack.dev/rehype-dom-stringify@3?min'
const file = await unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeDomStringify)
.process('# Hello, world!')
console.log(String(file))
</script>
Now running
open example.html
prints the following to the console:<h1>Hello, world!</h1>
API
This package exports no identifiers. The default export isrehypeDomStringify
.unified().use(rehypeDomStringify[, options])
Add support for serializing HTML.options
Configuration (optional).options.fragment
Specify whether to serialize a fragment (boolean
, default: true
), instead of
a complete document.
In document mode, an html
element is added when needed.👉 Note: the default of thefragment
option istrue
in this package, which is different from the value inrehype-stringify
, because it makes more sense in browsers.
Types
This package is fully typed with TypeScript. The extra typeOptions
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 ofrehype-dom-stringify
can open you up to a
cross-site scripting (XSS)xss attack if the result is used with the actual
DOM.
Use rehype-sanitize
rehype-sanitize to solve that.Contribute
Seecontributing.md
contributing in rehypejs/.github
health for ways
to get started.
See support.md
support for ways to get help.This project has a code of conductcoc. By interacting with this repository, organisation, or community you agree to abide by its terms.