1.4.3 • Published 2 months ago

unist-util-ancestor v1.4.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

unist-util-ancestor

Build Coverage Types Size

Unist utility for finding the closest common ancestor for multiple nodes. Useful for working with remark, rehype and retext.

Install

npm install unist-util-ancestor

Use

import { u } from 'unist-builder'
import { inspect } from 'unist-util-inspect'
import findAncestor from 'unist-util-ancestor'

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 nodesToFind = [{ value: 'leaf 1' }, { value: 'leaf 2' }]

console.log(inspect(findAncestor(tree, nodesToFind)))

Yields:

node[2]
├─0 node[1]
│   └─0 leaf "leaf 1"
└─1 node[2]
    ├─0 leaf "leaf 2"
    └─1 node[1]
        └─0 leaf "leaf 3"

API

findAncestor(tree, nodesToFind[, includeNodes])

Return the closest node that contains all nodesToFind along with data.depth containing the distance between the deepest node.

  • tree (Node) - Unist node to search
  • nodesToFind (Node[]) - Array of unist nodes
  • includeNodes (boolean) - Whether to include target nodes in response

Test

Run npm test to run tests.

Run npm run coverage to produce a test coverage report.

License

MIT © Goran Spasojevic

1.4.3

2 months ago

1.4.2

2 months ago

1.4.1

3 months ago

1.4.0

11 months ago

1.3.1

12 months ago

1.2.2

2 years ago

1.3.0

2 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago