hast-util-to-parse5 is a utility within the unified (syntax-tree) ecosystem designed to convert a HAST (Hypertext Abstract Syntax Tree) into a parse5 AST (Abstract Syntax Tree). It is currently at version 8.0.1 and receives updates as part of the broader unified collective, with major versions typically introducing significant changes and minor/patch versions addressing features and fixes. This package differentiates itself by avoiding the use of parse5 adapters, which the developers state reduces code weight and fragility. It is primarily used when there's a specific need to generate a parse5 AST from an existing HAST tree, though its inverse, `hast-util-from-parse5`, is often more commonly employed. The library is ESM-only and requires Node.js 16 or newer since its 8.x release line. Users should be aware of potential cross-site scripting (XSS) risks if the input HAST tree is not properly sanitized, as highlighted in its security considerations.
npm install hast-util-to-parse5Verified import paths — ran on the pinned version, not inferred.
This example demonstrates how to convert a complex HAST (Hypertext Abstract Syntax Tree) to a parse5 AST using `toParse5` and specify the `space` option.
Change `toParse5(tree, 'html')` to `toParse5(tree, { space: 'html' })`.Update your Node.js environment to version 16 or newer. Alternatively, pin your `hast-util-to-parse5` dependency to `<8.0.0`.
Run `npm update @types/hast` or `yarn upgrade @types/hast`.
Migrate your project to use ES modules (`import`/`export`) or use a dynamic `import()` statement. For TypeScript, ensure `"module": "Node16"` or `"ESNext"` is set in `tsconfig.json`.
Ensure that your input `hast` tree represents an HTML document. If you need to handle non-HTML doctypes, you may need to use an older version or a different utility.
Always import from the package root: `import { toParse5 } from 'hast-util-to-parse5'`.Always sanitize or validate the input HAST tree if it originates from untrusted sources, for example, using a tool like `hast-util-sanitize`.
Change `const { toParse5 } = require('hast-util-to-parse5')` to `import { toParse5 } from 'hast-util-to-parse5'` and ensure your project is configured for ES modules (e.g., `"type": "module"` in `package.json`).Wrap the `space` option in an object: `toParse5(tree, { space: 'html' })`.No dependency data recorded yet.