unist-util-source is a focused utility in the unified ecosystem designed to extract the original source code corresponding to a `unist` node or a specific position within a `VFile`. It is currently stable at version 5.0.0 and follows the unified collective's release cadence, which typically aligns major versions with Node.js LTS releases. Key differentiators include its tight integration with the `unist` specification and `vfile` for reliable positional data, its minimal API surface, and its commitment to modern JavaScript practices like being ESM-only and fully typed with TypeScript. It serves as a foundational building block for tools that need to represent parts of a parsed document back in their original textual form, such as linters, code formatters, or rich text editors displaying snippets.
npm install unist-util-sourceVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to read a Markdown string, parse it into a unist tree using `mdast-util-from-markdown`, and then extract the source code for a specific `strong` node using `unist-util-source`.
Upgrade your Node.js environment to version 16 or higher. If unable to upgrade, use `unist-util-source@^4`.
Update all calls to `source` to pass the `VFile` instance as the first argument and the `Node` or `Position` as the second: `source(file, node)`.
Update consumers of `source`'s return value to explicitly handle `undefined` checks. For example, `const text = source(file, node) ?? ''` if an empty string is desired for missing source.
Migrate your project to use ES modules (`import`). Ensure your `package.json` specifies `"type": "module"` or use `.mjs` file extensions. If sticking to CommonJS, you must use `unist-util-source@^3`.
Always use named imports: `import { source } from 'unist-util-source'`.Switch to ES module `import { source } from 'unist-util-source'` and ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`).Use a named import: `import { source } from 'unist-util-source'`.Ensure `source` is called with the `VFile` instance as the first argument and the `Node` or `Position` as the second: `source(file, node)`.