Socket
Socket
Sign inDemoInstall

rehype-parse

Package Overview
Dependencies
23
Maintainers
2
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 8.0.5 to 9.0.0

25

index.d.ts

@@ -1,7 +0,24 @@

// This wrapper exists because JS in TS can’t export a `@type` of a function.
import type {Options, ErrorCode, ErrorSeverity} from './lib/index.js'
import type {Root} from 'hast'
import type {Plugin} from 'unified'
declare const rehypeParse: Plugin<[Options?] | Array<void>, string, Root>
import type {Options} from './lib/index.js'
export type {ErrorCode, ErrorSeverity} from 'hast-util-from-html'
export type {Options} from './lib/index.js'
/**
* Plugin to add support for parsing from HTML.
*
* @this
* Unified processor.
* @param
* Configuration (optional).
* @returns
* Nothing.
*/
declare const rehypeParse: Plugin<[(Options | null | undefined)?], string, Root>
export default rehypeParse
export type {Options, ErrorCode, ErrorSeverity}
// Add custom settings supported when `rehype-parse` is added.
declare module 'unified' {
interface Settings extends Options {}
}

@@ -0,1 +1,2 @@

// Note: types exposed from `index.d.ts`.
export {default} from './lib/index.js'

343

lib/index.d.ts

@@ -1,292 +0,53 @@

