Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8
utility to transform Parse5’s AST to hast
License
syntax-tree/hast-util-from-parse5
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
hast utility to transform from theparse5 AST.
This package is a utility that can turn a parse5 tree into a hast tree.
You can use this package when usingparse5 as an HTML parser and wanting towork with hast.
The utilityhast-util-to-parse5 does theinverse of this utility.It generatesparse5s AST again.
The utilityhast-util-from-html wraps thisutility andparse5 to both parse HTML and generate hast from it.
This package isESM only.In Node.js (version 16+),install withnpm:
npm install hast-util-from-parse5
In Deno withesm.sh:
import{fromParse5}from"https://esm.sh/hast-util-from-parse5@8"
In browsers withesm.sh:
<scripttype="module">import{fromParse5}from"https://esm.sh/hast-util-from-parse5@8?bundle"</script>
Say our documentexample.html contains:
<!doctype html><title>Hello!</title><h1id="world">World!<!--after-->
…and our moduleexample.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'constfile=awaitread('example.html')constp5ast=parse(String(file),{sourceCodeLocationInfo:true})consthast=fromParse5(p5ast,{file})console.log(inspect(hast))
…now runningnode 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)This package exports the identifierfromParse5.There is no default export.
Transform aparse5 AST to hast.
tree(Parse5Node)—parse5tree to transformoptions(Options, optional)— configuration
hast tree (HastNode).
Configuration (TypeScript type).
File used to add positional info to nodes(VFile, optional).
If given,the file should represent the original HTML source.
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 whenentering and exiting it.
Whether to add extra positional info about starting tags,closing tags,and attributes to elements(boolean, default:false).
👉Note:only used when
fileis given.
For the following HTML:
<imgsrc="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}}}
Namespace (TypeScript type).
typeSpace='html'|'svg'
This package is fully typed withTypeScript.It exports the additional typesOptions andSpace.
Projects maintained by the unified collective are compatible with maintainedversions 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.
Use ofhast-util-from-parse5 can open you up to across-site scripting (XSS) attack if Parse5’s AST is unsafe.
hast-util-to-parse5— transform hast to Parse5’s ASThast-util-to-nlcst— transform hast to nlcsthast-util-to-mdast— transform hast to mdasthast-util-to-xast— transform hast to xastmdast-util-to-hast— transform mdast to hastmdast-util-to-nlcst— transform mdast to nlcst
Seecontributing.mdinsyntax-tree/.githubfor ways to get started.Seesupport.md for ways to get help.
This project has acode of conduct.By interacting with this repository,organization,or community you agree to abide by its terms.
About
utility to transform Parse5’s AST to hast
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.