🎉 initiate project *astro_rewrite*
This commit is contained in:
parent
ffd4d5e86c
commit
2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions
3
node_modules/hast-util-from-parse5/index.d.ts
generated
vendored
Normal file
3
node_modules/hast-util-from-parse5/index.d.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export { fromParse5 } from "./lib/index.js";
|
||||
export type Options = import('./lib/index.js').Options;
|
||||
export type Space = import('./lib/index.js').Space;
|
||||
6
node_modules/hast-util-from-parse5/index.js
generated
vendored
Normal file
6
node_modules/hast-util-from-parse5/index.js
generated
vendored
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* @typedef {import('./lib/index.js').Options} Options
|
||||
* @typedef {import('./lib/index.js').Space} Space
|
||||
*/
|
||||
|
||||
export {fromParse5} from './lib/index.js'
|
||||
81
node_modules/hast-util-from-parse5/lib/index.d.ts
generated
vendored
Normal file
81
node_modules/hast-util-from-parse5/lib/index.d.ts
generated
vendored
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/**
|
||||
* Transform a `parse5` AST to hast.
|
||||
*
|
||||
* @param {P5Node} tree
|
||||
* `parse5` tree to transform.
|
||||
* @param {Options | VFile | null | undefined} [options]
|
||||
* Configuration.
|
||||
* @returns {Node}
|
||||
* 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 type Element = import('hast').Element;
|
||||
export type Root = import('hast').Root;
|
||||
export type Content = import('hast').Content;
|
||||
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 P5Document = DefaultTreeAdapterMap['document'];
|
||||
export type P5DocumentFragment = DefaultTreeAdapterMap['documentFragment'];
|
||||
export type P5DocumentType = DefaultTreeAdapterMap['documentType'];
|
||||
export type P5Comment = DefaultTreeAdapterMap['commentNode'];
|
||||
export type P5Text = DefaultTreeAdapterMap['textNode'];
|
||||
export type P5Element = DefaultTreeAdapterMap['element'];
|
||||
export type P5Node = DefaultTreeAdapterMap['node'];
|
||||
export type P5Template = DefaultTreeAdapterMap['template'];
|
||||
/**
|
||||
* Namespace.
|
||||
*/
|
||||
export type Space = 'html' | 'svg';
|
||||
/**
|
||||
* Configuration.
|
||||
*/
|
||||
export type Options = {
|
||||
/**
|
||||
* Which space the document is in.
|
||||
*
|
||||
* When an `<svg>` element is found in the HTML space, this package already
|
||||
* automatically switches to and from the SVG space when entering and exiting
|
||||
* it.
|
||||
*/
|
||||
space?: Space | null | undefined;
|
||||
/**
|
||||
* File used to add positional info to nodes.
|
||||
*
|
||||
* If given, the file should represent the original HTML source.
|
||||
*/
|
||||
file?: VFile | null | undefined;
|
||||
/**
|
||||
* Whether to add extra positional info about starting tags, closing tags,
|
||||
* and attributes to elements.
|
||||
*
|
||||
* > 👉 **Note**: only used when `file` is given.
|
||||
*/
|
||||
verbose?: boolean | undefined;
|
||||
};
|
||||
/**
|
||||
* Info passed around about the current state.
|
||||
*/
|
||||
export type State = {
|
||||
/**
|
||||
* Current schema.
|
||||
*/
|
||||
schema: Schema;
|
||||
/**
|
||||
* Corresponding file.
|
||||
*/
|
||||
file: VFile | undefined;
|
||||
/**
|
||||
* Add extra positional info.
|
||||
*/
|
||||
verbose: boolean | undefined;
|
||||
/**
|
||||
* Whether location info was found.
|
||||
*/
|
||||
location: boolean;
|
||||
};
|
||||
384
node_modules/hast-util-from-parse5/lib/index.js
generated
vendored
Normal file
384
node_modules/hast-util-from-parse5/lib/index.js
generated
vendored
Normal file
|
|
@ -0,0 +1,384 @@
|
|||
/**
|
||||
* @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').Root} Root
|
||||
* @typedef {import('hast').Content} Content
|
||||
* @typedef {import('parse5').DefaultTreeAdapterMap} DefaultTreeAdapterMap
|
||||
* @typedef {import('parse5').Token.ElementLocation} P5ElementLocation
|
||||
* @typedef {import('parse5').Token.Location} P5Location
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Content | Root} Node
|
||||
* @typedef {DefaultTreeAdapterMap['document']} P5Document
|
||||
* @typedef {DefaultTreeAdapterMap['documentFragment']} P5DocumentFragment
|
||||
* @typedef {DefaultTreeAdapterMap['documentType']} P5DocumentType
|
||||
* @typedef {DefaultTreeAdapterMap['commentNode']} P5Comment
|
||||
* @typedef {DefaultTreeAdapterMap['textNode']} P5Text
|
||||
* @typedef {DefaultTreeAdapterMap['element']} P5Element
|
||||
* @typedef {DefaultTreeAdapterMap['node']} P5Node
|
||||
* @typedef {DefaultTreeAdapterMap['template']} P5Template
|
||||
*
|
||||
* @typedef {'html' | 'svg'} Space
|
||||
* Namespace.
|
||||
*
|
||||
* @typedef Options
|
||||
* Configuration.
|
||||
* @property {Space | null | undefined} [space='html']
|
||||
* Which space the document is in.
|
||||
*
|
||||
* When an `<svg>` element is found in the HTML space, this package already
|
||||
* automatically switches to and from the SVG space when entering and exiting
|
||||
* it.
|
||||
* @property {VFile | null | undefined} [file]
|
||||
* File used to add positional info to nodes.
|
||||
*
|
||||
* If given, the file should represent the original HTML source.
|
||||
* @property {boolean} [verbose=false]
|
||||
* Whether to add extra positional info about starting tags, closing tags,
|
||||
* and attributes to elements.
|
||||
*
|
||||
* > 👉 **Note**: only used when `file` is given.
|
||||
*
|
||||
* @typedef State
|
||||
* Info passed around about the current state.
|
||||
* @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 {h, s} from 'hastscript'
|
||||
import {html, svg, find} from 'property-information'
|
||||
import {location} from 'vfile-location'
|
||||
import {webNamespaces} from 'web-namespaces'
|
||||
|
||||
const own = {}.hasOwnProperty
|
||||
/** @type {unknown} */
|
||||
// type-coverage:ignore-next-line
|
||||
const proto = Object.prototype
|
||||
|
||||
/**
|
||||
* Transform a `parse5` AST to hast.
|
||||
*
|
||||
* @param {P5Node} tree
|
||||
* `parse5` tree to transform.
|
||||
* @param {Options | VFile | null | undefined} [options]
|
||||
* Configuration.
|
||||
* @returns {Node}
|
||||
* hast tree.
|
||||
*/
|
||||
export function fromParse5(tree, options) {
|
||||
const options_ = options || {}
|
||||
/** @type {Options} */
|
||||
let settings
|
||||
/** @type {VFile | undefined} */
|
||||
let file
|
||||
|
||||
if (isFile(options_)) {
|
||||
file = options_
|
||||
settings = {}
|
||||
} else {
|
||||
file = options_.file || undefined
|
||||
settings = options_
|
||||
}
|
||||
|
||||
return one(
|
||||
{
|
||||
schema: settings.space === 'svg' ? svg : html,
|
||||
file,
|
||||
verbose: settings.verbose,
|
||||
location: false
|
||||
},
|
||||
tree
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform a node.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around about the current state.
|
||||
* @param {P5Node} node
|
||||
* p5 node.
|
||||
* @returns {Node}
|
||||
* hast node.
|
||||
*/
|
||||
function one(state, node) {
|
||||
/** @type {Node} */
|
||||
let result
|
||||
|
||||
switch (node.nodeName) {
|
||||
case '#comment': {
|
||||
const reference = /** @type {P5Comment} */ (node)
|
||||
result = {type: 'comment', value: reference.data}
|
||||
patch(state, reference, result)
|
||||
return result
|
||||
}
|
||||
|
||||
case '#document':
|
||||
case '#document-fragment': {
|
||||
const reference = /** @type {P5Document | P5DocumentFragment} */ (node)
|
||||
const quirksMode =
|
||||
'mode' in reference
|
||||
? reference.mode === 'quirks' || reference.mode === 'limited-quirks'
|
||||
: false
|
||||
|
||||
result = {
|
||||
type: 'root',
|
||||
children: all(state, node.childNodes),
|
||||
data: {quirksMode}
|
||||
}
|
||||
|
||||
if (state.file && state.location) {
|
||||
const doc = String(state.file)
|
||||
const loc = location(doc)
|
||||
const start = loc.toPoint(0)
|
||||
const end = loc.toPoint(doc.length)
|
||||
// @ts-expect-error: always defined as we give valid input.
|
||||
result.position = {start, end}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
case '#documentType': {
|
||||
const reference = /** @type {P5DocumentType} */ (node)
|
||||
// @ts-expect-error Types are out of date.
|
||||
result = {type: 'doctype'}
|
||||
patch(state, reference, result)
|
||||
return result
|
||||
}
|
||||
|
||||
case '#text': {
|
||||
const reference = /** @type {P5Text} */ (node)
|
||||
result = {type: 'text', value: reference.value}
|
||||
patch(state, reference, result)
|
||||
return result
|
||||
}
|
||||
|
||||
// Element.
|
||||
default: {
|
||||
const reference = /** @type {P5Element} */ (node)
|
||||
result = element(state, reference)
|
||||
return result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform children.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around about the current state.
|
||||
* @param {Array<P5Node>} nodes
|
||||
* Nodes.
|
||||
* @returns {Array<Content>}
|
||||
* hast nodes.
|
||||
*/
|
||||
function all(state, nodes) {
|
||||
let index = -1
|
||||
/** @type {Array<Content>} */
|
||||
const result = []
|
||||
|
||||
while (++index < nodes.length) {
|
||||
// @ts-expect-error Assume no roots in `nodes`.
|
||||
result[index] = one(state, nodes[index])
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform an element.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around about the current state.
|
||||
* @param {P5Element} node
|
||||
* `parse5` node to transform.
|
||||
* @returns {Element}
|
||||
* hast node.
|
||||
*/
|
||||
function element(state, node) {
|
||||
const schema = state.schema
|
||||
|
||||
state.schema = node.namespaceURI === webNamespaces.svg ? svg : html
|
||||
|
||||
// Props.
|
||||
let index = -1
|
||||
/** @type {Record<string, string>} */
|
||||
const props = {}
|
||||
|
||||
while (++index < node.attrs.length) {
|
||||
const attribute = node.attrs[index]
|
||||
const name =
|
||||
(attribute.prefix ? attribute.prefix + ':' : '') + attribute.name
|
||||
if (!own.call(proto, name)) {
|
||||
props[name] = attribute.value
|
||||
}
|
||||
}
|
||||
|
||||
// Build.
|
||||
const fn = state.schema.space === 'svg' ? s : h
|
||||
const result = fn(node.tagName, props, all(state, node.childNodes))
|
||||
patch(state, node, result)
|
||||
|
||||
// Switch content.
|
||||
if (result.tagName === 'template') {
|
||||
const reference = /** @type {P5Template} */ (node)
|
||||
const pos = reference.sourceCodeLocation
|
||||
const startTag = pos && pos.startTag && position(pos.startTag)
|
||||
const endTag = pos && pos.endTag && position(pos.endTag)
|
||||
|
||||
/** @type {Root} */
|
||||
// @ts-expect-error Types are wrong.
|
||||
const content = one(state, reference.content)
|
||||
|
||||
if (startTag && endTag && state.file) {
|
||||
content.position = {start: startTag.end, end: endTag.start}
|
||||
}
|
||||
|
||||
result.content = content
|
||||
}
|
||||
|
||||
state.schema = schema
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Patch positional info from `from` onto `to`.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around about the current state.
|
||||
* @param {P5Node} from
|
||||
* p5 node.
|
||||
* @param {Node} to
|
||||
* hast node.
|
||||
* @returns {void}
|
||||
* Nothing.
|
||||
*/
|
||||
function patch(state, from, to) {
|
||||
if ('sourceCodeLocation' in from && from.sourceCodeLocation && state.file) {
|
||||
const position = createLocation(state, to, from.sourceCodeLocation)
|
||||
|
||||
if (position) {
|
||||
state.location = true
|
||||
to.position = position
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create clean positional information.
|
||||
*
|
||||
* @param {State} state
|
||||
* Info passed around about the current state.
|
||||
* @param {Node} node
|
||||
* hast node.
|
||||
* @param {P5ElementLocation} location
|
||||
* p5 location info.
|
||||
* @returns {Position | undefined}
|
||||
* Position, or nothing.
|
||||
*/
|
||||
function createLocation(state, node, location) {
|
||||
const result = position(location)
|
||||
|
||||
if (node.type === 'element') {
|
||||
const tail = node.children[node.children.length - 1]
|
||||
|
||||
// Bug for unclosed with children.
|
||||
// See: <https://github.com/inikulin/parse5/issues/109>.
|
||||
if (
|
||||
result &&
|
||||
!location.endTag &&
|
||||
tail &&
|
||||
tail.position &&
|
||||
tail.position.end
|
||||
) {
|
||||
result.end = Object.assign({}, tail.position.end)
|
||||
}
|
||||
|
||||
if (state.verbose) {
|
||||
/** @type {Record<string, Position | undefined>} */
|
||||
const props = {}
|
||||
/** @type {string} */
|
||||
let key
|
||||
|
||||
if (location.attrs) {
|
||||
for (key in location.attrs) {
|
||||
if (own.call(location.attrs, key)) {
|
||||
props[find(state.schema, key).property] = position(
|
||||
location.attrs[key]
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
node.data = {
|
||||
position: {
|
||||
// @ts-expect-error: assume not `undefined`.
|
||||
opening: position(location.startTag),
|
||||
closing: location.endTag ? position(location.endTag) : null,
|
||||
properties: props
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a p5 location into a position.
|
||||
*
|
||||
* @param {P5Location} loc
|
||||
* Location.
|
||||
* @returns {Position | undefined}
|
||||
* Position or nothing.
|
||||
*/
|
||||
function position(loc) {
|
||||
const start = point({
|
||||
line: loc.startLine,
|
||||
column: loc.startCol,
|
||||
offset: loc.startOffset
|
||||
})
|
||||
const end = point({
|
||||
line: loc.endLine,
|
||||
column: loc.endCol,
|
||||
offset: loc.endOffset
|
||||
})
|
||||
// @ts-expect-error `undefined` is fine.
|
||||
return start || end ? {start, end} : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter out invalid points.
|
||||
*
|
||||
* @param {Point} point
|
||||
* Point with potentially `undefined` values.
|
||||
* @returns {Point | undefined}
|
||||
* Point or nothing.
|
||||
*/
|
||||
function point(point) {
|
||||
return point.line && point.column ? point : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
22
node_modules/hast-util-from-parse5/license
generated
vendored
Normal file
22
node_modules/hast-util-from-parse5/license
generated
vendored
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(The MIT License)
|
||||
|
||||
Copyright (c) 2016 Titus Wormer <tituswormer@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
'Software'), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
98
node_modules/hast-util-from-parse5/package.json
generated
vendored
Normal file
98
node_modules/hast-util-from-parse5/package.json
generated
vendored
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
{
|
||||
"name": "hast-util-from-parse5",
|
||||
"version": "7.1.2",
|
||||
"description": "hast utility to transform from Parse5’s AST",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"unist",
|
||||
"hast",
|
||||
"hast-util",
|
||||
"util",
|
||||
"utility",
|
||||
"transform",
|
||||
"change",
|
||||
"ast"
|
||||
],
|
||||
"repository": "syntax-tree/hast-util-from-parse5",
|
||||
"bugs": "https://github.com/syntax-tree/hast-util-from-parse5/issues",
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/unified"
|
||||
},
|
||||
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
|
||||
"contributors": [
|
||||
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
|
||||
],
|
||||
"sideEffects": false,
|
||||
"type": "module",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"files": [
|
||||
"lib/",
|
||||
"index.d.ts",
|
||||
"index.js"
|
||||
],
|
||||
"dependencies": {
|
||||
"@types/hast": "^2.0.0",
|
||||
"@types/unist": "^2.0.0",
|
||||
"hastscript": "^7.0.0",
|
||||
"property-information": "^6.0.0",
|
||||
"vfile": "^5.0.0",
|
||||
"vfile-location": "^4.0.0",
|
||||
"web-namespaces": "^2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.0.0",
|
||||
"c8": "^7.0.0",
|
||||
"is-hidden": "^2.0.0",
|
||||
"parse5": "^7.0.0",
|
||||
"prettier": "^2.0.0",
|
||||
"remark-cli": "^11.0.0",
|
||||
"remark-preset-wooorm": "^9.0.0",
|
||||
"to-vfile": "^7.0.0",
|
||||
"type-coverage": "^2.0.0",
|
||||
"typescript": "^4.0.0",
|
||||
"unist-util-visit": "^4.0.0",
|
||||
"xo": "^0.53.0"
|
||||
},
|
||||
"scripts": {
|
||||
"prepack": "npm run build && npm run format",
|
||||
"build": "tsc --build --clean && tsc --build && type-coverage",
|
||||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
|
||||
"test-api": "node --conditions development test/index.js",
|
||||
"test-coverage": "c8 --check-coverage --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"
|
||||
},
|
||||
"xo": {
|
||||
"prettier": true,
|
||||
"rules": {
|
||||
"max-depth": "off"
|
||||
},
|
||||
"overrides": [
|
||||
{
|
||||
"files": "test/**/*.js",
|
||||
"rules": {
|
||||
"no-await-in-loop": 0
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"remarkConfig": {
|
||||
"plugins": [
|
||||
"preset-wooorm"
|
||||
]
|
||||
},
|
||||
"typeCoverage": {
|
||||
"atLeast": 100,
|
||||
"detail": true,
|
||||
"strict": true
|
||||
}
|
||||
}
|
||||
333
node_modules/hast-util-from-parse5/readme.md
generated
vendored
Normal file
333
node_modules/hast-util-from-parse5/readme.md
generated
vendored
Normal file
|
|
@ -0,0 +1,333 @@
|
|||
# hast-util-from-parse5
|
||||
|
||||
[![Build][build-badge]][build]
|
||||
[![Coverage][coverage-badge]][coverage]
|
||||
[![Downloads][downloads-badge]][downloads]
|
||||
[![Size][size-badge]][size]
|
||||
[![Sponsors][sponsors-badge]][collective]
|
||||
[![Backers][backers-badge]][collective]
|
||||
[![Chat][chat-badge]][chat]
|
||||
|
||||
[hast][] utility to transform from [`parse5`][parse5]s AST.
|
||||
|
||||
## Contents
|
||||
|
||||
* [What is this?](#what-is-this)
|
||||
* [When should I use this?](#when-should-i-use-this)
|
||||
* [Install](#install)
|
||||
* [Use](#use)
|
||||
* [API](#api)
|
||||
* [`fromParse5(tree[, file|options])`](#fromparse5tree-fileoptions)
|
||||
* [`Options`](#options)
|
||||
* [`Space`](#space-1)
|
||||
* [Types](#types)
|
||||
* [Compatibility](#compatibility)
|
||||
* [Security](#security)
|
||||
* [Related](#related)
|
||||
* [Contribute](#contribute)
|
||||
* [License](#license)
|
||||
|
||||
## What is this?
|
||||
|
||||
This package is a utility that can turn a parse5 tree into a hast tree.
|
||||
|
||||
## When should I use this?
|
||||
|
||||
You can use this package when using `parse5` as an HTML parser and wanting to
|
||||
work with hast.
|
||||
|
||||
The utility [`hast-util-to-parse5`][hast-util-to-parse5] does the inverse of
|
||||
this utility.
|
||||
It generates `parse5`s AST again.
|
||||
|
||||
The utility [`hast-util-from-html`][hast-util-from-html] wraps this utility and
|
||||
`parse5` to both parse HTML and generate hast from it.
|
||||
|
||||
## Install
|
||||
|
||||
This package is [ESM only][esm].
|
||||
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
|
||||
|
||||
```sh
|
||||
npm install hast-util-from-parse5
|
||||
```
|
||||
|
||||
In Deno with [`esm.sh`][esmsh]:
|
||||
|
||||
```js
|
||||
import {fromParse5} from "https://esm.sh/hast-util-from-parse5@7"
|
||||
```
|
||||
|
||||
In browsers with [`esm.sh`][esmsh]:
|
||||
|
||||
```html
|
||||
<script type="module">
|
||||
import {fromParse5} from "https://esm.sh/hast-util-from-parse5@7?bundle"
|
||||
</script>
|
||||
```
|
||||
|
||||
## Use
|
||||
|
||||
Say our document `example.html` contains:
|
||||
|
||||
```html
|
||||
<!doctype html><title>Hello!</title><h1 id="world">World!<!--after-->
|
||||
```
|
||||
|
||||
…and our module `example.js` looks as follows:
|
||||
|
||||
```js
|
||||
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)
|
||||
|
||||
console.log(inspect(hast))
|
||||
```
|
||||
|
||||
…now running `node example.js` yields:
|
||||
|
||||
```text
|
||||
root[2] (1:1-2:1, 0-70)
|
||||
│ data: {"quirksMode":false}
|
||||
├─0 doctype<html> (1:1-1:16, 0-15)
|
||||
│ public: null
|
||||
│ system: null
|
||||
└─1 element<html>[2]
|
||||
│ properties: {}
|
||||
├─0 element<head>[1]
|
||||
│ │ properties: {}
|
||||
│ └─0 element<title>[1] (1:16-1:37, 15-36)
|
||||
│ │ properties: {}
|
||||
│ └─0 text "Hello!" (1:23-1:29, 22-28)
|
||||
└─1 element<body>[1]
|
||||
│ properties: {}
|
||||
└─0 element<h1>[3] (1:37-2:1, 36-70)
|
||||
│ properties: {"id":"world"}
|
||||
├─0 text "World!" (1:52-1:58, 51-57)
|
||||
├─1 comment "after" (1:58-1:70, 57-69)
|
||||
└─2 text "\n" (1:70-2:1, 69-70)
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
This package exports the identifier [`fromParse5`][fromparse5].
|
||||
There is no default export.
|
||||
|
||||
### `fromParse5(tree[, file|options])`
|
||||
|
||||
Transform a `parse5` AST to hast.
|
||||
|
||||
###### Parameters
|
||||
|
||||
* `tree` ([`Parse5Node`][parse5-node])
|
||||
— `parse5` tree to transform
|
||||
* `file` ([`VFile`][vfile], optional)
|
||||
— corresponding file (treated as `{file: file}`)
|
||||
* `options` ([`Options`][options], optional)
|
||||
— configuration
|
||||
|
||||
###### Returns
|
||||
|
||||
hast tree ([`HastNode`][hast-node]).
|
||||
|
||||
### `Options`
|
||||
|
||||
Configuration (TypeScript type).
|
||||
|
||||
##### Fields
|
||||
|
||||
###### `space`
|
||||
|
||||
Which space the document is in ([`Space`][space], default: `'html'`).
|
||||
|
||||
When an `<svg>` element is found in the HTML space, this package already
|
||||
automatically switches to and from the SVG space when entering and exiting
|
||||
it.
|
||||
|
||||
###### `file`
|
||||
|
||||
File used to add positional info to nodes ([`VFile`][vfile], optional).
|
||||
|
||||
If given, the file should represent the original HTML source.
|
||||
|
||||
###### `verbose`
|
||||
|
||||
Whether to add extra positional info about starting tags, closing tags,
|
||||
and attributes to elements (`boolean`, default: `false`).
|
||||
|
||||
> 👉 **Note**: only used when `file` is given.
|
||||
|
||||
For the following HTML:
|
||||
|
||||
```html
|
||||
<img src="http://example.com/fav.ico" alt="foo" title="bar">
|
||||
```
|
||||
|
||||
The verbose info would looks as follows:
|
||||
|
||||
```js
|
||||
{
|
||||
type: 'element',
|
||||
tagName: 'img',
|
||||
properties: {src: 'http://example.com/fav.ico', alt: 'foo', title: 'bar'},
|
||||
children: [],
|
||||
data: {
|
||||
position: {
|
||||
opening: {
|
||||
start: {line: 1, column: 1, offset: 0},
|
||||
end: {line: 1, column: 61, offset: 60}
|
||||
},
|
||||
closing: null,
|
||||
properties: {
|
||||
src: {
|
||||
start: {line: 1, column: 6, offset: 5},
|
||||
end: {line: 1, column: 38, offset: 37}
|
||||
},
|
||||
alt: {
|
||||
start: {line: 1, column: 39, offset: 38},
|
||||
end: {line: 1, column: 48, offset: 47}
|
||||
},
|
||||
title: {
|
||||
start: {line: 1, column: 49, offset: 48},
|
||||
end: {line: 1, column: 60, offset: 59}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
position: {
|
||||
start: {line: 1, column: 1, offset: 0},
|
||||
end: {line: 1, column: 61, offset: 60}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### `Space`
|
||||
|
||||
Namespace (TypeScript type).
|
||||
|
||||
###### Type
|
||||
|
||||
```ts
|
||||
type Space = 'html' | 'svg'
|
||||
```
|
||||
|
||||
## Types
|
||||
|
||||
This package is fully typed with [TypeScript][].
|
||||
It exports the additional types [`Options`][options] and [`Space`][space].
|
||||
|
||||
## Compatibility
|
||||
|
||||
Projects maintained by the unified collective are compatible with all maintained
|
||||
versions of Node.js.
|
||||
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
|
||||
Our projects sometimes work with older versions, but this is not guaranteed.
|
||||
|
||||
## Security
|
||||
|
||||
Use of `hast-util-from-parse5` can open you up to a
|
||||
[cross-site scripting (XSS)][xss] attack if Parse5’s AST is unsafe.
|
||||
|
||||
## Related
|
||||
|
||||
* [`hast-util-to-parse5`](https://github.com/syntax-tree/hast-util-to-parse5)
|
||||
— transform hast to Parse5’s AST
|
||||
* [`hast-util-to-nlcst`](https://github.com/syntax-tree/hast-util-to-nlcst)
|
||||
— transform hast to nlcst
|
||||
* [`hast-util-to-mdast`](https://github.com/syntax-tree/hast-util-to-mdast)
|
||||
— transform hast to mdast
|
||||
* [`hast-util-to-xast`](https://github.com/syntax-tree/hast-util-to-xast)
|
||||
— transform hast to xast
|
||||
* [`mdast-util-to-hast`](https://github.com/syntax-tree/mdast-util-to-hast)
|
||||
— transform mdast to hast
|
||||
* [`mdast-util-to-nlcst`](https://github.com/syntax-tree/mdast-util-to-nlcst)
|
||||
— transform mdast to nlcst
|
||||
|
||||
## Contribute
|
||||
|
||||
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
|
||||
ways to get started.
|
||||
See [`support.md`][support] for ways to get help.
|
||||
|
||||
This project has a [code of conduct][coc].
|
||||
By interacting with this repository, organization, or community you agree to
|
||||
abide by its terms.
|
||||
|
||||
## License
|
||||
|
||||
[MIT][license] © [Titus Wormer][author]
|
||||
|
||||
<!-- Definitions -->
|
||||
|
||||
[build-badge]: https://github.com/syntax-tree/hast-util-from-parse5/workflows/main/badge.svg
|
||||
|
||||
[build]: https://github.com/syntax-tree/hast-util-from-parse5/actions
|
||||
|
||||
[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-from-parse5.svg
|
||||
|
||||
[coverage]: https://codecov.io/github/syntax-tree/hast-util-from-parse5
|
||||
|
||||
[downloads-badge]: https://img.shields.io/npm/dm/hast-util-from-parse5.svg
|
||||
|
||||
[downloads]: https://www.npmjs.com/package/hast-util-from-parse5
|
||||
|
||||
[size-badge]: https://img.shields.io/bundlephobia/minzip/hast-util-from-parse5.svg
|
||||
|
||||
[size]: https://bundlephobia.com/result?p=hast-util-from-parse5
|
||||
|
||||
[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
|
||||
|
||||
[backers-badge]: https://opencollective.com/unified/backers/badge.svg
|
||||
|
||||
[collective]: https://opencollective.com/unified
|
||||
|
||||
[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg
|
||||
|
||||
[chat]: https://github.com/syntax-tree/unist/discussions
|
||||
|
||||
[npm]: https://docs.npmjs.com/cli/install
|
||||
|
||||
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
|
||||
|
||||
[esmsh]: https://esm.sh
|
||||
|
||||
[typescript]: https://www.typescriptlang.org
|
||||
|
||||
[license]: license
|
||||
|
||||
[author]: https://wooorm.com
|
||||
|
||||
[health]: https://github.com/syntax-tree/.github
|
||||
|
||||
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
|
||||
|
||||
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
|
||||
|
||||
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
|
||||
|
||||
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
|
||||
|
||||
[parse5]: https://github.com/inikulin/parse5
|
||||
|
||||
[parse5-node]: https://github.com/inikulin/parse5/blob/master/packages/parse5/lib/tree-adapters/default.ts
|
||||
|
||||
[vfile]: https://github.com/vfile/vfile
|
||||
|
||||
[hast-util-to-parse5]: https://github.com/syntax-tree/hast-util-to-parse5
|
||||
|
||||
[hast]: https://github.com/syntax-tree/hast
|
||||
|
||||
[hast-util-from-html]: https://github.com/syntax-tree/hast-util-from-html
|
||||
|
||||
[hast-node]: https://github.com/syntax-tree/hast#nodes
|
||||
|
||||
[fromparse5]: #fromparse5tree-fileoptions
|
||||
|
||||
[options]: #options
|
||||
|
||||
[space]: #space-1
|
||||
Loading…
Add table
Add a link
Reference in a new issue