unist-util-distance
v1.1.1
Published
Unist utility for finding the distance between nodes
Maintainers
Readme
unist-util-distance
Unist utility for finding the distance between nodes. Useful for working with remark, rehype and retext.
Install
npm install unist-util-distanceUse
import { u } from 'unist-builder'
import { find } from 'unist-util-find'
import { findDistance } from 'unist-util-distance'
const tree =
u('root', [
u('node', [
u('leaf', 'leaf 0')
]),
u('node', [
u('node', [
u('leaf', 'leaf 1'),
]),
u('node', [
u('leaf', 'leaf 2'),
u('node', [
u('leaf', 'leaf 3'),
])
])
]),
u('leaf', 'leaf 4')
])
const nodeA = find(tree, { value: 'leaf 0' })
const nodeB = find(tree, { value: 'leaf 1' })
console.log(findDistance(tree, nodeA, nodeB))Yields:
5API
findDistance(tree, nodeA, nodeB)
Return the distance between two nodes.
Returns a number representing the distance (number of edges) between the nodes. Returns 0 if nodes are the same or one is an ancestor of the other.
Test
Run npm test to run tests.
Run npm run coverage to produce a test coverage report.
