Find all Unist Nodes After a Specific Point
unist-util-find-all-after is a utility from the unified collective designed to locate all unist (Universal Syntax Tree) nodes that appear after a specified child node or index within a parent node. Its current stable version is 5.0.0, which requires Node.js 16 or higher and is ESM-only. The unified collective typically ties major releases to Node.js LTS cycles, providing stable maintenance. This package differentiates itself as a highly focused, lightweight utility for tree traversal, intended for integration within the broader unified ecosystem, offering a small, composable piece of functionality rather than a comprehensive tree manipulation library. It's fully typed with TypeScript and works alongside other `unist-util-*` packages like `unist-util-is` for node testing.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` an ESM-only package in a CommonJS context.fixChange `require('unist-util-find-all-after')` to `import { findAllAfter } from 'unist-util-find-all-after'` and ensure your project is configured for ES modules (e.g., by adding `"type": "module"` to `package.json` or using the `.mjs` file extension). -
TypeError: unist_util_find_all_after_1.findAllAfter is not a function
cause Incorrect import syntax for an ESM-only module that uses named exports, often encountered when TypeScript transpiles to CommonJS or due to incorrect CJS interop.fixEnsure you are using named import: `import { findAllAfter } from 'unist-util-find-all-after'`. This package does not have a default export. If transpiling to CJS, verify your transpiler correctly handles ESM named exports. -
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '.../node_modules/unist-util-find-all-after/lib' is not supported
cause Attempting to import from internal paths or specific files within the package instead of its primary entry point, which is restricted by the `export` map introduced in v5.0.0.fixAlways import from the package name directly: `import { findAllAfter } from 'unist-util-find-all-after'`. Avoid referencing internal directory structures. -
SyntaxError: Cannot use import statement outside a module
cause Using an ES `import` statement in a JavaScript file that is being interpreted as a CommonJS module.fixTo use `import` statements, your file must be part of an ES module. This can be achieved by adding `"type": "module"` to your nearest `package.json` file, or by renaming your module file to use the `.mjs` extension.
Warnings
- breaking Version 5.0.0 changed to require Node.js 16 or higher. Older Node.js versions are no longer supported, impacting runtime compatibility.
- breaking Since version 4.0.0, the package is ESM-only. CommonJS `require()` syntax is no longer supported for importing this package.
- breaking Version 5.0.0 introduced an `export` map, restricting direct imports from internal paths (e.g., `pkg/lib/index.js`).
- breaking Type definitions were significantly improved and added since v3.0.0 and updated in v5.0.0. This might cause TypeScript compilation issues if your project relies on older `@types/unist` or incompatible type definitions.
- breaking Error messages were updated in version 3.0.0. If your code relies on exact string matching for error messages, this will break your tests or error handling.
Install
-
npm install unist-util-find-all-after -
yarn add unist-util-find-all-after -
pnpm add unist-util-find-all-after
Imports
- findAllAfter
const findAllAfter = require('unist-util-find-all-after')import { findAllAfter } from 'unist-util-find-all-after'
Quickstart
import { u } from 'unist-builder';
import { findAllAfter } from 'unist-util-find-all-after';
const tree = u('tree', [
u('leaf', 'leaf 1'),
u('parent', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
u('leaf', 'leaf 4'),
u('parent', [u('leaf', 'leaf 5')]),
u('leaf', 'leaf 6'),
u('empty'),
u('leaf', 'leaf 7')
]);
// Find all 'leaf' nodes after the node at index 1 (which is a 'parent' node)
const result = findAllAfter(tree, 1, 'leaf');
console.log(JSON.stringify(result, null, 2));
/*
Expected output:
[
{
"type": "leaf",
"value": "leaf 4"
},
{
"type": "leaf",
"value": "leaf 6"
},
{
"type": "leaf",
"value": "leaf 7"
}
]
*/