npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hast-util-from-parse5

v8.0.2

Published

hast utility to transform from a `parse5` AST

Downloads

16,263,301

Readme

hast-util-from-parse5

Build Coverage Downloads Size Sponsors Backers Chat

hast utility to transform from parse5s AST.

Contents

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 does the inverse of this utility. It generates parse5s AST again.

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

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install hast-util-from-parse5

In Deno with esm.sh:

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

In browsers with esm.sh:

<script type="module">
  import {fromParse5} from "https://esm.sh/hast-util-from-parse5@8?bundle"
</script>

Use

Say our document example.html contains:

<!doctype html><title>Hello!</title><h1 id="world">World!<!--after-->

…and our module example.js looks as follows:

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:

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. There is no default export.

fromParse5(tree[, options])

Transform a parse5 AST to hast.

Parameters
  • tree (Parse5Node) — parse5 tree to transform
  • options (Options, optional) — configuration
Returns

hast tree (HastNode).

Options

Configuration (TypeScript type).

Fields
file

File used to add positional info to nodes (VFile, optional).

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

space

Which space the document is in (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:

<img src="http://example.com/fav.ico" alt="foo" title="bar">

The verbose info would looks as follows:

{
  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
type Space = 'html' | 'svg'

Types

This package is fully typed with TypeScript. It exports the additional types Options and 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) attack if Parse5’s AST is unsafe.

Related

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

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

License

MIT © Titus Wormer