hast-util-to-nlcst is a utility package within the unified/syntax-tree ecosystem designed to transform a HAST (HTML Abstract Syntax Tree) into an NLCST (Natural Language Concrete Syntax Tree). This transformation extracts the natural language content from an HTML structure, making it suitable for natural language processing tasks such as linting, sentiment analysis, or spell checking with tools like retext. The package is currently stable at version 4.0.0 and follows a semver release cadence, with major versions often introducing breaking changes related to environment support (e.g., Node.js versions, ESM-only) or parser API updates. A key differentiator is its focused role in bridging HTML content to natural language processing within the unist AST family, though it currently lacks a mechanism to apply changes back from NLCST to HAST. It is often used in conjunction with parsers like `parse-english` and wrappers like `rehype-retext`.
npm install hast-util-to-nlcstVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to parse an HTML string into a HAST tree, then convert that HAST tree into an NLCST tree using `hast-util-to-nlcst` with `ParseEnglish`. It then uses `unist-util-inspect` to log the resulting natural language tree structure, showing how text content from HTML elements is represented.
Upgrade your Node.js environment to version 16 or newer.
Ensure your build tools and Node.js version fully support the `exports` field. If experiencing issues, verify your `tsconfig.json` (for TypeScript) or bundler configuration is up-to-date.
Update the NLCST parser packages you are using (e.g., `npm install parse-english@latest`) and ensure your code passes their latest constructor function.
Refactor your code to use ES module `import` statements. Ensure your project is configured for ESM, potentially by adding `"type": "module"` to your `package.json` or using `.mjs` file extensions.
When creating your HAST tree, ensure that the parser used (e.g., `hast-util-from-html`, `rehype-parse`) is configured to retain positional information. For example, `fromHtml` automatically handles this when given a `VFile`.
Change `const { toNlcst } = require('hast-util-to-nlcst')` to `import { toNlcst } from 'hast-util-to-nlcst'`. Ensure your project runs in an ESM context (e.g., `type: "module"` in `package.json` or `.mjs` file extension).Ensure your Node.js version is 16+ (required by v4.0.0), and add `"type": "module"` to your `package.json` file. Alternatively, rename your file to use the `.mjs` extension.
Update your NLCST parser package (e.g., `npm install parse-english@latest`). Ensure you are passing the constructor function itself (e.g., `ParseEnglish`) and not an instance or an incorrect export.
Ensure the utility or parser you use to create the HAST tree preserves positional data. For instance, when using `hast-util-from-html`, ensure you pass a `VFile` object created from actual content, which typically retains this info.