Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork1
utility to index property values or computed keys to nodes
License
syntax-tree/unist-util-index
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
unist utility to create an index from certain nodes.
This utility creates a mutable index data structure, that maps property valuesor computed keys, to nodes.For example, you can use this to index all (footnote) definitions in a tree,or all headings of a certain rank, to later retrieve them without having to walkthe tree each time.
This is a utility that helps you deal with indexing the tree.It’s pretty small, and you can definitely do it yourself, but this littlewrapper makes it all a bit easier.
This package isESM only.In Node.js (version 16+), install withnpm:
npm install unist-util-index
In Deno withesm.sh:
import{Index}from'https://esm.sh/unist-util-index@4'
In browsers withesm.sh:
<scripttype="module">import{Index}from'https://esm.sh/unist-util-index@4?bundle'</script>
importfsfrom'node:fs/promises'import{fromMarkdown}from'mdast-util-from-markdown'import{toString}from'mdast-util-to-string'import{Index}from'unist-util-index'// Parse and read this repo’s readme:consttree=fromMarkdown(awaitfs.readFile('readme.md'))// Index on heading depth:constindexOnDepth=newIndex('depth',tree,'heading')console.log(indexOnDepth.get(2).map(function(d){returntoString(d)}))// Index on definition identifier:constindexOnIdentifier=newIndex('identifier',tree,'definition')console.log(indexOnIdentifier.get('unist').map(function(node){returnnode.url}))
Yields:
['Contents','What is this?','When should I use this?','Install','Use','API','Types','Compatibility','Related','Contribute','License']['https://github.com/syntax-tree/unist']
This package exports the identifierIndex.There is no default export.
Create a mutable index data structure, that maps property values or computedkeys, to nodes.
Iftree is given, the index is initialized with all nodes, optionallyfiltered bytest.
prop(string)— field to look up in each node to find keyskeyFunction(KeyFunction)— function called with each node to calculate keystree(Node, optional)— tree to indextest(Test, optional)—unist-util-iscompatible test
Instance (Index).
Get nodes bykey.
key(unknown)— key to retrieve, can be anything that can be used as a key in aMap
List of zero or more nodes (Array<Node>).
Addnode to the index (if not already present).
node(Node)— node to index
Current instance (Index).
Removenode from the index (if present).
node(Node)— node to remove
Current instance (Index).
Function called with every added node to calculate the key to index on(TypeScript type).
node(Node)— node to calculate a key for
Key to index on (unknown).
Can be anything that can be used as a key in aMap.
unist-util-is compatible test (TypeScript type).
This package is fully typed withTypeScript.It exports the additional typesKeyFunction andTest.
Projects maintained by the unified collective are compatible with maintainedversions of Node.js.
When we cut a new major release, we drop support for unmaintained versions ofNode.This means we try to keep the current release line,unist-util-index@^4,compatible with Node.js 16.
unist-util-is— utility to check if a node passes a testunist-util-visit— utility to recursively walk over nodesunist-util-select— select nodes with CSS-like selectors
Seecontributing.md insyntax-tree/.github forways to get started.Seesupport.md for ways to get help.
This project has aCode of Conduct.By interacting with this repository, organisation, or community you agree toabide by its terms.
MIT © Eugene Sharygin
About
utility to index property values or computed keys to nodes
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.