{"id":11020,"library":"hast-util-to-parse5","title":"hast-util-to-parse5","description":"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.","status":"active","version":"8.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/syntax-tree/hast-util-to-parse5","tags":["javascript","unist","hast","hast-util","util","utility","html","parse5","ast","typescript"],"install":[{"cmd":"npm install hast-util-to-parse5","lang":"bash","label":"npm"},{"cmd":"yarn add hast-util-to-parse5","lang":"bash","label":"yarn"},{"cmd":"pnpm add hast-util-to-parse5","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package is ESM-only since v7.0.0. Use `import` statements.","wrong":"const toParse5 = require('hast-util-to-parse5')","symbol":"toParse5","correct":"import { toParse5 } from 'hast-util-to-parse5'"},{"note":"This symbol is a TypeScript type, not a runtime value. Use `import type`.","wrong":"import { Options } from 'hast-util-to-parse5'","symbol":"Options","correct":"import type { Options } from 'hast-util-to-parse5'"},{"note":"This symbol is a TypeScript type, not a runtime value. Use `import type`.","wrong":"import { Space } from 'hast-util-to-parse5'","symbol":"Space","correct":"import type { Space } from 'hast-util-to-parse5'"}],"quickstart":{"code":"import { toParse5 } from 'hast-util-to-parse5';\nimport type { Element, Text, Root } from 'hast';\n\nconst hastTree: Root = {\n  type: 'root',\n  children: [\n    {\n      type: 'element',\n      tagName: 'html',\n      properties: { lang: 'en' },\n      children: [\n        {\n          type: 'element',\n          tagName: 'head',\n          properties: {},\n          children: [\n            { type: 'element', tagName: 'title', properties: {}, children: [{ type: 'text', value: 'Hello' }] as Text[] }\n          ]\n        } as Element,\n        {\n          type: 'element',\n          tagName: 'body',\n          properties: {},\n          children: [\n            { type: 'element', tagName: 'h1', properties: {}, children: [{ type: 'text', value: 'World!' }] as Text[] },\n            { type: 'element', tagName: 'p', properties: {}, children: [{ type: 'text', value: 'This is a paragraph.' }] as Text[] }\n          ]\n        } as Element\n      ]\n    } as Element\n  ]\n};\n\n// Convert the hast tree to a parse5 AST\nconst parse5Tree = toParse5(hastTree, { space: 'html' });\n\nconsole.log(JSON.stringify(parse5Tree, null, 2));\n\n/* Example of what the output parse5Tree might look like (truncated):\n{\n  \"nodeName\": \"html\",\n  \"tagName\": \"html\",\n  \"attrs\": [\n    {\n      \"name\": \"lang\",\n      \"value\": \"en\"\n    }\n  ],\n  \"namespaceURI\": \"http://www.w3.org/1999/xhtml\",\n  \"childNodes\": [\n    {\n      \"nodeName\": \"head\",\n      \"tagName\": \"head\",\n      // ... more child nodes ...\n    },\n    {\n      \"nodeName\": \"body\",\n      \"tagName\": \"body\",\n      // ... more child nodes ...\n    }\n  ]\n}\n*/","lang":"typescript","description":"This example demonstrates how to convert a complex HAST (Hypertext Abstract Syntax Tree) to a parse5 AST using `toParse5` and specify the `space` option."},"warnings":[{"fix":"Change `toParse5(tree, 'html')` to `toParse5(tree, { space: 'html' })`.","message":"Version 8.0.0 removed direct support for passing the `space` option as a string. It must now be passed within an `options` object as `{ space: 'html' }` or `{ space: 'svg' }`.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Update your Node.js environment to version 16 or newer. Alternatively, pin your `hast-util-to-parse5` dependency to `<8.0.0`.","message":"Version 8.0.0 changed to require Node.js 16 or later. Older Node.js versions are no longer supported.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Run `npm update @types/hast` or `yarn upgrade @types/hast`.","message":"Version 8.0.0 updated its internal `@types/hast` dependency. If your project uses `@types/hast` directly, ensure you also update it to maintain compatibility.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"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`.","message":"Version 7.0.0 switched to being an ESM-only package. CommonJS `require()` is no longer supported.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"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.","message":"Version 7.0.0 removed support for transforming non-HTML doctypes.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Always import from the package root: `import { toParse5 } from 'hast-util-to-parse5'`.","message":"The package uses the `exports` field in its `package.json`. Directly accessing internal paths (e.g., `hast-util-to-parse5/lib/index.js`) is not supported and may break in future versions.","severity":"gotcha","affected_versions":">=8.0.0"},{"fix":"Always sanitize or validate the input HAST tree if it originates from untrusted sources, for example, using a tool like `hast-util-sanitize`.","message":"Using `hast-util-to-parse5` with an untrusted HAST tree can lead to a cross-site scripting (XSS) attack if the resulting parse5 AST is later serialized and rendered without proper sanitization.","severity":"gotcha","affected_versions":">=5.1.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"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`).","cause":"Attempting to use `require()` to import `hast-util-to-parse5` in a CommonJS environment, but the package is ESM-only.","error":"ERR_REQUIRE_ESM"},{"fix":"Wrap the `space` option in an object: `toParse5(tree, { space: 'html' })`.","cause":"Passing the `space` option directly as a string to `toParse5` after version 8.0.0.","error":"Argument of type 'string' is not assignable to parameter of type 'Options | undefined'."}],"ecosystem":"npm"}