hast-util-to-jsx-runtime is a utility within the unifiedjs ecosystem that transforms a HAST (Hypertext Abstract Syntax Tree) into a representation suitable for automatic JSX runtimes (React, Preact, Solid, Svelte, Vue, etc.). It acts as a bridge, allowing HTML-like content represented as HAST nodes to be rendered efficiently by various JSX-based frameworks without direct framework-specific code. The package is currently at version 2.3.6 and follows a release cadence driven by feature additions, bug fixes, and type improvements, typically releasing patch and minor versions regularly. Its core value proposition is enabling framework-agnostic rendering of parsed HTML, leveraging the standard automatic JSX runtime API for broad compatibility. This design choice differentiates it by providing a unified approach to content rendering across different JSX environments.
npm install hast-util-to-jsx-runtimeVerified import paths — ran on the pinned version, not inferred.
This example demonstrates converting a HAST tree to a React static HTML string, illustrating the use of `toJsxRuntime` with React's automatic JSX runtime functions.
Ensure your project is configured for ESM. Update your import statements to use `import ... from 'hast-util-to-jsx-runtime'` and ensure your `package.json` includes `"type": "module"` or files end in `.mjs`.
Add the recommended global `JSX` namespace declaration to your project's `d.ts` file, typically by importing `JSX` from `react/jsx-runtime` (or your chosen framework's runtime) and declaring it globally, as shown in the package's documentation.
After generating JSX with `toJsxRuntime`, ensure your client-side application properly mounts or hydrates the root JSX element using methods like `ReactDOM.hydrateRoot` or `ReactDOM.createRoot().render()` for React, or similar APIs for other frameworks.
Add the following to a global declaration file (e.g., `src/global.d.ts`): `import type {JSX as Jsx} from 'react/jsx-runtime'; declare global { namespace JSX { type ElementClass = Jsx.ElementClass; type Element = Jsx.Element; type IntrinsicElements = Jsx.IntrinsicElements; } }` (adjust `react/jsx-runtime` for your framework).Convert your consuming file or project to use ES Modules. Use `import { toJsxRuntime } from 'hast-util-to-jsx-runtime'` and ensure your `package.json` contains `"type": "module"` or files use the `.mjs` extension.Ensure you are importing the correct runtime functions from your framework (e.g., `import { Fragment, jsxs, jsx } from 'react/jsx-runtime'`) and correctly passing them in the `options` object: `toJsxRuntime(tree, { Fragment, jsxs, jsx })`.No dependency data recorded yet.