A robust Next.js newsletter Next.js Weekly is sponsoring me 💖

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 is a unist utility to find nodes between two nodes or indexes in a parent.
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).
This package is ESM only. In Node.js (version 16+), install with npm:
npm install unist-util-find-betweenIn 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>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'},
]This package exports the identifiers findBetween and findBetweenIncluded. There is no default export.
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(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.
parent(Node) — parent nodeindex(number) — index of child inparentchild(Node) — child inparenttest(Test) —unist-util-is-compatible test
Children of parent (Array<Node>).
This package is fully typed with TypeScript.
It exports no additional types (types for the test are in unist-util-is).
This plugin works with unified version 6+ and Node.js 16+.
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
I like to contribute the Unified / Remark / MDX ecosystem, so I recommend you to have a look my 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
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.
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.
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.
MIT License © ipikuka