Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated parse5 to v7 #140

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions packages/rehype-parse/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
* @typedef {FromParse5Options & ParseFields & ErrorFields} Options
*/

// @ts-expect-error: remove when typed
import Parser5 from 'parse5/lib/parser/index.js'
import {parse, parseFragment} from 'parse5'
import {fromParse5} from 'hast-util-from-parse5'
import {errors} from './errors.js'

Expand All @@ -48,21 +47,24 @@ export default function rehypeParse(options) {

/** @type {import('unified').ParserFunction<Root>} */
function parser(doc, file) {
const fn = settings.fragment ? 'parseFragment' : 'parse'
const onParseError = settings.emitParseErrors ? onerror : null
const parse5 = new Parser5({
const parserOptions = {
sourceCodeLocationInfo: true,
onParseError,
onParseError: settings.emitParseErrors ? onerror : null,
scriptingEnabled: false
})
}

// @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
})
return fromParse5(
settings.fragment
? parseFragment(doc, parserOptions)
: parse(doc, parserOptions),
{
space: settings.space,
file,
verbose: settings.verbose
}
)

/**
* @param {{code: string, startLine: number, startCol: number, startOffset: number, endLine: number, endCol: number, endOffset: number}} error
Expand Down
2 changes: 1 addition & 1 deletion packages/rehype-parse/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"dependencies": {
"@types/hast": "^2.0.0",
"hast-util-from-parse5": "^7.0.0",
"parse5": "^6.0.0",
"parse5": "^7.0.0",
"unified": "^10.0.0"
},
"scripts": {
Expand Down
3 changes: 1 addition & 2 deletions test/parse-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import path from 'node:path'
import test from 'tape'
import {toVFile, readSync} from 'to-vfile'
import {rehype} from 'rehype'
// @ts-expect-error: untyped.
import p5errors from 'parse5/lib/common/error-codes.js'
import {ErrorCodes as p5errors} from 'parse5'
import {errors as rerrors} from 'rehype-parse/lib/errors.js'

// Related to https://github.com/inikulin/parse5/issues/255
Expand Down