hast-util-from-string is a utility package within the unified and rehype ecosystem designed to efficiently set the plain-text value of a HAST (Hypertext Abstract Syntax Tree) node. It mimics the behavior of the DOM's `Node#textContent` setter, effectively replacing all children of a node with a single text node containing the provided string value. The current stable version is 3.0.1. As part of the unified collective, it follows a coordinated release cadence, often aligning major version bumps across related packages, though individual utilities receive minor and patch updates as needed. A key differentiator is its `textContent`-like behavior, contrasting with `hast-util-from-text` which emulates `innerText` and considers rendering specifics like line breaks and table cell separation. The library is ESM-only and fully typed with TypeScript.
npm install hast-util-from-stringVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to initialize an empty HAST node and how to overwrite an existing node's children with a new plain-text string using `fromString`.
Migrate your project to use ES modules (`import`/`export`) and ensure you are running Node.js 16 or later. Update your `tsconfig.json` and `package.json` to reflect ESM usage.
Ensure you keep a reference to the node passed to `fromString`. Do not rely on the function's return value. Example: `const myNode = h('p'); fromString(myNode, 'text'); console.log(myNode);`Always use `rehype-sanitize` when processing untrusted HTML content in your rehype pipelines to strip out potentially malicious scripts or attributes.
Change your import statement from `const { fromString } = require('hast-util-from-string')` to `import { fromString } from 'hast-util-from-string'`.Modify your code to retain a reference to the HAST node passed into `fromString`. The function modifies the node in place. For example, `const node = h('p'); fromString(node, 'hello'); // node is now modified`.No dependency data recorded yet.