hast-util-from-dom is a utility package designed to convert a DOM tree (either from a browser's native DOM or headless environments like JSDOM) into a HAST (Hypertext Abstract Syntax Tree) HTML syntax tree. It is part of the unified ecosystem for processing text. The current stable version is 5.0.1, which is ESM-only and requires Node.js 16 or newer. It typically follows the release cadence of the wider unified ecosystem. Key differentiators include its small size, primary suitability for browser environments, and its explicit choice *not* to provide positional information, which distinguishes it from more comprehensive parsing solutions. It serves as the inverse to `hast-util-to-dom` and is wrapped by `rehype-dom-parse` for HTML parsing with DOM APIs. While efficient for its purpose, users should be aware that it might yield varying results in different or older browsers.
npm install hast-util-from-domVerified import paths — ran on the pinned version, not inferred.
Demonstrates converting a DOM element (from JSDOM in Node.js) into a HAST tree.
Migrate your project to use ES modules (`import`/`export`) or use a tool like `esm.sh` for browser environments. Ensure your Node.js version is 16 or newer.
Update your `@types/hast` and other `@types/*` dependencies to their latest versions to ensure compatibility with `hast-util-from-dom@5`.
Always pass a valid DOM node (`DomNode`) to the `fromDom` function. Ensure the input is not `null` or `undefined`.
Ensure that the input DOM tree corresponds to a valid HTML document. If you need to handle non-HTML doctypes, consider older versions or alternative parsers.
If positional information is critical, consider using `rehype-parse` which typically generates full positional data.
Thoroughly test the output of `hast-util-from-dom` in all targeted browser environments, particularly if supporting legacy browsers.
Change your import statement to `import { fromDom } from 'hast-util-from-dom';`. Ensure your environment supports ES modules.Ensure your Node.js project is configured correctly for ES modules, typically by using `.mjs` files or setting `"type": "module"` in your `package.json`. If you are in a CJS context, you might need to use dynamic `import()` or adjust your module resolver configuration.
Add a check to ensure the DOM node you are passing is not `null` or `undefined` before calling `fromDom`, e.g., `const element = document.querySelector('some-selector'); if (element) { fromDom(element); }`No dependency data recorded yet.