unist-util-visit
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
@types/unist | 3.0.2 | 2.55 kB | MIT | prod | |
unist-util-is | 6.0.0 | 6.76 kB | MIT | prod | |
unist-util-visit-parents | 6.0.1 | 8.95 kB | MIT | prod |
Unist-util-visit is a highly effective utility for working with unist. It's main functionality involves the walking of trees and exploration of nodes within a unist tree. This proves useful when you want to traverse the tree and inspect or manipulate nodes, particularly when handling markdown or other hierarchical data structures.
To use unist-util-visit, first install the package using npm with npm install unist-util-visit
. After installing, import it into your JavaScript file with import {visit} from 'unist-util-visit'
. Then use the visit
function, providing a tree, a test, and a visitor function like so:
import {fromMarkdown} from 'mdast-util-from-markdown'
import {visit} from 'unist-util-visit'
const tree = fromMarkdown('Some *emphasis*, **strong**, and `code`.')
visit(tree, 'text', function (node, index, parent) {
console.log([node.value, parent ? parent.type : index])
})
In this example, the markdown text is converted to a unist tree, then the visit
function traverses the tree and logs each 'text' node's value along with either the type of its parent node or its index.
The documentation for unist-util-visit can be found in the README file on its GitHub repository, available at git+https://github.com/syntax-tree/unist-util-visit.git
. The README includes a detailed explanation of the utility's functionality, how to use it, and the various API methods it provides. It also includes TypeScript typings and related resources.