hast-util-from-parse5
Advanced tools
Comparing version 7.1.2 to 8.0.0
@@ -1,3 +0,49 @@ | ||
export { fromParse5 } from "./lib/index.js"; | ||
export type Options = import('./lib/index.js').Options; | ||
export type Space = import('./lib/index.js').Space; | ||
import type {Position} from 'unist' | ||
export type {Options, Space} from './lib/index.js' | ||
export {fromParse5} from './lib/index.js' | ||
// Register data on hast. | ||
declare module 'hast' { | ||
interface ElementData { | ||
position: { | ||
/** | ||
* Positional info of the start tag of an element. | ||
* | ||
* Field added by `hast-util-from-parse5` (a utility used inside | ||
* `rehype-parse` responsible for parsing HTML), when passing | ||
* `verbose: true`. | ||
*/ | ||
opening?: Position | undefined | ||
/** | ||
* Positional info of the end tag of an element. | ||
* | ||
* Field added by `hast-util-from-parse5` (a utility used inside | ||
* `rehype-parse` responsible for parsing HTML), when passing | ||
* `verbose: true`. | ||
*/ | ||
closing?: Position | undefined | ||
/** | ||
* Positional info of the properties of an element. | ||
* | ||
* Field added by `hast-util-from-parse5` (a utility used inside | ||
* `rehype-parse` responsible for parsing HTML), when passing | ||
* `verbose: true`. | ||
*/ | ||
properties?: Record<string, Position | undefined> | undefined | ||
} | ||
} | ||
interface RootData { | ||
/** | ||
* Whether the document was using quirksmode. | ||
* | ||
* Field added by `hast-util-from-parse5` (a utility used inside | ||
* `rehype-parse` responsible for parsing HTML). | ||
*/ | ||
quirksMode?: boolean | undefined | ||
} | ||
} |
@@ -1,6 +0,2 @@ | ||
/** | ||
* @typedef {import('./lib/index.js').Options} Options | ||
* @typedef {import('./lib/index.js').Space} Space | ||
*/ | ||
// Note: extra types exposed from `index.d.ts`. | ||
export {fromParse5} from './lib/index.js' |
@@ -6,19 +6,20 @@ /** | ||
* `parse5` tree to transform. | ||
* @param {Options | VFile | null | undefined} [options] | ||
* Configuration. | ||
* @returns {Node} | ||
* @param {Options | null | undefined} [options] | ||
* Configuration (optional). | ||
* @returns {Nodes} | ||
* hast tree. | ||
*/ | ||
export function fromParse5(tree: P5Node, options?: Options | VFile | null | undefined): Node; | ||
export type VFile = import('vfile').VFile; | ||
export type Schema = import('property-information').Schema; | ||
export type Position = import('unist').Position; | ||
export type Point = import('unist').Point; | ||
export function fromParse5(tree: P5Node, options?: Options | null | undefined): Nodes; | ||
export type Element = import('hast').Element; | ||
export type ElementData = import('hast').ElementData; | ||
export type Nodes = import('hast').Nodes; | ||
export type Root = import('hast').Root; | ||
export type Content = import('hast').Content; | ||
export type RootContent = import('hast').RootContent; | ||
export type DefaultTreeAdapterMap = import('parse5').DefaultTreeAdapterMap; | ||
export type P5ElementLocation = import('parse5').Token.ElementLocation; | ||
export type P5Location = import('parse5').Token.Location; | ||
export type Node = Content | Root; | ||
export type Schema = import('property-information').Schema; | ||
export type Point = import('unist').Point; | ||
export type Position = import('unist').Position; | ||
export type VFile = import('vfile').VFile; | ||
export type P5Document = DefaultTreeAdapterMap['document']; | ||
@@ -33,6 +34,2 @@ export type P5DocumentFragment = DefaultTreeAdapterMap['documentFragment']; | ||
/** | ||
* Namespace. | ||
*/ | ||
export type Space = 'html' | 'svg'; | ||
/** | ||
* Configuration. | ||
@@ -42,3 +39,3 @@ */ | ||
/** | ||
* Which space the document is in. | ||
* Which space the document is in (default: `'html'`). | ||
* | ||
@@ -51,3 +48,3 @@ * When an `<svg>` element is found in the HTML space, this package already | ||
/** | ||
* File used to add positional info to nodes. | ||
* File used to add positional info to nodes (optional). | ||
* | ||
@@ -59,9 +56,13 @@ * If given, the file should represent the original HTML source. | ||
* Whether to add extra positional info about starting tags, closing tags, | ||
* and attributes to elements. | ||
* and attributes to elements (default: `false`). | ||
* | ||
* > 👉 **Note**: only used when `file` is given. | ||
*/ | ||
verbose?: boolean | undefined; | ||
verbose?: boolean; | ||
}; | ||
/** | ||
* Namespace. | ||
*/ | ||
export type Space = 'html' | 'svg'; | ||
/** | ||
* Info passed around about the current state. | ||
@@ -71,17 +72,17 @@ */ | ||
/** | ||
* Current schema. | ||
* Corresponding file. | ||
*/ | ||
schema: Schema; | ||
file: VFile | undefined; | ||
/** | ||
* Corresponding file. | ||
* Whether location info was found. | ||
*/ | ||
file: VFile | undefined; | ||
location: boolean; | ||
/** | ||
* Add extra positional info. | ||
* Current schema. | ||
*/ | ||
verbose: boolean | undefined; | ||
schema: Schema; | ||
/** | ||
* Whether location info was found. | ||
* Add extra positional info. | ||
*/ | ||
location: boolean; | ||
verbose: boolean | undefined; | ||
}; |
136
lib/index.js
/** | ||
* @typedef {import('vfile').VFile} VFile | ||
* @typedef {import('property-information').Schema} Schema | ||
* @typedef {import('unist').Position} Position | ||
* @typedef {import('unist').Point} Point | ||
* @typedef {import('hast').Element} Element | ||
* @typedef {import('hast').ElementData} ElementData | ||
* @typedef {import('hast').Nodes} Nodes | ||
* @typedef {import('hast').Root} Root | ||
* @typedef {import('hast').Content} Content | ||
* @typedef {import('hast').RootContent} RootContent | ||
* | ||
* @typedef {import('parse5').DefaultTreeAdapterMap} DefaultTreeAdapterMap | ||
* @typedef {import('parse5').Token.ElementLocation} P5ElementLocation | ||
* @typedef {import('parse5').Token.Location} P5Location | ||
* | ||
* @typedef {import('property-information').Schema} Schema | ||
* | ||
* @typedef {import('unist').Point} Point | ||
* @typedef {import('unist').Position} Position | ||
* | ||
* @typedef {import('vfile').VFile} VFile | ||
*/ | ||
/** | ||
* @typedef {Content | Root} Node | ||
* @typedef {DefaultTreeAdapterMap['document']} P5Document | ||
@@ -24,10 +29,9 @@ * @typedef {DefaultTreeAdapterMap['documentFragment']} P5DocumentFragment | ||
* @typedef {DefaultTreeAdapterMap['template']} P5Template | ||
* | ||
* @typedef {'html' | 'svg'} Space | ||
* Namespace. | ||
* | ||
*/ | ||
/** | ||
* @typedef Options | ||
* Configuration. | ||
* @property {Space | null | undefined} [space='html'] | ||
* Which space the document is in. | ||
* Which space the document is in (default: `'html'`). | ||
* | ||
@@ -38,3 +42,3 @@ * When an `<svg>` element is found in the HTML space, this package already | ||
* @property {VFile | null | undefined} [file] | ||
* File used to add positional info to nodes. | ||
* File used to add positional info to nodes (optional). | ||
* | ||
@@ -44,20 +48,24 @@ * If given, the file should represent the original HTML source. | ||
* Whether to add extra positional info about starting tags, closing tags, | ||
* and attributes to elements. | ||
* and attributes to elements (default: `false`). | ||
* | ||
* > 👉 **Note**: only used when `file` is given. | ||
* | ||
* @typedef {'html' | 'svg'} Space | ||
* Namespace. | ||
* | ||
* @typedef State | ||
* Info passed around about the current state. | ||
* @property {VFile | undefined} file | ||
* Corresponding file. | ||
* @property {boolean} location | ||
* Whether location info was found. | ||
* @property {Schema} schema | ||
* Current schema. | ||
* @property {VFile | undefined} file | ||
* Corresponding file. | ||
* @property {boolean | undefined} verbose | ||
* Add extra positional info. | ||
* @property {boolean} location | ||
* Whether location info was found. | ||
*/ | ||
import {ok as assert} from 'devlop' | ||
import {h, s} from 'hastscript' | ||
import {html, svg, find} from 'property-information' | ||
import {find, html, svg} from 'property-information' | ||
import {location} from 'vfile-location' | ||
@@ -76,28 +84,16 @@ import {webNamespaces} from 'web-namespaces' | ||
* `parse5` tree to transform. | ||
* @param {Options | VFile | null | undefined} [options] | ||
* Configuration. | ||
* @returns {Node} | ||
* @param {Options | null | undefined} [options] | ||
* Configuration (optional). | ||
* @returns {Nodes} | ||
* hast tree. | ||
*/ | ||
export function fromParse5(tree, options) { | ||
const options_ = options || {} | ||
/** @type {Options} */ | ||
let settings | ||
/** @type {VFile | undefined} */ | ||
let file | ||
const settings = options || {} | ||
if (isFile(options_)) { | ||
file = options_ | ||
settings = {} | ||
} else { | ||
file = options_.file || undefined | ||
settings = options_ | ||
} | ||
return one( | ||
{ | ||
file: settings.file || undefined, | ||
location: false, | ||
schema: settings.space === 'svg' ? svg : html, | ||
file, | ||
verbose: settings.verbose, | ||
location: false | ||
verbose: settings.verbose | ||
}, | ||
@@ -115,7 +111,7 @@ tree | ||
* p5 node. | ||
* @returns {Node} | ||
* @returns {Nodes} | ||
* hast node. | ||
*/ | ||
function one(state, node) { | ||
/** @type {Node} */ | ||
/** @type {Nodes} */ | ||
let result | ||
@@ -150,3 +146,5 @@ | ||
const end = loc.toPoint(doc.length) | ||
// @ts-expect-error: always defined as we give valid input. | ||
// Always defined as we give valid input. | ||
assert(start, 'expected `start`') | ||
assert(end, 'expected `end`') | ||
result.position = {start, end} | ||
@@ -160,3 +158,2 @@ } | ||
const reference = /** @type {P5DocumentType} */ (node) | ||
// @ts-expect-error Types are out of date. | ||
result = {type: 'doctype'} | ||
@@ -190,3 +187,3 @@ patch(state, reference, result) | ||
* Nodes. | ||
* @returns {Array<Content>} | ||
* @returns {Array<RootContent>} | ||
* hast nodes. | ||
@@ -196,11 +193,12 @@ */ | ||
let index = -1 | ||
/** @type {Array<Content>} */ | ||
const result = [] | ||
/** @type {Array<RootContent>} */ | ||
const results = [] | ||
while (++index < nodes.length) { | ||
// @ts-expect-error Assume no roots in `nodes`. | ||
result[index] = one(state, nodes[index]) | ||
// Assume no roots in `nodes`. | ||
const result = /** @type {RootContent} */ (one(state, nodes[index])) | ||
results.push(result) | ||
} | ||
return result | ||
return results | ||
} | ||
@@ -249,5 +247,4 @@ | ||
/** @type {Root} */ | ||
// @ts-expect-error Types are wrong. | ||
const content = one(state, reference.content) | ||
// Root in, root out. | ||
const content = /** @type {Root} */ (one(state, reference.content)) | ||
@@ -273,5 +270,5 @@ if (startTag && endTag && state.file) { | ||
* p5 node. | ||
* @param {Node} to | ||
* @param {Nodes} to | ||
* hast node. | ||
* @returns {void} | ||
* @returns {undefined} | ||
* Nothing. | ||
@@ -295,3 +292,3 @@ */ | ||
* Info passed around about the current state. | ||
* @param {Node} node | ||
* @param {Nodes} node | ||
* hast node. | ||
@@ -337,10 +334,11 @@ * @param {P5ElementLocation} location | ||
node.data = { | ||
position: { | ||
// @ts-expect-error: assume not `undefined`. | ||
opening: position(location.startTag), | ||
closing: location.endTag ? position(location.endTag) : null, | ||
properties: props | ||
} | ||
} | ||
assert(location.startTag, 'a start tag should exist') | ||
const opening = position(location.startTag) | ||
const closing = location.endTag ? position(location.endTag) : undefined | ||
/** @type {ElementData['position']} */ | ||
const data = {opening} | ||
if (closing) data.closing = closing | ||
data.properties = props | ||
node.data = {position: data} | ||
} | ||
@@ -371,3 +369,5 @@ } | ||
}) | ||
// @ts-expect-error `undefined` is fine. | ||
// @ts-expect-error: we do use `undefined` for points if one or the other | ||
// exists. | ||
return start || end ? {start, end} : undefined | ||
@@ -387,13 +387,1 @@ } | ||
} | ||
/** | ||
* Check if something is a file. | ||
* | ||
* @param {VFile | Options} value | ||
* File or options. | ||
* @returns {value is VFile} | ||
* Whether `value` is a file. | ||
*/ | ||
function isFile(value) { | ||
return 'messages' in value | ||
} |
{ | ||
"name": "hast-util-from-parse5", | ||
"version": "7.1.2", | ||
"version": "8.0.0", | ||
"description": "hast utility to transform from Parse5’s AST", | ||
@@ -28,4 +28,3 @@ "license": "MIT", | ||
"type": "module", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"exports": "./index.js", | ||
"files": [ | ||
@@ -37,23 +36,24 @@ "lib/", | ||
"dependencies": { | ||
"@types/hast": "^2.0.0", | ||
"@types/unist": "^2.0.0", | ||
"hastscript": "^7.0.0", | ||
"@types/hast": "^3.0.0", | ||
"@types/unist": "^3.0.0", | ||
"devlop": "^1.0.0", | ||
"hastscript": "^8.0.0", | ||
"property-information": "^6.0.0", | ||
"vfile": "^5.0.0", | ||
"vfile-location": "^4.0.0", | ||
"vfile": "^6.0.0", | ||
"vfile-location": "^5.0.0", | ||
"web-namespaces": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
"@types/node": "^20.0.0", | ||
"c8": "^8.0.0", | ||
"is-hidden": "^2.0.0", | ||
"parse5": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"prettier": "^3.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"to-vfile": "^7.0.0", | ||
"to-vfile": "^8.0.0", | ||
"type-coverage": "^2.0.0", | ||
"typescript": "^4.0.0", | ||
"unist-util-visit": "^4.0.0", | ||
"xo": "^0.53.0" | ||
"typescript": "^5.0.0", | ||
"unist-util-visit": "^5.0.0", | ||
"xo": "^0.55.0" | ||
}, | ||
@@ -63,32 +63,18 @@ "scripts": { | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"format": "remark . -qfo && prettier . -w --log-level warn && xo --fix", | ||
"test-api": "node --conditions development test/index.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test-coverage": "c8 --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
}, | ||
"prettier": { | ||
"tabWidth": 2, | ||
"useTabs": false, | ||
"singleQuote": true, | ||
"bracketSpacing": false, | ||
"semi": false, | ||
"trailingComma": "none" | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "none", | ||
"useTabs": false | ||
}, | ||
"xo": { | ||
"prettier": true, | ||
"rules": { | ||
"max-depth": "off" | ||
}, | ||
"overrides": [ | ||
{ | ||
"files": "test/**/*.js", | ||
"rules": { | ||
"no-await-in-loop": 0 | ||
} | ||
} | ||
] | ||
}, | ||
"remarkConfig": { | ||
"plugins": [ | ||
"preset-wooorm" | ||
"remark-preset-wooorm" | ||
] | ||
@@ -99,4 +85,26 @@ }, | ||
"detail": true, | ||
"ignoreCatch": true, | ||
"strict": true | ||
}, | ||
"xo": { | ||
"overrides": [ | ||
{ | ||
"files": "**/*.ts", | ||
"rules": { | ||
"@typescript-eslint/consistent-type-definitions": "off" | ||
} | ||
}, | ||
{ | ||
"files": "test/**/*.js", | ||
"rules": { | ||
"no-await-in-loop": "off" | ||
} | ||
} | ||
], | ||
"prettier": true, | ||
"rules": { | ||
"max-depth": "off", | ||
"unicorn/prefer-at": "off" | ||
} | ||
} | ||
} |
@@ -20,3 +20,3 @@ # hast-util-from-parse5 | ||
* [API](#api) | ||
* [`fromParse5(tree[, file|options])`](#fromparse5tree-fileoptions) | ||
* [`fromParse5(tree[, options])`](#fromparse5tree-options) | ||
* [`Options`](#options) | ||
@@ -50,3 +50,3 @@ * [`Space`](#space-1) | ||
This package is [ESM only][esm]. | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
In Node.js (version 16+), install with [npm][]: | ||
@@ -60,3 +60,3 @@ ```sh | ||
```js | ||
import {fromParse5} from "https://esm.sh/hast-util-from-parse5@7" | ||
import {fromParse5} from "https://esm.sh/hast-util-from-parse5@8" | ||
``` | ||
@@ -68,3 +68,3 @@ | ||
<script type="module"> | ||
import {fromParse5} from "https://esm.sh/hast-util-from-parse5@7?bundle" | ||
import {fromParse5} from "https://esm.sh/hast-util-from-parse5@8?bundle" | ||
</script> | ||
@@ -84,10 +84,10 @@ ``` | ||
```js | ||
import {fromParse5} from 'hast-util-from-parse5' | ||
import {parse} from 'parse5' | ||
import {read} from 'to-vfile' | ||
import {inspect} from 'unist-util-inspect' | ||
import {fromParse5} from 'hast-util-from-parse5' | ||
const file = await read('example.html') | ||
const p5ast = parse(String(file), {sourceCodeLocationInfo: true}) | ||
const hast = fromParse5(p5ast, file) | ||
const hast = fromParse5(p5ast, {file}) | ||
@@ -102,5 +102,3 @@ console.log(inspect(hast)) | ||
│ data: {"quirksMode":false} | ||
├─0 doctype<html> (1:1-1:16, 0-15) | ||
│ public: null | ||
│ system: null | ||
├─0 doctype (1:1-1:16, 0-15) | ||
└─1 element<html>[2] | ||
@@ -124,6 +122,6 @@ │ properties: {} | ||
This package exports the identifier [`fromParse5`][fromparse5]. | ||
This package exports the identifier [`fromParse5`][api-from-parse5]. | ||
There is no default export. | ||
### `fromParse5(tree[, file|options])` | ||
### `fromParse5(tree[, options])` | ||
@@ -136,5 +134,3 @@ Transform a `parse5` AST to hast. | ||
— `parse5` tree to transform | ||
* `file` ([`VFile`][vfile], optional) | ||
— corresponding file (treated as `{file: file}`) | ||
* `options` ([`Options`][options], optional) | ||
* `options` ([`Options`][api-options], optional) | ||
— configuration | ||
@@ -152,5 +148,11 @@ | ||
###### `file` | ||
File used to add positional info to nodes ([`VFile`][vfile], optional). | ||
If given, the file should represent the original HTML source. | ||
###### `space` | ||
Which space the document is in ([`Space`][space], default: `'html'`). | ||
Which space the document is in ([`Space`][api-space], default: `'html'`). | ||
@@ -161,8 +163,2 @@ When an `<svg>` element is found in the HTML space, this package already | ||
###### `file` | ||
File used to add positional info to nodes ([`VFile`][vfile], optional). | ||
If given, the file should represent the original HTML source. | ||
###### `verbose` | ||
@@ -232,11 +228,15 @@ | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional types [`Options`][options] and [`Space`][space]. | ||
It exports the additional types [`Options`][api-options] and | ||
[`Space`][api-space]. | ||
## 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+, 16.0+, and 18.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, `hast-util-from-parse5@^8`, | ||
compatible with Node.js 16. | ||
## Security | ||
@@ -290,5 +290,5 @@ | ||
[size-badge]: https://img.shields.io/bundlephobia/minzip/hast-util-from-parse5.svg | ||
[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size&query=$.size.compressedSize&url=https://deno.bundlejs.com/?q=hast-util-from-parse5 | ||
[size]: https://bundlephobia.com/result?p=hast-util-from-parse5 | ||
[size]: https://bundlejs.com/?q=hast-util-from-parse5 | ||
@@ -341,6 +341,6 @@ [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg | ||
[fromparse5]: #fromparse5tree-fileoptions | ||
[api-from-parse5]: #fromparse5tree-options | ||
[options]: #options | ||
[api-options]: #options | ||
[space]: #space-1 | ||
[api-space]: #space-1 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26271
457
8
+ Addeddevlop@^1.0.0
+ Added@types/hast@3.0.4(transitive)
+ Added@types/unist@3.0.3(transitive)
+ Addeddequal@2.0.3(transitive)
+ Addeddevlop@1.1.0(transitive)
+ Addedhast-util-parse-selector@4.0.0(transitive)
+ Addedhastscript@8.0.0(transitive)
+ Addedunist-util-stringify-position@4.0.0(transitive)
+ Addedvfile@6.0.3(transitive)
+ Addedvfile-location@5.0.3(transitive)
+ Addedvfile-message@4.0.2(transitive)
- Removed@types/hast@2.3.10(transitive)
- Removed@types/unist@2.0.11(transitive)
- Removedhast-util-parse-selector@3.1.1(transitive)
- Removedhastscript@7.2.0(transitive)
- Removedis-buffer@2.0.5(transitive)
- Removedunist-util-stringify-position@3.0.3(transitive)
- Removedvfile@5.3.7(transitive)
- Removedvfile-location@4.1.0(transitive)
- Removedvfile-message@3.1.4(transitive)
Updated@types/hast@^3.0.0
Updated@types/unist@^3.0.0
Updatedhastscript@^8.0.0
Updatedvfile@^6.0.0
Updatedvfile-location@^5.0.0