# hast-util-to-estree
[![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 to [estree][] (JSX).
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`toEstree(tree[, options])`](#toestreetree-options)
* [`defaultHandlers`](#defaulthandlers)
* [`ElementAttributeNameCase`](#elementattributenamecase)
* [`Handle`](#handle)
* [`Options`](#options)
* [`Space`](#space)
* [`State`](#state)
* [`StylePropertyNameCase`](#stylepropertynamecase)
* [Types](#types)
* [Compatibility](#compatibility)
* [Security](#security)
* [Related](#related)
* [Contribute](#contribute)
* [License](#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][esm].
In Node.js (version 14.14+ and 16.0+), install with [npm][]:
```sh
npm install hast-util-to-estree
```
In Deno with [`esm.sh`][esmsh]:
```js
import {toEstree} from 'https://esm.sh/hast-util-to-estree@2'
```
In browsers with [`esm.sh`][esmsh]:
```html
```
## Use
Say our module `example.html` contains:
```html
{'\n'}
{'\n'}
{}
{'\n'}
{'\n'}
{'\n'}
>
```
## API
This package exports the identifiers [`defaultHandlers`][api-default-handlers]
and [`toEstree`][api-to-estree].
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
* `tree` ([`HastNode`][hast-node])
— hast tree
* `options` ([`Options`][api-options], optional)
— configuration
###### Returns
estree program node ([`Program`][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`][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`][estree-util-to-js].
Or, use [`estree-util-build-jsx`][build-jsx] to turn JSX into function
calls, and then serialize with whatever (`astring`, `escodegen`).
### `defaultHandlers`
Default handlers for elements (`Record`).
Each key is a node type, each value is a [`Handle`][api-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
```ts
type ElementAttributeNameCase = 'html' | 'react'
```
### `Handle`
Turn a hast node into an estree node (TypeScript type).
###### Parameters
* `node` ([`HastNode`][hast-node])
— expected hast node
* `state` ([`State`][api-state])
— info passed around about the current state
###### Returns
JSX child (`JsxChild`, optional).
You can also add more results to `state.esm` and `state.comments`.
### `Options`
Configuration (TypeScript type).
###### Fields
* `elementAttributeNameCase`
([`ElementAttributeNameCase`][api-element-attribute-name-case], 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`, optional)
— custom handlers
* `space` ([`Space`][api-space], default: `'html'`).
which space the document is in; when an `