unist-util-find-between
April 18, 2026 ยท View on GitHub
A robust Next.js newsletter Next.js Weekly is sponsoring me ๐

Become a sponsor ๐
If you find unist-util-find-between useful in your projects, consider supporting my work.
Your sponsorship means a lot ๐
My sponsors are going to be featured here and on my sponsor wall.
A warm thanks ๐ to @ErfanEbrahimnia, @recepkyk, and @LSeaburg for the support!
Thank you for supporting open source! ๐
unist-util-find-between
unist-util-find-between is a unist utility to find nodes between two nodes or indexes in a parent.
When should I use this?
You may need to use unist-util-find-between when you develop a unified plugin or unist utility while inspecting or traversing an abstract syntax tree (AST).
Install
This package is ESM only. In Node.js (version 16+), install with npm:
npm install unist-util-find-between
In Deno with esm.sh:
import {findBetween} from 'https://esm.sh/unist-util-find-between@1'
In browsers with esm.sh:
<script type="module">
import {findBetween} from 'https://esm.sh/unist-util-find-between@1?bundle'
</script>
Use
import {u} from 'unist-builder'
import {findBetween, findBetweenIncluded} from 'unist-util-find-between'
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')
])
console.log(findBetween(tree, 1, 4, 'leaf'))
Yields:
[
{type: 'leaf', value: 'leaf 4'},
]
console.log(findBetweenIncluded(tree, 1, 4, 'leaf'))
Yields:
[
{type: 'leaf', value: 'leaf 4'},
{type: 'leaf', value: 'leaf 6'},
]
API
This package exports the identifiers findBetween and findBetweenIncluded. There is no default export.
findBetween
findBetween(parent, child|index, child|index[, test])
Finds nodes in a parent between two childs or indexes, that pass test.
Starting and ending nodes or indexes are excluded. Use findBetweenIncluded for including starting and ending nodes or indexes.
findBetweenIncluded
findBetweenIncluded(parent, child|index, child|index[, test])
Finds nodes in a parent between two childs or indexes, that pass test.
Starting and ending nodes or indexes are included. Use findBetween for excluding starting and ending nodes or indexes.
Parameters
parent(Node) โ parent nodeindex(number) โ index of child inparentchild(Node) โ child inparenttest(Test) โunist-util-is-compatible test
Returns
Children of parent (Array<Node>).
Types
This package is fully typed with TypeScript.
It exports no additional types (types for the test are in unist-util-is).
Compatibility
This plugin works with unified version 6+ and Node.js 16+.
Related
unist-util-visitโ walk the treeunist-util-visit-parentsโ walk the tree with a stack of parentsunist-util-filterโ create a new tree with all nodes that pass a testunist-util-mapโ create a new tree with all nodes mapped by a given functionunist-util-flatmapโ create a new tree by mapping (to an array) with the given functionunist-util-find-afterโ find a node after another nodeunist-util-find-beforeโ find a node before another nodeunist-util-find-all-afterโ find all nodes after another nodeunist-util-find-all-beforeโ find all nodes before another nodeunist-util-removeโ remove nodes from a tree that pass a testunist-util-selectโ select nodes with CSS-like selectors
My Plugins
I like to contribute the Unified / Remark / MDX ecosystem, so I recommend you to have a look my plugins.
My Remark Plugins
remark-flexible-code-titlesโ Remark plugin to add titles or/and containers for the code blocks with customizable propertiesremark-flexible-containersโ Remark plugin to add custom containers with customizable properties in markdownremark-insโ Remark plugin to addinselement in markdownremark-flexible-paragraphsโ Remark plugin to add custom paragraphs with customizable properties in markdownremark-flexible-markersโ Remark plugin to add custommarkelement with customizable properties in markdownremark-flexible-tocโ Remark plugin to expose the table of contents viavfile.dataor via an option referenceremark-mdx-remove-esmโ Remark plugin to remove import and/or export statements (mdxjsEsm)remark-mdx-remove-expressionsโ Remark plugin to remove MDX expressions within curlybraces {} in MDX content
My Rehype Plugins
rehype-pre-languageโ Rehype plugin to add language information as a property topreelementrehype-highlight-code-linesโ Rehype plugin to add line numbers to code blocks and allow highlighting of desired code linesrehype-code-metaโ Rehype plugin to copycode.data.metatocode.properties.metastringrehype-image-toolkitโ Rehype plugin to enhance Markdown image syntax![]()and Markdown/MDX media elements (<img>,<audio>,<video>) by auto-linking bracketed or parenthesized image URLs, wrapping them in<figure>with optional captions, unwrapping images/videos/audio from paragraph, parsing directives in title for styling and adding attributes, and dynamically converting images into<video>or<audio>elements based on file extension.
My Recma Plugins
recma-mdx-escape-missing-componentsโ Recma plugin to set the default value() => nullfor the Components in MDX in case of missing or not provided so as not to throw an errorrecma-mdx-change-propsโ Recma plugin to change thepropsparameter into the_propsin thefunction _createMdxContent(props) {/* */}in the compiled source in order to be able to use{props.foo}like expressions. It is useful for thenext-mdx-remoteornext-mdx-remote-clientusers innextjsapplications.recma-mdx-change-importsโ Recma plugin to convert import declarations for assets and media with relative links into variable declarations with string URLs, enabling direct asset URL resolution in compiled MDX.recma-mdx-import-mediaโ Recma plugin to turn media relative paths into import declarations for both markdown and html syntax in MDX.recma-mdx-import-reactโ Recma plugin to ensure gettingReactinstance from the arguments and to make the runtime props{React, jsx, jsxs, jsxDev, Fragment}is available in the dynamically imported components in the compiled source of MDX.recma-mdx-html-overrideโ Recma plugin to allow selected raw HTML elements to be overridden via MDX components.recma-mdx-interpolateโ Recma plugin to enable interpolation of identifiers wrapped in curly braces within thealt,src,href, andtitleattributes of markdown link and image syntax in MDX.
My Unist Utils and Unified Plugins
I also build low-level utilities and plugins for the Unified ecosystem that can be used across Remark, Rehype, Recma, and other unist-based abstract syntax trees (ASTs).
unist-util-find-betweenโ Unist utility to find the nodes between two nodes.unified-log-treeโ Unified plugin to log abstract syntax trees (ASTs) for debugging without mutating.
License
MIT License ยฉ ipikuka