{"name":"hast-util-to-estree","version":"3.1.1","keywords":["change","ecmascript","estree","hast-util","hast","html","javascript","jsx","mdx","rehype","transform","unist","utility","util"],"author":{"url":"https://wooorm.com","name":"Titus Wormer","email":"tituswormer@gmail.com"},"license":"MIT","_id":"hast-util-to-estree@3.1.1","maintainers":[{"name":"wooorm","email":"tituswormer@gmail.com"},{"name":"kmck","email":"keith@mcknig.ht"}],"contributors":[{"url":"https://wooorm.com","name":"Titus Wormer","email":"tituswormer@gmail.com"}],"homepage":"https://github.com/syntax-tree/hast-util-to-estree#readme","bugs":{"url":"https://github.com/syntax-tree/hast-util-to-estree/issues"},"xo":{"rules":{"unicorn/prefer-code-point":"off","logical-assignment-operators":"off","unicorn/prefer-string-replace-all":"off"},"prettier":true},"dist":{"shasum":"b7f0b247d9f62127bb5db34e3a86c93d17279071","tarball":"http://registry-npm.ywwl.com/hast-util-to-estree/download/hast-util-to-estree-3.1.1.tgz","fileCount":36,"integrity":"sha512-IWtwwmPskfSmma9RpzCappDUitC8t5jhAynHhc1m2+5trOgsrp7txscUSavc5Ic8PATyAjfrCK1wgtxh2cICVQ==","signatures":[{"sig":"MEUCIQC9LTVnbgYwpQdtxsqC2inm3d/rf0co+zVE+EZsCY1XqQIgGLbn/7u2O97hmRvpRTga3JdHsd2O0Kzy982jYtMJkQI=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":68700,"size":17857,"noattachment":false,"key":"/hast-util-to-estree/-/hast-util-to-estree-3.1.1.tgz"},"type":"module","types":"./index.d.ts","exports":"./index.js","funding":{"url":"https://opencollective.com/unified","type":"opencollective"},"gitHead":"c0c4bd33583abade25c4f4e248a06cb1ec8c3aff","scripts":{"test":"npm run build && npm run format && npm run test-coverage","build":"tsc --build --clean && tsc --build && type-coverage","format":"remark --frail --output --quiet -- . && prettier --log-level warn --write -- . && xo --fix","test-api":"node --conditions development test.js","test-coverage":"c8 --100 --reporter lcov -- npm run test-api"},"_npmUser":{"name":"wooorm","email":"tituswormer@gmail.com"},"prettier":{"semi":false,"useTabs":false,"tabWidth":2,"singleQuote":true,"trailingComma":"none","bracketSpacing":false},"repository":{"url":"git+https://github.com/syntax-tree/hast-util-to-estree.git","type":"git"},"_npmVersion":"11.0.0","description":"hast utility to transform to estree (JavaScript AST) JSX","directories":{},"sideEffects":false,"_nodeVersion":"23.1.0","dependencies":{"devlop":"^1.0.0","zwitch":"^2.0.0","@types/hast":"^3.0.0","@types/estree":"^1.0.0","style-to-object":"^1.0.0","@types/estree-jsx":"^1.0.0","mdast-util-mdx-jsx":"^3.0.0","unist-util-position":"^5.0.0","hast-util-whitespace":"^3.0.0","mdast-util-mdxjs-esm":"^2.0.0","property-information":"^6.0.0","comma-separated-tokens":"^2.0.0","space-separated-tokens":"^2.0.0","mdast-util-mdx-expression":"^2.0.0","estree-util-attach-comments":"^3.0.0","estree-util-is-identifier-name":"^3.0.0"},"remarkConfig":{"plugins":["remark-preset-wooorm"]},"typeCoverage":{"strict":true,"atLeast":100,"ignoreFiles":["lib/state.d.ts","lib/state.js"],"ignoreFiles#":"needed `any`"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.0.0","xo":"^0.60.0","prettier":"^3.0.0","acorn-jsx":"^5.0.0","hastscript":"^9.0.0","remark-cli":"^12.0.0","typescript":"^5.0.0","@types/node":"^22.0.0","estree-walker":"^3.0.0","type-coverage":"^2.0.0","mdast-util-mdx":"^3.0.0","unist-util-visit":"^5.0.0","estree-util-to-js":"^2.0.0","esast-util-from-js":"^2.0.0","mdast-util-to-hast":"^13.0.0","remark-preset-wooorm":"^10.0.0","mdast-util-from-markdown":"^2.0.0","micromark-extension-mdxjs":"^3.0.0"},"_npmOperationalInternal":{"tmp":"tmp/hast-util-to-estree_3.1.1_1735906682490_0.9167978332206379","host":"s3://npm-registry-packages-npm-production"},"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<script type=\"module\">\n  import {toEstree} from 'https://esm.sh/hast-util-to-estree@3?bundle'\n</script>\n```\n\n## Use\n\nSay our module `example.html` contains:\n\n```html\n<!doctype html>\n<html lang=en>\n<title>Hi!</title>\n<link rel=stylesheet href=index.css>\n<h1>Hello, world!</h1>\n<a download style=\"width:1;height:10px\"></a>\n<!--commentz-->\n<svg xmlns=\"http://www.w3.org/2000/svg\">\n  <title>SVG `&lt;ellipse&gt;` element</title>\n  <ellipse\n    cx=\"120\"\n    cy=\"70\"\n    rx=\"100\"\n    ry=\"50\"\n  />\n</svg>\n<script src=\"index.js\"></script>\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;<>\n  <html lang=\"en\">\n    <head>\n      <title>{'Hi!'}</title>\n      {'\\n'}\n      <link rel=\"stylesheet\" href=\"index.css\" />\n      {'\\n'}\n    </head>\n    <body>\n      <h1>{'Hello, world!'}</h1>\n      {'\\n'}\n      <a\n        download\n        style={{\n          width: '1',\n          height: '10px'\n        }}\n      />\n      {'\\n'}\n      {}\n      {'\\n'}\n      <svg xmlns=\"http://www.w3.org/2000/svg\">\n        {'\\n  '}\n        <title>{'SVG `<ellipse>` element'}</title>\n        {'\\n  '}\n        <ellipse cx=\"120\" cy=\"70\" rx=\"100\" ry=\"50\" />\n        {'\\n'}\n      </svg>\n      {'\\n'}\n      <script src=\"index.js\" />\n      {'\\n'}\n    </body>\n  </html>\n</>\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<string, Handle>`).\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<string, Handle>`, optional)\n  — custom handlers\n* `space` ([`Space`][api-space], default: `'html'`)\n  — which space the document is in; when an `<svg>` 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) => EstreeJsxChild | undefined`)\n  — transform children of a hast parent to estree\n* `comments` (`Array<EstreeComment>`)\n  — list of estree comments\n* `createJsxAttributeName` (`(name: string) => EstreeJsxAttributeName`)\n  — create a JSX attribute name\n* `createJsxElementName` (`(name: string) => 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<EstreeNode>`)\n  — list of top-level estree nodes\n* `handle` (`(node: HastNode) => EstreeJsxChild | undefined`)\n  — transform a hast node to estree\n* `inherit` (`(from: HastNode, to: EstreeNode) => undefined`)\n  — take positional info and data from `from` (use `patch` if you don’t want\n  data)\n* `patch` (`(from: HastNode, to: EstreeNode) => 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<!-- Definitions -->\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","publish_time":1735906682727,"_cnpm_publish_time":1735906682727,"dist-tags":{"latest":"3.1.3"}}