{"id":13482728,"url":"https://github.com/syntax-tree/hast-util-to-estree","last_synced_at":"2025-04-05T17:05:44.880Z","repository":{"id":47731368,"uuid":"321085554","full_name":"syntax-tree/hast-util-to-estree","owner":"syntax-tree","description":"hast utility to transform to estree (JavaScript AST) JSX","archived":false,"fork":false,"pushed_at":"2025-03-03T10:37:50.000Z","size":207,"stargazers_count":20,"open_issues_count":0,"forks_count":2,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-29T23:36:12.263Z","etag":null,"topics":["babel","ecmascript","estree","hast","hast-util","html","javascript","js","jsx"],"latest_commit_sha":null,"homepage":"https://unifiedjs.com","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"unifiedjs","open_collective":"unified","thanks_dev":"u/gh/syntax-tree"}},"created_at":"2020-12-13T14:35:49.000Z","updated_at":"2025-03-03T10:37:52.000Z","dependencies_parsed_at":"2025-03-06T10:16:40.667Z","dependency_job_id":"f6ec3491-7b93-4fcc-b2d1-ff0d0021f336","html_url":"https://github.com/syntax-tree/hast-util-to-estree","commit_stats":{"total_commits":127,"total_committers":5,"mean_commits":25.4,"dds":0.03149606299212604,"last_synced_commit":"151ce7ab6ea4be0ef582641cc887ff2a2f2b0578"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-estree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-estree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-estree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Fhast-util-to-estree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/hast-util-to-estree/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247369952,"owners_count":20927928,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["babel","ecmascript","estree","hast","hast-util","html","javascript","js","jsx"],"created_at":"2024-07-31T17:01:04.971Z","updated_at":"2025-04-05T17:05:44.860Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified","https://thanks.dev/u/gh/syntax-tree"],"categories":["JavaScript"],"sub_categories":[],"readme":"# hast-util-to-estree\n\n[![Build][badge-build-image]][badge-build-url]\n[![Coverage][badge-coverage-image]][badge-coverage-url]\n[![Downloads][badge-downloads-image]][badge-downloads-url]\n[![Size][badge-size-image]][badge-size-url]\n\n[hast][github-hast] utility to transform to [estree][github-estree] (JSX).\n\n## Contents\n\n* [What is this?](#what-is-this)\n* [When should I use this?](#when-should-i-use-this)\n* [Install](#install)\n* [Use](#use)\n* [API](#api)\n  * [`toEstree(tree[, options])`](#toestreetree-options)\n  * [`defaultHandlers`](#defaulthandlers)\n  * [`ElementAttributeNameCase`](#elementattributenamecase)\n  * [`Handle`](#handle)\n  * [`Options`](#options)\n  * [`Space`](#space)\n  * [`State`](#state)\n  * [`StylePropertyNameCase`](#stylepropertynamecase)\n* [Types](#types)\n* [Compatibility](#compatibility)\n* [Security](#security)\n* [Related](#related)\n* [Contribute](#contribute)\n* [License](#license)\n\n## What is this?\n\nThis package is a utility that takes a\n[hast][github-hast] (HTML)\nsyntax tree as input and turns it into an\n[estree][github-estree] (JavaScript)\nsyntax tree (with a JSX extension).\nThis package also supports embedded MDX nodes.\n\n## When should I use this?\n\nThis project is useful when you want to embed HTML as JSX inside JS while\nworking with syntax trees.\nThis is used in [MDX][mdxjs].\n\n## Install\n\nThis package is [ESM only][github-gist-esm].\nIn Node.js (version 16+), install with [npm][npmjs-install]:\n\n```sh\nnpm install hast-util-to-estree\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {toEstree} from 'https://esm.sh/hast-util-to-estree@3'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {toEstree} from 'https://esm.sh/hast-util-to-estree@3?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\nSay our module `example.html` contains:\n\n```html\n\u003c!doctype html\u003e\n\u003chtml lang=en\u003e\n\u003ctitle\u003eHi!\u003c/title\u003e\n\u003clink rel=stylesheet href=index.css\u003e\n\u003ch1\u003eHello, world!\u003c/h1\u003e\n\u003ca download style=\"width:1;height:10px\"\u003e\u003c/a\u003e\n\u003c!--commentz--\u003e\n\u003csvg xmlns=\"http://www.w3.org/2000/svg\"\u003e\n  \u003ctitle\u003eSVG `\u0026lt;ellipse\u0026gt;` element\u003c/title\u003e\n  \u003cellipse\n    cx=\"120\"\n    cy=\"70\"\n    rx=\"100\"\n    ry=\"50\"\n  /\u003e\n\u003c/svg\u003e\n\u003cscript src=\"index.js\"\u003e\u003c/script\u003e\n```\n\n…and our module `example.js` looks as follows:\n\n```js\nimport fs from 'node:fs/promises'\nimport {jsx, toJs} from 'estree-util-to-js'\nimport {fromHtml} from 'hast-util-from-html'\nimport {toEstree} from 'hast-util-to-estree'\n\nconst hast = fromHtml(await fs.readFile('example.html'))\n\nconst estree = toEstree(hast)\n\nconsole.log(toJs(estree, {handlers: jsx}).value)\n```\n\n…now running `node example.js` (and prettier) yields:\n\n```js\n/* Commentz */\n;\u003c\u003e\n  \u003chtml lang=\"en\"\u003e\n    \u003chead\u003e\n      \u003ctitle\u003e{'Hi!'}\u003c/title\u003e\n      {'\\n'}\n      \u003clink rel=\"stylesheet\" href=\"index.css\" /\u003e\n      {'\\n'}\n    \u003c/head\u003e\n    \u003cbody\u003e\n      \u003ch1\u003e{'Hello, world!'}\u003c/h1\u003e\n      {'\\n'}\n      \u003ca\n        download\n        style={{\n          width: '1',\n          height: '10px'\n        }}\n      /\u003e\n      {'\\n'}\n      {}\n      {'\\n'}\n      \u003csvg xmlns=\"http://www.w3.org/2000/svg\"\u003e\n        {'\\n  '}\n        \u003ctitle\u003e{'SVG `\u003cellipse\u003e` element'}\u003c/title\u003e\n        {'\\n  '}\n        \u003cellipse cx=\"120\" cy=\"70\" rx=\"100\" ry=\"50\" /\u003e\n        {'\\n'}\n      \u003c/svg\u003e\n      {'\\n'}\n      \u003cscript src=\"index.js\" /\u003e\n      {'\\n'}\n    \u003c/body\u003e\n  \u003c/html\u003e\n\u003c/\u003e\n```\n\n## API\n\nThis package exports the identifiers [`defaultHandlers`][api-default-handlers]\nand [`toEstree`][api-to-estree].\nThere is no default export.\n\n### `toEstree(tree[, options])`\n\nTransform a hast tree (with embedded MDX nodes) into an estree (with JSX\nnodes).\n\n##### Notes\n\n###### Comments\n\nComments are attached to the tree in their neighbouring nodes (`recast`,\n`babel` style) and also added as a `comments` array on the program node\n(`espree` style).\nYou may have to do `program.comments = undefined` for certain compilers.\n\n###### Frameworks\n\nThere are differences between what JSX frameworks accept, such as whether they\naccept `class` or `className`, or `background-color` or `backgroundColor`.\n\nFor JSX components written in MDX, the author has to be aware of this\ndifference and write code accordingly.\nFor hast elements transformed by this project, this will be handled through\noptions.\n\n| Framework | `elementAttributeNameCase` | `stylePropertyNameCase` |\n| --------- | -------------------------- | ----------------------- |\n| Preact    | `'html'`                   | `'dom'`                 |\n| React     | `'react'`                  | `'dom'`                 |\n| Solid     | `'html'`                   | `'css'`                 |\n| Vue       | `'html'`                   | `'dom'`                 |\n\n###### Parameters\n\n* `tree` ([`HastNode`][github-hast-nodes])\n  — hast tree\n* `options` ([`Options`][api-options], optional)\n  — configuration\n\n###### Returns\n\nestree program node ([`Program`][github-estree-program]).\n\nThe program’s last child in `body` is most likely an `ExpressionStatement`,\nwhose expression is a `JSXFragment` or a `JSXElement`.\n\nTypically, there is only one node in `body`, however, this utility also supports\nembedded MDX nodes in the HTML\n(when [`mdast-util-mdx`][github-mdast-util-mdx] is used with mdast to parse\nmarkdown before passing its nodes through to hast).\nWhen MDX ESM import/exports are used, those nodes are added before the fragment\nor element in body.\n\nThere aren’t many great estree serializers out there that support JSX.\nTo do that, you can use [`estree-util-to-js`][github-estree-util-to-js].\nOr, use [`estree-util-build-jsx`][github-estree-util-build-jsx]\nto turn JSX into function calls,\nand then serialize with whatever (`astring`, `escodegen`).\n\n### `defaultHandlers`\n\nDefault handlers for elements (`Record\u003cstring, Handle\u003e`).\n\nEach key is a node type, each value is a [`Handle`][api-handle].\n\n### `ElementAttributeNameCase`\n\nSpecify casing to use for attribute names (TypeScript type).\n\nHTML casing is for example `class`, `stroke-linecap`, `xml:lang`.\nReact casing is for example `className`, `strokeLinecap`, `xmlLang`.\n\n###### Type\n\n```ts\ntype ElementAttributeNameCase = 'html' | 'react'\n```\n\n### `Handle`\n\nTurn a hast node into an estree node (TypeScript type).\n\n###### Parameters\n\n* `node` ([`HastNode`][github-hast-nodes])\n  — expected hast node\n* `state` ([`State`][api-state])\n  — info passed around about the current state\n\n###### Returns\n\nJSX child (`JsxChild`, optional).\n\nYou can also add more results to `state.esm` and `state.comments`.\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n###### Fields\n\n* `elementAttributeNameCase`\n  ([`ElementAttributeNameCase`][api-element-attribute-name-case], default:\n  `'react'`)\n  — specify casing to use for attribute names; this casing is used for hast\n  elements, not for embedded MDX JSX nodes (components that someone authored\n  manually)\n* `handlers` (`Record\u003cstring, Handle\u003e`, optional)\n  — custom handlers\n* `space` ([`Space`][api-space], default: `'html'`)\n  — which space the document is in; when an `\u003csvg\u003e` element is found in the\n  HTML space, this package already automatically switches to and from the SVG\n  space when entering and exiting it\n* `stylePropertyNameCase`\n  ([`StylePropertyNameCase`][api-style-property-name-case],\n  default: `'dom'`)\n  — specify casing to use for property names in `style` objects; this casing\n  is used for hast elements, not for embedded MDX JSX nodes (components that\n  someone authored manually)\n* `tableCellAlignToStyle` (`boolean`, default: `true`)\n  — turn obsolete `align` props on `td` and `th` into CSS `style` props\n\n### `Space`\n\nNamespace (TypeScript type).\n\n###### Type\n\n```ts\ntype Space = 'html' | 'svg'\n```\n\n### `State`\n\nInfo passed around about the current state (TypeScript type).\n\n###### Fields\n\n* `all` (`(node: HastParent) =\u003e EstreeJsxChild | undefined`)\n  — transform children of a hast parent to estree\n* `comments` (`Array\u003cEstreeComment\u003e`)\n  — list of estree comments\n* `createJsxAttributeName` (`(name: string) =\u003e EstreeJsxAttributeName`)\n  — create a JSX attribute name\n* `createJsxElementName` (`(name: string) =\u003e EstreeJsxElementName`)\n  — create a JSX attribute name\n* `elementAttributeNameCase`\n  ([`ElementAttributeNameCase`][api-element-attribute-name-case])\n  — casing to use for attribute names\n* `esm` (`Array\u003cEstreeNode\u003e`)\n  — list of top-level estree nodes\n* `handle` (`(node: HastNode) =\u003e EstreeJsxChild | undefined`)\n  — transform a hast node to estree\n* `inherit` (`(from: HastNode, to: EstreeNode) =\u003e undefined`)\n  — take positional info and data from `from` (use `patch` if you don’t want\n  data)\n* `patch` (`(from: HastNode, to: EstreeNode) =\u003e undefined`)\n  — take positional info from `from` (use `inherit` if you also want data)\n* `schema` ([`Schema`][github-property-information-schema])\n  — current schema\n* `stylePropertyNameCase`\n  ([`StylePropertyNameCase`][api-style-property-name-case])\n  — casing for property names in `style` objects\n* `tableCellAlignToStyle` (`boolean`)\n  — turn obsolete `align` props on `td` and `th` into CSS `style` props\n\n### `StylePropertyNameCase`\n\nCasing to use for property names in `style` objects (TypeScript type).\n\nCSS casing is for example `background-color` and `-webkit-line-clamp`.\nDOM casing is for example `backgroundColor` and `WebkitLineClamp`.\n\n###### Type\n\n```ts\ntype StylePropertyNameCase = 'css' | 'dom'\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional types\n[`ElementAttributeNameCase`][api-element-attribute-name-case],\n[`Handle`][api-handle], [`Options`][api-options],\n[`Space`][api-space], [`State`][api-state], and\n[`StylePropertyNameCase`][api-style-property-name-case].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `hast-util-to-estree@^3`,\ncompatible with Node.js 16.\n\n## Security\n\nYou’re working with JavaScript.\nIt’s not safe.\n\n## Related\n\n* [`estree-util-build-jsx`][github-estree-util-build-jsx]\n  — transform JSX to function calls\n* [`hastscript`][github-hastscript]\n  — hyperscript compatible interface for creating nodes\n* [`hast-util-from-dom`](https://github.com/syntax-tree/hast-util-from-dom)\n  — transform a DOM tree to hast\n\n## Contribute\n\nSee [`contributing.md`][health-contributing] in [`syntax-tree/.github`][health]\nfor ways to get started.\nSee [`support.md`][health-support] for ways to get help.\n\nThis project has a [code of conduct][health-coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][file-license] © [Titus Wormer][wooorm]\n\n\u003c!-- Definitions --\u003e\n\n[api-default-handlers]: #defaulthandlers\n\n[api-element-attribute-name-case]: #elementattributenamecase\n\n[api-handle]: #handle\n\n[api-options]: #options\n\n[api-space]: #space\n\n[api-state]: #state\n\n[api-style-property-name-case]: #stylepropertynamecase\n\n[api-to-estree]: #toestreetree-options\n\n[badge-build-image]: https://github.com/syntax-tree/hast-util-to-estree/workflows/main/badge.svg\n\n[badge-build-url]: https://github.com/syntax-tree/hast-util-to-estree/actions\n\n[badge-coverage-image]: https://img.shields.io/codecov/c/github/syntax-tree/hast-util-to-estree.svg\n\n[badge-coverage-url]: https://codecov.io/github/syntax-tree/hast-util-to-estree\n\n[badge-downloads-image]: https://img.shields.io/npm/dm/hast-util-to-estree.svg\n\n[badge-downloads-url]: https://www.npmjs.com/package/hast-util-to-estree\n\n[badge-size-image]: https://img.shields.io/bundlejs/size/hast-util-to-estree\n\n[badge-size-url]: https://bundlejs.com/?q=hast-util-to-estree\n\n[esmsh]: https://esm.sh\n\n[file-license]: license\n\n[github-estree]: https://github.com/estree/estree\n\n[github-estree-program]: https://github.com/estree/estree/blob/master/es5.md#programs\n\n[github-estree-util-build-jsx]: https://github.com/wooorm/estree-util-build-jsx\n\n[github-estree-util-to-js]: https://github.com/syntax-tree/estree-util-to-js\n\n[github-gist-esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[github-hast]: https://github.com/syntax-tree/hast\n\n[github-hast-nodes]: https://github.com/syntax-tree/hast#nodes\n\n[github-hastscript]: https://github.com/syntax-tree/hastscript\n\n[github-mdast-util-mdx]: https://github.com/syntax-tree/mdast-util-mdx\n\n[github-property-information-schema]: https://github.com/wooorm/property-information#api\n\n[health]: https://github.com/syntax-tree/.github\n\n[health-coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[health-contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[health-support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[mdxjs]: https://mdxjs.com\n\n[npmjs-install]: https://docs.npmjs.com/cli/install\n\n[typescript]: https://www.typescriptlang.org\n\n[wooorm]: https://wooorm.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-to-estree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Fhast-util-to-estree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Fhast-util-to-estree/lists"}