rehype-dom-parse
Advanced tools
Comparing version
@@ -8,2 +8,3 @@ // This wrapper exists because JS in TS can’t export a `@type` of a function. | ||
export default rehypeDomParse | ||
export type {Options} | ||
export {Options} from './lib/index.js' |
@@ -1,3 +0,1 @@ | ||
import rehypeDomParse from './lib/index.js' | ||
export default rehypeDomParse | ||
export {default} from './lib/index.js' |
@@ -1,2 +0,2 @@ | ||
/** @type {import('unified').Plugin<[Options?] | void[], string, Root>} */ | ||
/** @type {import('unified').Plugin<[Options?] | Array<void>, string, Root>} */ | ||
export default function parse(options: void | Options | undefined): void | ||
@@ -3,0 +3,0 @@ export type Root = import('hast').Root |
@@ -14,3 +14,3 @@ /** | ||
/** @type {import('unified').Plugin<[Options?] | void[], string, Root>} */ | ||
/** @type {import('unified').Plugin<[Options?] | Array<void>, string, Root>} */ | ||
export default function parse(options) { | ||
@@ -17,0 +17,0 @@ const settings = /** @type {Options} */ (this.data('settings')) |
{ | ||
"name": "rehype-dom-parse", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"description": "rehype plugin to use browser APIs to parse HTML", | ||
@@ -36,3 +36,3 @@ "license": "ISC", | ||
"@types/hast": "^2.0.0", | ||
"@types/web": "^0.0.12", | ||
"@types/web": "^0.0.50", | ||
"hast-util-from-dom": "^4.0.0", | ||
@@ -39,0 +39,0 @@ "unified": "^10.0.0" |
146
readme.md
@@ -11,21 +11,39 @@ # rehype-dom-parse | ||
[**rehype**][rehype] plugin to use browser APIs to parse HTML. | ||
[Parser][] for [**unified**][unified]. | ||
Used in the [**rehype-dom** processor][processor]. | ||
**[rehype][]** plugin to add support for parsing HTML input in browsers. | ||
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-parse`][rehype-parse] | ||
instead. | ||
## Contents | ||
As `rehype-dom-parse` is designed for browser use, it defaults to parsing in | ||
**fragment mode**, whereas [`rehype-parse`][rehype-parse] defaults to **document | ||
mode**! | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`unified.use(rehypeDomParse[, options])`](#unifieduserehypedomparse-options) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Security](#security) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
## What is this? | ||
This is like [`rehype-parse`][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][rehype-dom] for info on when to use `rehype-dom`. | ||
This plugin is built on [`hast-util-from-dom`][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](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): | ||
Node 12+ is needed to use it and it must be `import`ed instead of `require`d. | ||
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c). | ||
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]: | ||
[npm][]: | ||
```sh | ||
@@ -35,25 +53,49 @@ npm install rehype-dom-parse | ||
## Use | ||
In Deno with [Skypack][]: | ||
```js | ||
import {unified} from 'unified' | ||
import rehypeDomParse from 'rehype-dom-parse' | ||
import rehypeDomStringify from 'rehype-dom-stringify' | ||
import rehypeDomParse from 'https://cdn.skypack.dev/rehype-dom-parse@4?dts' | ||
``` | ||
unified() | ||
.use(rehypeDomParse) | ||
.use(rehypeDomStringify) | ||
.data('settings', {fragment: true}) | ||
In browsers with [Skypack][]: | ||
processor.process('<p>text, <b>hyper').then((file) => { | ||
console.log(String(file)) | ||
}) | ||
```html | ||
<script type="module"> | ||
import rehypeDomParse from 'https://cdn.skypack.dev/rehype-dom-parse@4?min' | ||
</script> | ||
``` | ||
Yields: | ||
## Use | ||
Say our page `example.html` looks as follows: | ||
```html | ||
<p>text, <b>hyper</b></p> | ||
<!doctype html> | ||
<title>Example</title> | ||
<body> | ||
<script type="module"> | ||
import {unified} from 'https://cdn.skypack.dev/unified@10?min' | ||
import rehypeDomParse from 'https://cdn.skypack.dev/rehype-dom-parse@4?min' | ||
import rehypeRemark from 'https://cdn.skypack.dev/rehype-remark@8?min' | ||
import remarkStringify from 'https://cdn.skypack.dev/remark-stringify@10?min' | ||
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: | ||
```markdown | ||
# Hi | ||
*Hello*, world! | ||
``` | ||
## API | ||
@@ -66,7 +108,8 @@ | ||
Configure a processor to parse HTML as input and transform it to | ||
[**hast**][hast]. | ||
Add support for parsing HTML input. | ||
##### `options` | ||
Configuration (optional). | ||
###### `options.fragment` | ||
@@ -79,7 +122,22 @@ | ||
> 👉 **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` can open you up to a [cross-site scripting (XSS)][xss] | ||
attack if the DOM is unsafe. | ||
Use [`rehype-sanitize`][sanitize] to make the tree safe. | ||
Use of `rehype-dom-parse` itself is safe but see `rehype`, `rehype-dom`, or | ||
`rehype-dom-stringify` for more information on potential security problems. | ||
@@ -92,3 +150,3 @@ ## Contribute | ||
This project has a [Code of Conduct][coc]. | ||
This project has a [code of conduct][coc]. | ||
By interacting with this repository, organisation, or community you agree to | ||
@@ -131,2 +189,4 @@ abide by its terms. | ||
[skypack]: https://www.skypack.dev | ||
[author]: https://keith.mcknig.ht | ||
@@ -136,24 +196,18 @@ | ||
[typescript]: https://www.typescriptlang.org | ||
[health]: https://github.com/rehypejs/.github | ||
[contributing]: https://github.com/rehypejs/.github/blob/HEAD/contributing.md | ||
[contributing]: https://github.com/rehypejs/.github/blob/main/contributing.md | ||
[support]: https://github.com/rehypejs/.github/blob/HEAD/support.md | ||
[support]: https://github.com/rehypejs/.github/blob/main/support.md | ||
[coc]: https://github.com/rehypejs/.github/blob/HEAD/code-of-conduct.md | ||
[coc]: https://github.com/rehypejs/.github/blob/main/code-of-conduct.md | ||
[unified]: https://github.com/unifiedjs/unified | ||
[processor]: https://github.com/rehypejs/rehype/blob/HEAD/packages/rehype | ||
[parser]: https://github.com/unifiedjs/unified#processorparser | ||
[hast]: https://github.com/syntax-tree/hast | ||
[rehype]: https://github.com/rehypejs/rehype | ||
[rehype-parse]: https://github.com/rehypejs/rehype/tree/HEAD/packages/rehype-parse | ||
[rehype-dom]: https://github.com/rehypejs/rehype-dom | ||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting | ||
[rehype-parse]: https://github.com/rehypejs/rehype/tree/main/packages/rehype-parse | ||
[sanitize]: https://github.com/rehypejs/rehype-sanitize | ||
[hast-util-from-dom]: https://github.com/syntax-tree/hast-util-from-dom |
9333
19.81%207
35.29%70
-1.41%+ Added
- Removed
Updated