The Wayback Machine - https://web.archive.org/web/20201107234700/https://github.com/syntax-tree/unist-util-index
Skip to content
main
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

readme.md

unist-util-index

Build Coverage Downloads Size Sponsors Backers Chat

unist utility to create a mutable index mapping property values or computed keys back to nodes.

Install

npm:

npm install unist-util-index

Use

var fs = require('fs')
var remark = require('remark')
var toString = require('mdast-util-to-string')
var Index = require('unist-util-index')

// Parse and read this repo’s readme:
var tree = remark.parse(fs.readFileSync('readme.md'))

// Index on heading depth:
var index = new Index(tree, 'heading', 'depth')

console.log(index.get(2).map(toString))

// Index on definition identifier:
index = new Index(tree, 'definition', 'identifier')

console.log(index.get('unist').map(node => node.url))

Yields:

[ 'Install', 'Use', 'API', 'Related', 'Contribute', 'License' ]
[ 'https://github.com/syntax-tree/unist' ]

API

Index([tree[, test], ]prop|keyFn)

Create an index data structure that maps keys (calculated by keyFn function or the values at prop in each node) to a list of nodes.

If tree is given, the index is initialized with all nodes, optionally filtered by test.

Signatures
  • Index(prop|keyFn)
  • Index(tree, prop|keyFn)
  • Index(tree, test, prop|keyFn)
Parameters
  • tree (Node?) — Tree to index
  • test (Test, optional) — is-compatible test (such as a type)
  • prop (string) — Property to look up in each node to find keys
  • keyFn (Function) — Function called with each node to calculate keys
Returns

Index — an index instance.

function keyFn(node)

Function called with every added node to return the key to index on.

Index#get(key)

Get nodes by key (*). Returns a list of zero or more nodes (Array.<Node>).

Index#add(node)

Add node to the index (if not already present).

Index#remove(node)

Remove node from the index (if present).

Related

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a Code of Conduct. By interacting with this repository, organisation, or community you agree to abide by its terms.

License

MIT © Eugene Sharygin

You can’t perform that action at this time.