hast-util-to-mdast is a core utility within the unified.js ecosystem designed to transform a HAST (HTML Abstract Syntax Tree) into an MDAST (Markdown Abstract Syntax Tree). This package is essential for converting HTML content into Markdown programmatically, often serving as the engine behind higher-level plugins like `rehype-remark`. The current stable version is 10.1.2. The package maintains an active release cadence, with frequent patch and minor updates addressing bug fixes and feature enhancements, alongside major versions that typically align with Node.js LTS updates or significant API refinements. Key differentiators include its tight integration with the unified ecosystem, robust handling of various HTML structures, and the provision of a flexible API with custom handlers for fine-grained control over the transformation process, allowing developers to define how specific HTML elements or nodes should be represented in Markdown.
npm install hast-util-to-mdastVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to convert an HTML string to Markdown using `hast-util-from-html`, `hast-util-to-mdast`, and `mdast-util-to-markdown`.
Ensure your project uses Node.js v16.0.0 or higher. Update your Node.js runtime if necessary.
Refactor your imports to use ESM syntax (e.g., `import { toMdast } from 'hast-util-to-mdast'`). If your project is still CommonJS, consider transpilation or upgrading your project to use ESM.If you are using custom handlers, consult the official documentation for the new API. You will need to rewrite your handler functions to use the `state` object and its methods (e.g., `state.one`, `state.all`).
Update to `hast-util-to-mdast@10.1.2` or later to ensure `br` elements are correctly preserved in phrasing content.
Change `const { toMdast } = require('hast-util-to-mdast');` to `import { toMdast } from 'hast-util-to-mdast';`. Ensure your project's `package.json` includes `"type": "module"` or uses `.mjs` files for ESM.These functions are no longer direct exports. When implementing custom handlers, access them via the `state` object passed to your handler: `state.one(node, parent)` and `state.all(node, parent)`.
Upgrade your Node.js runtime environment to version 16 or newer. Use `nvm install 16` (or higher) and `nvm use 16` if using Node Version Manager.