export default function rehypeParse(
this: import('unified').Processor<
import('hast').Root,
import('hast').Root,
void,
void
>,
...settings: [(Options | undefined)?] | void[]
): void
export type Root = import('hast').Root
export type FromParse5Options = Pick<
import('hast-util-from-parse5').Options,
'space' | 'verbose'
>
export type ErrorCode = keyof {
abandonedHeadElementChild: {
reason: string
description: string
url: boolean
}
abruptClosingOfEmptyComment: {
reason: string
description: string
}
abruptDoctypePublicIdentifier: {
reason: string
description: string
}
abruptDoctypeSystemIdentifier: {
reason: string
description: string
}
absenceOfDigitsInNumericCharacterReference: {
reason: string
description: string
}
cdataInHtmlContent: {
reason: string
description: string
}
characterReferenceOutsideUnicodeRange: {
reason: string
description: string
}
closingOfElementWithOpenChildElements: {
reason: string
description: string
url: boolean
}
controlCharacterInInputStream: {
reason: string
description: string
}
controlCharacterReference: {
reason: string
description: string
}
disallowedContentInNoscriptInHead: {
reason: string
description: string
url: boolean
}
duplicateAttribute: {
reason: string
description: string
}
endTagWithAttributes: {
reason: string
description: string
}
endTagWithTrailingSolidus: {
reason: string
description: string
}
endTagWithoutMatchingOpenElement: {
reason: string
description: string
url: boolean
}
eofBeforeTagName: {
reason: string
description: string
}
eofInCdata: {
reason: string
description: string
}
eofInComment: {
reason: string
description: string
}
eofInDoctype: {
reason: string
description: string
}
eofInElementThatCanContainOnlyText: {
reason: string
description: string
url: boolean
}
eofInScriptHtmlCommentLikeText: {
reason: string
description: string
}
eofInTag: {
reason: string
description: string
}
incorrectlyClosedComment: {
reason: string
description: string
}
incorrectlyOpenedComment: {
reason: string
description: string
}
invalidCharacterSequenceAfterDoctypeName: {
reason: string
description: string
}
invalidFirstCharacterOfTagName: {
reason: string
description: string
}
misplacedDoctype: {
reason: string
description: string
url: boolean
}
misplacedStartTagForHeadElement: {
reason: string
description: string
url: boolean
}
missingAttributeValue: {
reason: string
description: string
}
missingDoctype: {
reason: string
description: string
url: boolean
}
missingDoctypeName: {
reason: string
description: string
}
missingDoctypePublicIdentifier: {
reason: string
description: string
}
missingDoctypeSystemIdentifier: {
reason: string
description: string
}
missingEndTagName: {
reason: string
description: string
}
missingQuoteBeforeDoctypePublicIdentifier: {
reason: string
description: string
}
missingQuoteBeforeDoctypeSystemIdentifier: {
reason: string
description: string
}
missingSemicolonAfterCharacterReference: {
reason: string
description: string
}
missingWhitespaceAfterDoctypePublicKeyword: {
reason: string
description: string
}
missingWhitespaceAfterDoctypeSystemKeyword: {
reason: string
description: string
}
missingWhitespaceBeforeDoctypeName: {
reason: string
description: string
}
missingWhitespaceBetweenAttributes: {
reason: string
description: string
}
missingWhitespaceBetweenDoctypePublicAndSystemIdentifiers: {
reason: string
description: string
}
nestedComment: {
reason: string
description: string
}
nestedNoscriptInHead: {
reason: string
description: string
url: boolean
}
nonConformingDoctype: {
reason: string
description: string
url: boolean
}
nonVoidHtmlElementStartTagWithTrailingSolidus: {
reason: string
description: string
}
noncharacterCharacterReference: {
reason: string
description: string
}
noncharacterInInputStream: {
reason: string
description: string
}
nullCharacterReference: {
reason: string
description: string
}
openElementsLeftAfterEof: {
reason: string
description: string
url: boolean
}
surrogateCharacterReference: {
reason: string
description: string
}
surrogateInInputStream: {
reason: string
description: string
}
unexpectedCharacterAfterDoctypeSystemIdentifier: {
reason: string
description: string
}
unexpectedCharacterInAttributeName: {
reason: string
description: string
}
unexpectedCharacterInUnquotedAttributeValue: {
reason: string
description: string
}
unexpectedEqualsSignBeforeAttributeName: {
reason: string
description: string
}
unexpectedNullCharacter: {
reason: string
description: string
}
unexpectedQuestionMarkInsteadOfTagName: {
reason: string
description: string
}
unexpectedSolidusInTag: {
reason: string
description: string
}
unknownNamedCharacterReference: {
reason: string
description: string
}
/**
* Plugin to add support for parsing from HTML.
*
* > 👉 **Note**: this 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.
* > Use [`xast-util-from-xml`][xast-util-from-xml] to parse XML.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
export default function rehypeParse(options?: Options | null | undefined): undefined;
export default class rehypeParse {
/**
* Plugin to add support for parsing from HTML.
*
* > 👉 **Note**: this 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.
* > Use [`xast-util-from-xml`][xast-util-from-xml] to parse XML.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
constructor(options?: Options | null | undefined);
parser: (document: string, file: import("vfile").VFile) => import("hast").Root;
}
export type ErrorSeverity = 0 | 1 | 2 | boolean | null | undefined
export type ErrorFields = Partial<Record<ErrorCode, ErrorSeverity>>
export type ParseFields = {
/**
* Specify whether to parse a fragment, instead of a complete document.
* In document mode, unopened `html`, `head`, and `body` elements are opened
* in just the right places.
*/
fragment?: boolean | undefined
/**
* > ⚠️ Parse errors are currently being added to HTML.
* > Not all errors emitted by parse5 (or rehype-parse) are specced yet.
* > Some documentation may still be missing.
*
* Emit parse errors while parsing on the vfile.
* Setting this to `true` starts emitting HTML parse errors.
*
* Specific rules can be turned off by setting them 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.
*/
emitParseErrors?: boolean | undefined
}
export type Options = FromParse5Options & ParseFields & ErrorFields
export type Root = import('hast').Root;
export type FromHtmlOptions = import('hast-util-from-html').Options;
export type Parser = import('unified').Parser<Root>;
/**
* Configuration.
*/
export type Options = Omit<FromHtmlOptions, 'onerror'> & RehypeParseFields;
/**
* Extra fields.
*/
export type RehypeParseFields = {
/**
* Whether to emit parse errors while parsing (default: `false`).
*
* > 👉 **Note**: parse errors are currently being added to HTML.
* > Not all errors emitted by parse5 (or us) are specced yet.
* > Some documentation may still be missing.
*/
emitParseErrors?: boolean | null | undefined;
};
/**
* @typedef {import('hast').Root} Root
* @typedef {Pick<import('hast-util-from-parse5').Options, 'space' | 'verbose'>} FromParse5Options
* @typedef {import('hast-util-from-html').Options} FromHtmlOptions
* @typedef {import('unified').Parser<Root>} Parser
*/
/**
* @typedef {Omit<FromHtmlOptions, 'onerror'> & RehypeParseFields} Options
* Configuration.
*
* @typedef {keyof errors} ErrorCode
* @typedef {0|1|2|boolean|null|undefined} ErrorSeverity
* @typedef {Partial<Record<ErrorCode, ErrorSeverity>>} ErrorFields
* @typedef RehypeParseFields
* Extra fields.
* @property {boolean | null | undefined} [emitParseErrors=false]
* Whether to emit parse errors while parsing (default: `false`).
*
* @typedef ParseFields
* @property {boolean|undefined} [fragment=false]
* Specify whether to parse a fragment, instead of a complete document.
* In document mode, unopened `html`, `head`, and `body` elements are opened
* in just the right places.
* @property {boolean|undefined} [emitParseErrors=false]
* > ⚠️ Parse errors are currently being added to HTML.
* > Not all errors emitted by parse5 (or rehype-parse) are specced yet.
* > 👉 **Note**: parse errors are currently being added to HTML.
* > Not all errors emitted by parse5 (or us) are specced yet.
* > Some documentation may still be missing.
*
* Emit parse errors while parsing on the vfile.
* Setting this to `true` starts emitting HTML parse errors.
*
* Specific rules can be turned off by setting them 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.
*
* @typedef {FromParse5Options & ParseFields & ErrorFields} Options
*/
// @ts-expect-error: remove when typed
import Parser5 from 'parse5/lib/parser/index.js'
import {fromParse5} from 'hast-util-from-parse5'
import {errors} from './errors.js'
import {fromHtml} from 'hast-util-from-html'
const base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
const fatalities = {2: true, 1: false, 0: null}
/**
* @this {import('unified').Processor}
* @type {import('unified').Plugin<[Options?] | Array<void>, string, Root>}
* Plugin to add support for parsing from HTML.
*
* > 👉 **Note**: this 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.
* > Use [`xast-util-from-xml`][xast-util-from-xml] to parse XML.
*
* @param {Options | null | undefined} [options]
* Configuration (optional).
* @returns {undefined}
* Nothing.
*/
export default function rehypeParse(options) {
const processorSettings = /** @type {Options} */ (this.data('settings'))
const settings = Object.assign({}, processorSettings, options)
/** @type {import('unified').Processor<Root>} */
// @ts-expect-error: TS in JSDoc generates wrong types if `this` is typed regularly.
const self = this
const {emitParseErrors, ...settings} = {...self.data('settings'), ...options}
Object.assign(this, {Parser: parser})
self.parser = parser
/** @type {import('unified').ParserFunction<Root>} */
/**
* @type {Parser}
*/
function parser(doc, file) {
const fn = settings.fragment ? 'parseFragment' : 'parse'
const onParseError = settings.emitParseErrors ? onerror : null
const parse5 = new Parser5({
sourceCodeLocationInfo: true,
onParseError,
scriptingEnabled: false
})
return fromHtml(doc, {
...settings,
onerror: emitParseErrors
? function (message) {
if (file.path) {
message.name = file.path + ':' + message.name
message.file = file.path
}
// @ts-expect-error: `parse5` returns document or fragment, which are always
// mapped to roots.
return fromParse5(parse5[fn](doc), {
space: settings.space,
file,
verbose: settings.verbose
file.messages.push(message)
}
: undefined
})
/**
* @param {{code: string, startLine: number, startCol: number, startOffset: number, endLine: number, endCol: number, endOffset: number}} error
*/
function onerror(error) {
const code = error.code
const name = camelcase(code)
const setting = settings[name]
const config = setting === undefined || setting === null ? true : setting
const level = typeof config === 'number' ? config : config ? 1 : 0
const start = {
line: error.startLine,
column: error.startCol,
offset: error.startOffset
}
const end = {
line: error.endLine,
column: error.endCol,
offset: error.endOffset
}
if (level) {
/* c8 ignore next */
const info = errors[name] || {reason: '', description: '', url: ''}
const message = file.message(format(info.reason), {start, end})
message.source = 'parse-error'
message.ruleId = code
message.fatal = fatalities[level]
message.note = format(info.description)
message.url = 'url' in info && info.url === false ? null : base + code
}
/**
* @param {string} value
* @returns {string}
*/
function format(value) {
return value
.replace(/%c(?:-(\d+))?/g, (_, /** @type {string} */ $1) => {
const offset = $1 ? -Number.parseInt($1, 10) : 0
const char = doc.charAt(error.startOffset + offset)
return char === '`' ? '` ` `' : char
})
.replace(
/%x/g,
() =>
'0x' +
doc.charCodeAt(error.startOffset).toString(16).toUpperCase()
)
}
}
}
}
/**
* @param {string} value
* @returns {ErrorCode}
*/
function camelcase(value) {
// @ts-expect-error: this returns a valid error code.
return value.replace(/-[a-z]/g, ($0) => $0.charAt(1).toUpperCase())
}
{
"name": "rehype-parse",
"version": "8.0.5",
"version": "9.0.0",
"description": "rehype plugin to parse HTML",
"license": "MIT",
"keywords": [
"unified",
"abstract",
"ast",
"html",
"parse",
"plugin",
"rehype",
"rehype-plugin",
"plugin",
"html",
"abstract",
"syntax",
"tree",
"ast",
"parse"
"unified"
],

@@ -31,4 +31,3 @@ "homepage": "https://github.com/rehypejs/rehype",

"type": "module",
"main": "index.js",
"types": "index.d.ts",
"exports": "./index.js",
"files": [

@@ -40,10 +39,7 @@ "lib/",

"dependencies": {
"@types/hast": "^2.0.0",
"hast-util-from-parse5": "^7.0.0",
"parse5": "^6.0.0",
"unified": "^10.0.0"
"@types/hast": "^3.0.0",
"hast-util-from-html": "^2.0.0",
"unified": "^11.0.0"
},
"scripts": {
"build": "tsc --build --clean && tsc --build && type-coverage"
},
"scripts": {},
"xo": false,

@@ -50,0 +46,0 @@ "typeCoverage": {

@@ -11,3 +11,3 @@ # rehype-parse

**[rehype][]** plugin to add support for parsing HTML input.
**[rehype][]** plugin to add support for parsing from HTML.

@@ -22,2 +22,5 @@ ## Contents

* [`unified().use(rehypeParse[, options])`](#unifieduserehypeparse-options)
* [`ErrorCode`](#errorcode)
* [`ErrorSeverity`](#errorseverity)
* [`Options`](#options)
* [Examples](#examples)

@@ -48,12 +51,13 @@ * [Example: fragment versus document](#example-fragment-versus-document)

This plugin adds support to unified for parsing HTML.
You can alternatively use [`rehype`][rehype-core] instead, which combines
unified, this plugin, and [`rehype-stringify`][rehype-stringify].
If you also need to serialize HTML, you can alternatively use
[`rehype`][rehype-core], which combines unified, this plugin, and
[`rehype-stringify`][rehype-stringify].
When you’re in a browser, trust your content, don’t need positional info, and
When you are in a browser, trust your content, don’t need positional info, and
value a smaller bundle size, you can use [`rehype-dom-parse`][rehype-dom-parse]
instead.
This plugin is built on [`parse5`][parse5] and
[`hast-util-from-parse5`][hast-util-from-parse5], which deal with HTML-compliant
tokenizing, parsing, and creating nodes.
If you don’t use plugins and want to access the syntax tree, you can directly
use [`hast-util-from-html`][hast-util-from-html], which is used inside this
plugin.
rehype focusses on making it easier to transform content by abstracting such

@@ -65,3 +69,3 @@ internals away.

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

@@ -75,3 +79,3 @@ ```sh

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

@@ -83,3 +87,3 @@

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

@@ -93,18 +97,14 @@ ```

```js
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeRemark from 'rehype-remark'
import remarkStringify from 'remark-stringify'
import {unified} from 'unified'
main()
const file = await unified()
.use(rehypeParse)
.use(rehypeRemark)
.use(remarkStringify)
.process('<h1>Hello, world!</h1>')
async function main() {
const file = await unified()
.use(rehypeParse)
.use(rehypeRemark)
.use(remarkStringify)
.process('<h1>Hello, world!</h1>')
console.log(String(file))
}
console.log(String(file))
```

@@ -121,114 +121,133 @@

This package exports no identifiers.
The default export is `rehypeParse`.
The default export is [`rehypeParse`][api-rehype-parse].
### `unified().use(rehypeParse[, options])`
Add support for parsing HTML input.
Plugin to add support for parsing from HTML.
##### `options`
###### Parameters
Configuration (optional).
* `options` ([`Options`][api-options], optional)
— configuration
###### `options.fragment`
###### Returns
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.
Nothing (`undefined`).
###### `options.space`
### `ErrorCode`
Which space the document is in (`'svg'` or `'html'`, default: `'html'`).
Known names of [parse errors][parse-errors] (TypeScript type).
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.
For a bit more info on each error, see
[`hast-util-from-html`][hast-util-from-html-errors].
> 👉 **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.
###### Type
> 👉 **Note**: make sure to set `fragment: true` if `space: 'svg'`.
```ts
type ErrorCode =
| 'abandonedHeadElementChild'
| 'abruptClosingOfEmptyComment'
| 'abruptDoctypePublicIdentifier'
| 'abruptDoctypeSystemIdentifier'
| 'absenceOfDigitsInNumericCharacterReference'
| 'cdataInHtmlContent'
| 'characterReferenceOutsideUnicodeRange'
| 'closingOfElementWithOpenChildElements'
| 'controlCharacterInInputStream'
| 'controlCharacterReference'
| 'disallowedContentInNoscriptInHead'
| 'duplicateAttribute'
| 'endTagWithAttributes'
| 'endTagWithTrailingSolidus'
| 'endTagWithoutMatchingOpenElement'
| 'eofBeforeTagName'
| 'eofInCdata'
| 'eofInComment'
| 'eofInDoctype'
| 'eofInElementThatCanContainOnlyText'
| 'eofInScriptHtmlCommentLikeText'
| 'eofInTag'
| 'incorrectlyClosedComment'
| 'incorrectlyOpenedComment'
| 'invalidCharacterSequenceAfterDoctypeName'
| 'invalidFirstCharacterOfTagName'
| 'misplacedDoctype'
| 'misplacedStartTagForHeadElement'
| 'missingAttributeValue'
| 'missingDoctype'
| 'missingDoctypeName'
| 'missingDoctypePublicIdentifier'
| 'missingDoctypeSystemIdentifier'
| 'missingEndTagName'
| 'missingQuoteBeforeDoctypePublicIdentifier'
| 'missingQuoteBeforeDoctypeSystemIdentifier'
| 'missingSemicolonAfterCharacterReference'
| 'missingWhitespaceAfterDoctypePublicKeyword'
| 'missingWhitespaceAfterDoctypeSystemKeyword'
| 'missingWhitespaceBeforeDoctypeName'
| 'missingWhitespaceBetweenAttributes'
| 'missingWhitespaceBetweenDoctypePublicAndSystemIdentifiers'
| 'nestedComment'
| 'nestedNoscriptInHead'
| 'nonConformingDoctype'
| 'nonVoidHtmlElementStartTagWithTrailingSolidus'
| 'noncharacterCharacterReference'
| 'noncharacterInInputStream'
| 'nullCharacterReference'
| 'openElementsLeftAfterEof'
| 'surrogateCharacterReference'
| 'surrogateInInputStream'
| 'unexpectedCharacterAfterDoctypeSystemIdentifier'
| 'unexpectedCharacterInAttributeName'
| 'unexpectedCharacterInUnquotedAttributeValue'
| 'unexpectedEqualsSignBeforeAttributeName'
| 'unexpectedNullCharacter'
| 'unexpectedQuestionMarkInsteadOfTagName'
| 'unexpectedSolidusInTag'
| 'unknownNamedCharacterReference'
```
###### `options.emitParseErrors`
### `ErrorSeverity`
Emit [HTML parse errors][parse-errors] as warning messages
(`boolean`, default: `false`).
Error severity (TypeScript type).
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.
* `0` or `false`
— turn the parse error off
* `1` or `true`
— turn the parse error into a warning
* `2`
— turn the parse error into an actual error: processing stops
The list of parse errors:
###### Type
<!-- parse-error start -->
```ts
type ErrorSeverity = boolean | 0 | 1 | 2
```
* `abandonedHeadElementChild` — unexpected metadata element after head ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/abandoned-head-element-child/index.html))
* [`abruptClosingOfEmptyComment`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-abrupt-closing-of-empty-comment) — unexpected abruptly closed empty comment ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/abrupt-closing-of-empty-comment/index.html))
* [`abruptDoctypePublicIdentifier`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-abrupt-doctype-public-identifier) — unexpected abruptly closed public identifier ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/abrupt-doctype-public-identifier/index.html))
* [`abruptDoctypeSystemIdentifier`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-abrupt-doctype-system-identifier) — unexpected abruptly closed system identifier ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/abrupt-doctype-system-identifier/index.html))
* [`absenceOfDigitsInNumericCharacterReference`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-absence-of-digits-in-numeric-character-reference) — unexpected non-digit at start of numeric character reference ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/absence-of-digits-in-numeric-character-reference/index.html))
* [`cdataInHtmlContent`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-cdata-in-html-content) — unexpected CDATA section in HTML ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/cdata-in-html-content/index.html))
* [`characterReferenceOutsideUnicodeRange`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-character-reference-outside-unicode-range) — unexpected too big numeric character reference ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/character-reference-outside-unicode-range/index.html))
* `closingOfElementWithOpenChildElements` — unexpected closing tag with open child elements ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/closing-of-element-with-open-child-elements/index.html))
* [`controlCharacterInInputStream`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-control-character-in-input-stream) — unexpected control character ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/control-character-in-input-stream/index.html))
* [`controlCharacterReference`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-control-character-reference) — unexpected control character reference ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/control-character-reference/index.html))
* `disallowedContentInNoscriptInHead` — disallowed content inside `<noscript>` in `<head>` ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/disallowed-content-in-noscript-in-head/index.html))
* [`duplicateAttribute`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-duplicate-attribute) — unexpected duplicate attribute ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/duplicate-attribute/index.html))
* [`endTagWithAttributes`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-end-tag-with-attributes) — unexpected attribute on closing tag ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/end-tag-with-attributes/index.html))
* [`endTagWithTrailingSolidus`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-end-tag-with-trailing-solidus) — unexpected slash at end of closing tag ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/end-tag-with-trailing-solidus/index.html))
* `endTagWithoutMatchingOpenElement` — unexpected unopened end tag ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/end-tag-without-matching-open-element/index.html))
* [`eofBeforeTagName`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-before-tag-name) — unexpected end of file ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/eof-before-tag-name/index.html))
* [`eofInCdata`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-in-cdata) — unexpected end of file in CDATA ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/eof-in-cdata/index.html))
* [`eofInComment`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-in-comment) — unexpected end of file in comment ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/eof-in-comment/index.html))
* [`eofInDoctype`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-in-doctype) — unexpected end of file in doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/eof-in-doctype/index.html))
* `eofInElementThatCanContainOnlyText` — unexpected end of file in element that can only contain text ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/eof-in-element-that-can-contain-only-text/index.html))
* [`eofInScriptHtmlCommentLikeText`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-in-script-html-comment-like-text) — unexpected end of file in comment inside script ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/eof-in-script-html-comment-like-text/index.html))
* [`eofInTag`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-eof-in-tag) — unexpected end of file in tag ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/eof-in-tag/index.html))
* [`incorrectlyClosedComment`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-incorrectly-closed-comment) — incorrectly closed comment ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/incorrectly-closed-comment/index.html))
* [`incorrectlyOpenedComment`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-incorrectly-opened-comment) — incorrectly opened comment ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/incorrectly-opened-comment/index.html))
* [`invalidCharacterSequenceAfterDoctypeName`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-invalid-character-sequence-after-doctype-name) — invalid sequence after doctype name ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/invalid-character-sequence-after-doctype-name/index.html))
* [`invalidFirstCharacterOfTagName`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-invalid-first-character-of-tag-name) — invalid first character in tag name ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/invalid-first-character-of-tag-name/index.html))
* `misplacedDoctype` — misplaced doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/misplaced-doctype/index.html))
* `misplacedStartTagForHeadElement` — misplaced `<head>` start tag ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/misplaced-start-tag-for-head-element/index.html))
* [`missingAttributeValue`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-attribute-value) — missing attribute value ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-attribute-value/index.html))
* `missingDoctype` — missing doctype before other content ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-doctype/index.html))
* [`missingDoctypeName`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-doctype-name) — missing doctype name ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-doctype-name/index.html))
* [`missingDoctypePublicIdentifier`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-doctype-public-identifier) — missing public identifier in doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-doctype-public-identifier/index.html))
* [`missingDoctypeSystemIdentifier`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-doctype-system-identifier) — missing system identifier in doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-doctype-system-identifier/index.html))
* [`missingEndTagName`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-end-tag-name) — missing name in end tag ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-end-tag-name/index.html))
* [`missingQuoteBeforeDoctypePublicIdentifier`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-quote-before-doctype-public-identifier) — missing quote before public identifier in doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-quote-before-doctype-public-identifier/index.html))
* [`missingQuoteBeforeDoctypeSystemIdentifier`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-quote-before-doctype-system-identifier) — missing quote before system identifier in doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-quote-before-doctype-system-identifier/index.html))
* [`missingSemicolonAfterCharacterReference`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-semicolon-after-character-reference) — missing semicolon after character reference ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-semicolon-after-character-reference/index.html))
* [`missingWhitespaceAfterDoctypePublicKeyword`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-whitespace-after-doctype-public-keyword) — missing whitespace after public identifier in doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-whitespace-after-doctype-public-keyword/index.html))
* [`missingWhitespaceAfterDoctypeSystemKeyword`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-whitespace-after-doctype-system-keyword) — missing whitespace after system identifier in doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-whitespace-after-doctype-system-keyword/index.html))
* [`missingWhitespaceBeforeDoctypeName`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-whitespace-before-doctype-name) — missing whitespace before doctype name ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-whitespace-before-doctype-name/index.html))
* [`missingWhitespaceBetweenAttributes`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-whitespace-between-attributes) — missing whitespace between attributes ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-whitespace-between-attributes/index.html))
* [`missingWhitespaceBetweenDoctypePublicAndSystemIdentifiers`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-missing-whitespace-between-doctype-public-and-system-identifiers) — missing whitespace between public and system identifiers in doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/missing-whitespace-between-doctype-public-and-system-identifiers/index.html))
* [`nestedComment`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-nested-comment) — unexpected nested comment ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/nested-comment/index.html))
* `nestedNoscriptInHead` — unexpected nested `<noscript>` in `<head>` ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/nested-noscript-in-head/index.html))
* `nonConformingDoctype` — unexpected non-conforming doctype declaration ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/non-conforming-doctype/index.html))
* [`nonVoidHtmlElementStartTagWithTrailingSolidus`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-non-void-html-element-start-tag-with-trailing-solidus) — unexpected trailing slash on start tag of non-void element ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/non-void-html-element-start-tag-with-trailing-solidus/index.html))
* [`noncharacterCharacterReference`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-noncharacter-character-reference) — unexpected noncharacter code point referenced by character reference ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/noncharacter-character-reference/index.html))
* [`noncharacterInInputStream`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-noncharacter-in-input-stream) — unexpected noncharacter character ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/noncharacter-in-input-stream/index.html))
* [`nullCharacterReference`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-null-character-reference) — unexpected NULL character referenced by character reference ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/null-character-reference/index.html))
* `openElementsLeftAfterEof` — unexpected end of file ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/open-elements-left-after-eof/index.html))
* [`surrogateCharacterReference`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-surrogate-character-reference) — unexpected surrogate character referenced by character reference ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/surrogate-character-reference/index.html))
* [`surrogateInInputStream`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-surrogate-in-input-stream) — unexpected surrogate character
* [`unexpectedCharacterAfterDoctypeSystemIdentifier`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unexpected-character-after-doctype-system-identifier) — invalid character after system identifier in doctype ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/unexpected-character-after-doctype-system-identifier/index.html))
* [`unexpectedCharacterInAttributeName`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unexpected-character-in-attribute-name) — unexpected character in attribute name ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/unexpected-character-in-attribute-name/index.html))
* [`unexpectedCharacterInUnquotedAttributeValue`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unexpected-character-in-unquoted-attribute-value) — unexpected character in unquoted attribute value ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/unexpected-character-in-unquoted-attribute-value/index.html))
* [`unexpectedEqualsSignBeforeAttributeName`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unexpected-equals-sign-before-attribute-name) — unexpected equals sign before attribute name ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/unexpected-equals-sign-before-attribute-name/index.html))
* [`unexpectedNullCharacter`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unexpected-null-character) — unexpected NULL character ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/unexpected-null-character/index.html))
* [`unexpectedQuestionMarkInsteadOfTagName`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unexpected-question-mark-instead-of-tag-name) — unexpected question mark instead of tag name ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/unexpected-question-mark-instead-of-tag-name/index.html))
* [`unexpectedSolidusInTag`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unexpected-solidus-in-tag) — unexpected slash in tag ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/unexpected-solidus-in-tag/index.html))
* [`unknownNamedCharacterReference`](https://html.spec.whatwg.org/multipage/parsing.html#parse-error-unknown-named-character-reference) — unexpected unknown named character reference ([example](https://github.com/rehypejs/rehype/blob/main/test/parse-error/unknown-named-character-reference/index.html))
### `Options`
<!-- parse-error end -->
Configuration (TypeScript type).
###### `options.verbose`
> 👉 **Note**: this 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.
> Use [`xast-util-from-xml`][xast-util-from-xml] to parse XML.
Add extra positional info (`boolean`, default: `false`).
###### Fields
* `fragment` (`boolean`, default: `false`)
— whether to parse as a fragment; by default unopened `html`, `head`, and
`body` elements are opened
* `emitParseErrors` (`boolean`, default: `false`)
— whether to emit [parse errors][parse-errors] while parsing
* `space` (`'html'` or `'svg'`, default: `'html'`)
— which space the document is in
* `verbose` (`boolean`, default: `false`)
— add extra positional info about attributes, start tags, and end tags
* [`[key in ErrorCode]`][api-error-code]
([`ErrorSeverity`][api-error-severity], default: `1` if
`options.emitParseErrors`, otherwise `0`)
— configure specific [parse errors][parse-errors]
## Examples

@@ -242,29 +261,25 @@

```js
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
main()
const doc = '<title>Hi!</title><h1>Hello!</h1>'
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: true})
.use(rehypeStringify)
.process(doc)
)
)
console.log(
String(
await unified()
.use(rehypeParse, {fragment: false})
.use(rehypeStringify)
.process(doc)
)
console.log(
String(
await unified()
.use(rehypeParse, {fragment: false})
.use(rehypeStringify)
.process(doc)
)
}
)
```

@@ -291,7 +306,7 @@

```js
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
main(`<!doctype html>
const doc = `<!doctype html>
<html lang=en>

@@ -304,9 +319,7 @@ <head>

</body>
</html>`)
</html>`
async function main(doc) {
console.log(
String(await unified().use(rehypeParse).use(rehypeStringify).process(doc))
)
}
console.log(
String(await unified().use(rehypeParse).use(rehypeStringify).process(doc))
)
```

