hast utility to transform to estree (JavaScript AST) JSX
Installations
npm install hast-util-to-estree
Developer
Developer Guide
Module System
ESM
Min. Node Version
Typescript Support
Yes
Node Version
20.5.1
NPM Version
9.8.0
Statistics
19 Stars
127 Commits
2 Forks
8 Watching
1 Branches
12 Contributors
Updated on 23 Sept 2024
Bundle Size
29.80 kB
Minified
9.88 kB
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
93,219,829
Last day
0.2%
277,732
Compared to previous day
Last week
6.2%
1,497,742
Compared to previous week
Last month
13.7%
6,191,353
Compared to previous month
Last year
88.1%
56,159,737
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
16
hast-util-to-estree
hast utility to transform to estree (JSX).
Contents
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Security
- Related
- Contribute
- License
What is this?
This package is a utility that takes a hast (HTML) syntax tree as input and turns it into an estree (JavaScript) syntax tree (with a JSX extension). This package also supports embedded MDX nodes.
When should I use this?
This project is useful when you want to embed HTML as JSX inside JS while working with syntax trees. This is used in MDX.
Install
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install hast-util-to-estree
In Deno with esm.sh
:
1import {toEstree} from 'https://esm.sh/hast-util-to-estree@3'
In browsers with esm.sh
:
1<script type="module"> 2 import {toEstree} from 'https://esm.sh/hast-util-to-estree@3?bundle' 3</script>
Use
Say our module example.html
contains:
1<!doctype html> 2<html lang=en> 3<title>Hi!</title> 4<link rel=stylesheet href=index.css> 5<h1>Hello, world!</h1> 6<a download style="width:1;height:10px"></a> 7<!--commentz--> 8<svg xmlns="http://www.w3.org/2000/svg"> 9 <title>SVG `<ellipse>` element</title> 10 <ellipse 11 cx="120" 12 cy="70" 13 rx="100" 14 ry="50" 15 /> 16</svg> 17<script src="index.js"></script>
…and our module example.js
looks as follows:
1import fs from 'node:fs/promises' 2import {jsx, toJs} from 'estree-util-to-js' 3import {fromHtml} from 'hast-util-from-html' 4import {toEstree} from 'hast-util-to-estree' 5 6const hast = fromHtml(await fs.readFile('example.html')) 7 8const estree = toEstree(hast) 9 10console.log(toJs(estree, {handlers: jsx}).value)
…now running node example.js
(and prettier) yields:
1/* Commentz */ 2;<> 3 <html lang="en"> 4 <head> 5 <title>{'Hi!'}</title> 6 {'\n'} 7 <link rel="stylesheet" href="index.css" /> 8 {'\n'} 9 </head> 10 <body> 11 <h1>{'Hello, world!'}</h1> 12 {'\n'} 13 <a 14 download 15 style={{ 16 width: '1', 17 height: '10px' 18 }} 19 /> 20 {'\n'} 21 {} 22 {'\n'} 23 <svg xmlns="http://www.w3.org/2000/svg"> 24 {'\n '} 25 <title>{'SVG `<ellipse>` element'}</title> 26 {'\n '} 27 <ellipse cx="120" cy="70" rx="100" ry="50" /> 28 {'\n'} 29 </svg> 30 {'\n'} 31 <script src="index.js" /> 32 {'\n'} 33 </body> 34 </html> 35</>
API
This package exports the identifiers defaultHandlers
and toEstree
.
There is no default export.
toEstree(tree[, options])
Transform a hast tree (with embedded MDX nodes) into an estree (with JSX nodes).
Notes
Comments
Comments are attached to the tree in their neighbouring nodes (recast
,
babel
style) and also added as a comments
array on the program node
(espree
style).
You may have to do program.comments = undefined
for certain compilers.
Frameworks
There are differences between what JSX frameworks accept, such as whether they
accept class
or className
, or background-color
or backgroundColor
.
For JSX components written in MDX, the author has to be aware of this difference and write code accordingly. For hast elements transformed by this project, this will be handled through options.
Framework | elementAttributeNameCase | stylePropertyNameCase |
---|---|---|
Preact | 'html' | 'dom' |
React | 'react' | 'dom' |
Solid | 'html' | 'css' |
Vue | 'html' | 'dom' |
Parameters
Returns
estree program node (Program
).
The program’s last child in body
is most likely an ExpressionStatement
,
whose expression is a JSXFragment
or a JSXElement
.
Typically, there is only one node in body
, however, this utility also supports
embedded MDX nodes in the HTML (when mdast-util-mdx
is used
with mdast to parse markdown before passing its nodes through to hast).
When MDX ESM import/exports are used, those nodes are added before the fragment
or element in body.
There aren’t many great estree serializers out there that support JSX.
To do that, you can use estree-util-to-js
.
Or, use estree-util-build-jsx
to turn JSX into function
calls, and then serialize with whatever (astring
, escodegen
).
defaultHandlers
Default handlers for elements (Record<string, Handle>
).
Each key is a node type, each value is a Handle
.
ElementAttributeNameCase
Specify casing to use for attribute names (TypeScript type).
HTML casing is for example class
, stroke-linecap
, xml:lang
.
React casing is for example className
, strokeLinecap
, xmlLang
.
Type
1type ElementAttributeNameCase = 'html' | 'react'
Handle
Turn a hast node into an estree node (TypeScript type).
Parameters
Returns
JSX child (JsxChild
, optional).
You can also add more results to state.esm
and state.comments
.
Options
Configuration (TypeScript type).
Fields
elementAttributeNameCase
(ElementAttributeNameCase
, default:'react'
) — specify casing to use for attribute names; this casing is used for hast elements, not for embedded MDX JSX nodes (components that someone authored manually)handlers
(Record<string, Handle>
, optional) — custom handlersspace
(Space
, default:'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 itstylePropertyNameCase
(StylePropertyNameCase
, default:'dom'
) — specify casing to use for property names instyle
objects; this casing is used for hast elements, not for embedded MDX JSX nodes (components that someone authored manually)tableCellAlignToStyle
(boolean
, default:true
) — turn obsoletealign
props ontd
andth
into CSSstyle
props
Space
Namespace (TypeScript type).
Type
1type Space = 'html' | 'svg'
State
Info passed around about the current state (TypeScript type).
Fields
all
((node: HastParent) => EstreeJsxChild | undefined
) — transform children of a hast parent to estreecomments
(Array<EstreeComment>
) — list of estree commentscreateJsxAttributeName
((name: string) => EstreeJsxAttributeName
) — create a JSX attribute namecreateJsxElementName
((name: string) => EstreeJsxElementName
) — create a JSX attribute nameelementAttributeNameCase
(ElementAttributeNameCase
) — casing to use for attribute namesesm
(Array<EstreeNode>
) — list of top-level estree nodeshandle
((node: HastNode) => EstreeJsxChild | undefined
) — transform a hast node to estreeinherit
((from: HastNode, to: EstreeNode) => undefined
) — take positional info and data fromfrom
(usepatch
if you don’t want data)patch
((from: HastNode, to: EstreeNode) => undefined
) — take positional info fromfrom
(useinherit
if you also want data)schema
(Schema
) — current schemastylePropertyNameCase
(StylePropertyNameCase
) — casing for property names instyle
objectstableCellAlignToStyle
(boolean
) — turn obsoletealign
props ontd
andth
into CSSstyle
props
StylePropertyNameCase
Casing to use for property names in style
objects (TypeScript type).
CSS casing is for example background-color
and -webkit-line-clamp
.
DOM casing is for example backgroundColor
and WebkitLineClamp
.
Type
1type StylePropertyNameCase = 'css' | 'dom'
Types
This package is fully typed with TypeScript.
It exports the additional types
ElementAttributeNameCase
,
Handle
, Options
,
Space
, State
, and
StylePropertyNameCase
.
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-to-estree@^3
,
compatible with Node.js 16.
Security
You’re working with JavaScript. It’s not safe.
Related
estree-util-build-jsx
— transform JSX to function callshastscript
— hyperscript compatible interface for creating nodeshast-util-from-dom
— transform a DOM tree to hast
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
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: license:0
- Info: FSF or OSI recognized license: MIT License: license:0
Reason
0 existing vulnerabilities detected
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/syntax-tree/.github/security.md:1
- Info: Found linked content: github.com/syntax-tree/.github/security.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/syntax-tree/.github/security.md:1
- Info: Found text in security policy: github.com/syntax-tree/.github/security.md:1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/bb.yml:1
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/bb.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/syntax-tree/hast-util-to-estree/bb.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/syntax-tree/hast-util-to-estree/main.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/syntax-tree/hast-util-to-estree/main.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/main.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/syntax-tree/hast-util-to-estree/main.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/main.yml:15
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 2 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 1 are checked with a SAST tool
Score
4.1
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More