# 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 Hi!

Hello, world!

SVG `<ellipse>` element ``` …and our module `example.js` looks as follows: ```js import fs from 'node:fs/promises' import {fromHtml} from 'hast-util-from-html' import {toEstree} from 'hast-util-to-estree' import {toJs, jsx} from 'estree-util-to-js' const hast = fromHtml(await fs.readFile('example.html')) const estree = toEstree(hast) console.log(toJs(estree, {handlers: jsx}).value) ``` …now running `node example.js` (and prettier) yields: ```jsx /* Commentz */ ;<> {'Hi!'} {'\n'} {'\n'}

{'Hello, world!'}

{'\n'} {'\n'} {} {'\n'} {'\n '} {'SVG `` element'} {'\n '} {'\n'} {'\n'}