@@ -342,23 +355,18 @@

```js
import {reporter} from 'vfile-reporter'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
import {reporter} from 'vfile-reporter'
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>
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))
}
console.log(reporter(file))
```

@@ -369,4 +377,4 @@

```html
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
1:10-1:10 error Missing whitespace before doctype name missing-whitespace-before-doctype-name hast-util-from-html
2:23-2:23 warning Unexpected duplicate attribute duplicate-attribute hast-util-from-html

@@ -390,3 +398,3 @@ 2 messages (✖ 1 error, ⚠ 1 warning)

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

@@ -400,14 +408,19 @@ ## Syntax tree

This package is fully typed with [TypeScript][].
The extra types `Options`, `ErrorCode`, and `ErrorSeverity` are exported.
It exports the additional types [`ErrorCode`][api-error-code],
[`ErrorSeverity`][api-error-severity], and
[`Options`][api-options].
## Compatibility
Projects maintained by the unified collective are compatible with all maintained
Projects maintained by the unified collective are compatible with 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.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, `rehype-parse@^9`,
compatible with Node.js 16.
## Security
As **rehype** works on HTML, and improper use of HTML can open you up to a
As **rehype** works on HTML and improper use of HTML can open you up to a
[cross-site scripting (XSS)][xss] attack, use of rehype can also be unsafe.

@@ -435,4 +448,2 @@ Use [`rehype-sanitize`][rehype-sanitize] to make the tree safe.

<!--lint ignore no-html-->
<table>

@@ -523,5 +534,5 @@ <tr valign="middle">

[size-badge]: https://img.shields.io/bundlephobia/minzip/rehype-parse.svg
[size-badge]: https://img.shields.io/bundlejs/size/rehype-parse
[size]: https://bundlephobia.com/result?p=rehype-parse
[size]: https://bundlejs.com/?q=rehype-parse

@@ -568,16 +579,26 @@ [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg

[rehype-stringify]: ../rehype-stringify/
[hast-util-from-html]: https://github.com/syntax-tree/hast-util-from-html
[rehype-core]: ../rehype/
[hast-util-from-html-errors]: https://github.com/syntax-tree/hast-util-from-html#optionskey-in-errorcode
[xast-util-from-xml]: https://github.com/syntax-tree/xast-util-from-xml
[rehype-dom-parse]: https://github.com/rehypejs/rehype-dom/tree/main/packages/rehype-dom-parse
[rehype-format]: https://github.com/rehypejs/rehype-format
[rehype-sanitize]: https://github.com/rehypejs/rehype-sanitize
[hast-util-from-parse5]: https://github.com/syntax-tree/hast-util-from-parse5
[parse-errors]: https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
[rehype-dom-parse]: https://github.com/rehypejs/rehype-dom/tree/main/packages/rehype-dom-parse
[rehype-core]: ../rehype/
[rehype-format]: https://github.com/rehypejs/rehype-format
[rehype-stringify]: ../rehype-stringify/
[parse5]: https://github.com/inikulin/parse5
[api-error-code]: #errorcode
[api-error-severity]: #errorseverity
[api-options]: #options
[api-rehype-parse]: #unifieduserehypeparse-options
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc