# hast-util-from-parse5

> hast utility to transform from a `parse5` AST

Latest version **8.0.3** (published 2025-02-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install hast-util-from-parse5
pnpm add hast-util-from-parse5
yarn add hast-util-from-parse5
bun add hast-util-from-parse5
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 8.0.3 |
| Published | 2025-02-19 |
| First published | 2016-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 8 |
| Unpacked size | 24 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | Titus Wormer |
| Maintainers | wooorm, kmck |
| Keywords | ast, change, hast-util, hast, transform, unist, utility, util |

## Links

- npm: https://www.npmjs.com/package/hast-util-from-parse5
- Repository: https://github.com/syntax-tree/hast-util-from-parse5
- Homepage: https://github.com/syntax-tree/hast-util-from-parse5#readme
- Issues: https://github.com/syntax-tree/hast-util-from-parse5/issues
- Funding: https://opencollective.com/unified
- npm.io page: https://npm.io/package/hast-util-from-parse5

## Dependencies (8)

- [vfile](https://npm.io/package/vfile.md) ^6.0.0
- [devlop](https://npm.io/package/devlop.md) ^1.0.0
- [hastscript](https://npm.io/package/hastscript.md) ^9.0.0
- [@types/hast](https://npm.io/package/@types/hast.md) ^3.0.0
- [@types/unist](https://npm.io/package/@types/unist.md) ^3.0.0
- [vfile-location](https://npm.io/package/vfile-location.md) ^5.0.0
- [web-namespaces](https://npm.io/package/web-namespaces.md) ^2.0.0
- [property-information](https://npm.io/package/property-information.md) ^7.0.0

## Alternatives

- [update-check](https://npm.io/package/update-check.md) — 4.0M weekly downloads
- [react-native-onesignal](https://npm.io/package/react-native-onesignal.md) — 134.5K weekly downloads
- [react-redux-toastr](https://npm.io/package/react-redux-toastr.md) — 33.7K weekly downloads
- [@nocobase/plugin-notification-manager](https://npm.io/package/@nocobase/plugin-notification-manager.md) — 2.0K weekly downloads
- [react-simple-toasts](https://npm.io/package/react-simple-toasts.md) — 1.9K weekly downloads

## Recent versions

- 8.0.3 (latest) — 2025-02-19
- 8.0.2 — 2024-11-19
- 8.0.1 — 2023-08-08
- 8.0.0 — 2023-08-05
- 7.1.2 — 2023-02-16
- 7.1.1 — 2023-01-12
- 7.1.0 — 2021-07-23
- 7.0.0 — 2021-05-01
- 6.0.1 — 2020-11-01
- 6.0.0 — 2020-05-21
- 5.0.3 — 2020-02-29
- 5.0.2 — 2019-11-10
- 5.0.1 — 2019-05-26
- 5.0.0 — 2018-11-07
- 4.0.2 — 2018-07-18
- … 8 more at https://npm.io/package/hast-util-from-parse5/versions

## README

# hast-util-from-parse5

[![Build][badge-build-image]][badge-build-url]
[![Coverage][badge-coverage-image]][badge-coverage-url]
[![Downloads][badge-downloads-image]][badge-downloads-url]
[![Size][badge-size-image]][badge-size-url]

[hast][github-hast] utility to transform from the
[`parse5`][github-parse5] 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[, options])`](#fromparse5tree-options)
  * [`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`][github-hast-util-to-parse5] does the
inverse of this utility.
It generates `parse5`s AST again.

The utility [`hast-util-from-html`][github-hast-util-from-html] wraps this
utility and `parse5` to both parse HTML and generate hast from it.

## Install

This package is [ESM only][github-gist-esm].
In Node.js (version 16+),
install with [npm][npmjs-install]:

```sh
npm install hast-util-from-parse5
```

In Deno with [`esm.sh`][esmsh]:

```js
import {fromParse5} from "https://esm.sh/hast-util-from-parse5@8"
```

In browsers with [`esm.sh`][esmsh]:

```html
<script type="module">
  import {fromParse5} from "https://esm.sh/hast-util-from-parse5@8?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 {fromParse5} from 'hast-util-from-parse5'
import {parse} from 'parse5'
import {read} from 'to-vfile'
import {inspect} from 'unist-util-inspect'

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 (1:1-1:16, 0-15)
└─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`][api-from-parse5].
There is no default export.

### `fromParse5(tree[, options])`

Transform a `parse5` AST to hast.

###### Parameters

* `tree`
  ([`Parse5Node`][github-parse5-node])
  — `parse5` tree to transform
* `options`
  ([`Options`][api-options], optional)
  — configuration

###### Returns

hast tree ([`HastNode`][github-hast-nodes]).

### `Options`

Configuration (TypeScript type).

##### Fields

###### `file`

File used to add positional info to nodes
([`VFile`][github-vfile], optional).

If given,
the file should represent the original HTML source.

###### `space`

Which space the document is in
([`Space`][api-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.

###### `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`][api-options] and
[`Space`][api-space].

## Compatibility

Projects maintained by the unified collective are compatible with maintained
versions of Node.js.

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

Use of `hast-util-from-parse5` can open you up to a
[cross-site scripting (XSS)][wikipedia-xss] attack if Parse5’s AST is unsafe.

## Related

* [`hast-util-to-parse5`][github-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`][health-contributing]
in
[`syntax-tree/.github`][health]
for ways to get started.
See [`support.md`][health-support] for ways to get help.

This project has a [code of conduct][health-coc].
By interacting with this repository,
organization,
or community you agree to abide by its terms.

## License

[MIT][file-license] © [Titus Wormer][wooorm]

<!-- Definitions -->

[api-from-parse5]: #fromparse5tree-options

[api-options]: #options

[api-space]: #space-1

[badge-build-image]: https://github.com/syntax-tree/hast-util-from-parse5/workflows/main/badge.svg

[badge-build-url]: https://github.com/syntax-tree/hast-util-from-parse5/actions

[badge-coverage-image]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-from-parse5.svg

[badge-coverage-url]: https://codecov.io/github/syntax-tree/hast-util-from-parse5

[badge-downloads-image]: https://img.shields.io/npm/dm/hast-util-from-parse5.svg

[badge-downloads-url]: https://www.npmjs.com/package/hast-util-from-parse5

[badge-size-image]: https://img.shields.io/bundlejs/size/hast-util-from-parse5

[badge-size-url]: https://bundlejs.com/?q=hast-util-from-parse5

[esmsh]: https://esm.sh

[file-license]: license

[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

[github-hast]: https://github.com/syntax-tree/hast

[github-hast-nodes]: https://github.com/syntax-tree/hast#nodes

[github-hast-util-from-html]: https://github.com/syntax-tree/hast-util-from-html

[github-hast-util-to-parse5]: https://github.com/syntax-tree/hast-util-to-parse5

[github-parse5]: https://github.com/inikulin/parse5

[github-parse5-node]: https://github.com/inikulin/parse5/blob/master/packages/parse5/lib/tree-adapters/default.ts

[github-vfile]: https://github.com/vfile/vfile

[health]: https://github.com/syntax-tree/.github

[health-coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md

[health-contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md

[health-support]: https://github.com/syntax-tree/.github/blob/main/support.md

[npmjs-install]: https://docs.npmjs.com/cli/install

[typescript]: https://www.typescriptlang.org

[wikipedia-xss]: https://en.wikipedia.org/wiki/Cross-site_scripting

[wooorm]: https://wooorm.com

---
_Source: https://npm.io/package/hast-util-from-parse5 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
