Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

rehype-parse

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rehype-parse - npm Package Compare versions

Comparing version 4.1.0 to 5.0.0

errors.json

94

index.js

@@ -1,24 +0,94 @@

'use strict';
'use strict'
var fromParse5 = require('hast-util-from-parse5');
var Parser5 = require('parse5/lib/parser');
var xtend = require('xtend');
var fromParse5 = require('hast-util-from-parse5')
var Parser5 = require('parse5/lib/parser')
var xtend = require('xtend')
var errors = require('./errors.json')
module.exports = parse;
var base = 'https://html.spec.whatwg.org/multipage/parsing.html#parse-error-'
var fatalities = {2: true, 1: false, 0: null}
module.exports = parse
function parse(options) {
var settings = xtend(options, this.data('settings'));
var position = typeof settings.position === 'boolean' ? settings.position : true;
var parse5 = new Parser5({locationInfo: position});
var settings = xtend(options, this.data('settings'))
var position = settings.position
this.Parser = parser;
position = typeof position === 'boolean' ? position : true
this.Parser = parser
function parser(doc, file) {
var fn = settings.fragment ? 'parseFragment' : 'parse';
var fn = settings.fragment ? 'parseFragment' : 'parse'
var onParseError = settings.emitParseErrors ? onerror : null
var parse5 = new Parser5({
sourceCodeLocationInfo: position,
onParseError: onParseError,
scriptingEnabled: false
})
return fromParse5(parse5[fn](String(file)), {
return fromParse5(parse5[fn](doc), {
space: settings.space,
file: file,
verbose: settings.verbose
});
})
function onerror(err) {
var code = err.code
var name = camelcase(code)
var setting = settings[name]
var config = setting === undefined || setting === null ? true : setting
var level = typeof config === 'number' ? config : config ? 1 : 0
var start = {
line: err.startLine,
column: err.startCol,
offset: err.startOffset
}
var end = {line: err.endLine, column: err.endCol, offset: err.endOffset}
var info
var message
if (level) {
info = errors[name] || /* istanbul ignore next */ {
reason: '',
description: ''
}
message = file.message(format(info.reason), {start: start, end: end})
message.source = 'parse-error'
message.ruleId = code
message.fatal = fatalities[level]
message.note = format(info.description)
message.url = info.url === false ? null : base + code
}
function format(value) {
return value.replace(/%c(?:-(\d+))?/g, char).replace(/%x/g, encodedChar)
}
function char($0, $1) {
var offset = $1 ? -parseInt($1, 10) : 0
var char = doc.charAt(err.startOffset + offset)
return char === '`' ? '` ` `' : char
}
function encodedChar() {
var char = doc
.charCodeAt(err.startOffset)
.toString(16)
.toUpperCase()
return '0x' + char
}
}
}
}
function camelcase(value) {
return value.replace(/-[a-z]/g, replacer)
}
function replacer($0) {
return $0.charAt(1).toUpperCase()
}

12

package.json
{
"name": "rehype-parse",
"version": "4.1.0",
"version": "5.0.0",
"description": "HTML parser for rehype",

@@ -21,11 +21,9 @@ "license": "MIT",

],
"engines": {
"node": ">=0.11.0"
},
"files": [
"index.js"
"index.js",
"errors.json"
],
"dependencies": {
"hast-util-from-parse5": "^2.0.1",
"parse5": "^4.0.0",
"hast-util-from-parse5": "^4.0.0",
"parse5": "^5.0.0",
"xtend": "^4.0.1"

@@ -32,0 +30,0 @@ },

@@ -17,13 +17,49 @@ # rehype-parse [![Build Status][build-badge]][build-status] [![Coverage Status][coverage-badge]][coverage-status] [![Chat][chat-badge]][chat]

This example shows how we parse HTML with this module and configure it to emit
parse errors except for duplicate attributes.
Then we transform HTML to markdown with [`rehype-remark`][rehype-remark] and
finally compile that markdown with [`remark-stringify`][remark-stringify].
Say we have the following file, `example.html`, with a few errors:
```html
<!doctypehtml>
<title class="a" class="b">Hello…</title>
<h1/>World!</h1>
```
And our script, `example.js`, looks as follows:
```js
var unified = require('unified');
var createStream = require('unified-stream');
var parse = require('rehype-parse');
var stringify = require('rehype-stringify');
var vfile = require('to-vfile')
var report = require('vfile-reporter')
var unified = require('unified')
var parse = require('rehype-parse')
var rehype2remark = require('rehype-remark')
var stringify = require('remark-stringify')
process.stdin
.pipe(createStream(unified().use(parse).use(stringify)))
.pipe(process.stdout);
unified()
.use(parse, {emitParseErrors: true, duplicateAttribute: false})
.use(rehype2remark)
.use(stringify)
.process(vfile.readSync('example.html'), function(err, file) {
console.error(report(err || file))
console.log(String(file))
})
```
Now, running `node example` yields:
```txt
example.html
1:10-1:10 warning Missing whitespace before doctype name missing-whitespace-before-doctype-name parse-error
3:1-3:6 warning Unexpected trailing slash on start tag of non-void element non-void-html-element-start-tag-with-trailing-solidus parse-error
⚠ 2 warnings
```
```markdown
# World!
```
## API

@@ -44,2 +80,98 @@

###### `options.space`
> ⚠️ rehype is not an XML parser. It support SVG as embedded in HTML, but not
> the features available in the rest of XML/SVG. Passing SVG files could strip
> useful information, but fragments of modern SVG should be fine.
Whether the document is in the `'html'` or `'svg'` space (`'svg'` or `'html'`,
default: `'html'`).
If an `svg` element is found in the HTML space, `toHTML` automatically switches
to the SVG space when entering the element, and switches back when leaving.
**Note**: make sure to set `fragment: true` if `space: 'svg'`.
###### `options.emitParseErrors`
> ⚠️ 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][] (`boolean`, default: `false`).
Setting this to true starts emitting [HTML parse errors][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.
The specific parse errors that are currently supported are detailed below:
<!-- parse-error start -->
* `abandonedHeadElementChild` β€” unexpected metadata element after head ([example](https://github.com/rehypejs/rehype/blob/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/test/parse-error/control-character-reference/index.html))
* `disallowedContentInNoscriptInHead` β€” disallowed content inside \`&lt;noscript>\` in \`&lt;head>\` ([example](https://github.com/rehypejs/rehype/blob/master/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/master/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/master/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/master/test/parse-error/end-tag-with-trailing-solidus/index.html))
* `endTagWithoutMatchingOpenElement` β€” unexpected unopened end tag ([example](https://github.com/rehypejs/rehype/blob/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/test/parse-error/invalid-first-character-of-tag-name/index.html))
* `misplacedDoctype` β€” misplaced doctype ([example](https://github.com/rehypejs/rehype/blob/master/test/parse-error/misplaced-doctype/index.html))
* `misplacedStartTagForHeadElement` β€” misplaced \`&lt;head>\` start tag ([example](https://github.com/rehypejs/rehype/blob/master/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/master/test/parse-error/missing-attribute-value/index.html))
* `missingDoctype` β€” missing doctype before other content ([example](https://github.com/rehypejs/rehype/blob/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/test/parse-error/nested-comment/index.html))
* `nestedNoscriptInHead` β€” unexpected nested \`&lt;noscript>\` in \`&lt;head>\` ([example](https://github.com/rehypejs/rehype/blob/master/test/parse-error/nested-noscript-in-head/index.html))
* `nonConformingDoctype` β€” unexpected non-conforming doctype declaration ([example](https://github.com/rehypejs/rehype/blob/master/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/master/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/master/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/master/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/master/test/parse-error/null-character-reference/index.html))
* `openElementsLeftAfterEof` β€” unexpected end of file ([example](https://github.com/rehypejs/rehype/blob/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/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/master/test/parse-error/unknown-named-character-reference/index.html))
<!-- parse-error end -->
###### `options.verbose`

@@ -99,4 +231,12 @@

[rehype-remark]: https://github.com/rehypejs/rehype-remark
[remark-stringify]: https://github.com/remarkjs/remark/tree/master/packages/remark-stringify
[unified]: https://github.com/unifiedjs/unified
[vfile]: https://github.com/vfile/vfile
[parse-errors]: https://html.spec.whatwg.org/multipage/parsing.html#parse-errors
[processor]: https://github.com/rehypejs/rehype/blob/master/packages/rehype

@@ -103,0 +243,0 @@

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc