Skip to content

Commit ffff195

Browse files
committed
Add improved docs
1 parent f20a196 commit ffff195

File tree

1 file changed

+128
-44
lines changed

1 file changed

+128
-44
lines changed

readme.md

Lines changed: 128 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,69 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**unist**][unist] utility to create a mutable index mapping property values or
12-
computed keys back to nodes.
11+
[unist][] utility to create an index from certain nodes.
1312

14-
## Install
13+
## Contents
14+
15+
* [What is this?](#what-is-this)
16+
* [When should I use this?](#when-should-i-use-this)
17+
* [Install](#install)
18+
* [Use](#use)
19+
* [API](#api)
20+
* [`Index(prop|keyFunction[, tree[, test]])`](#indexpropkeyfunction-tree-test)
21+
* [Types](#types)
22+
* [Compatibility](#compatibility)
23+
* [Related](#related)
24+
* [Contribute](#contribute)
25+
* [License](#license)
26+
27+
## What is this?
28+
29+
This utility creates a mutable index data structure, that maps property values
30+
or computed keys, to nodes.
31+
For example, you can use this to index all (footnote) definitions in a tree,
32+
or all headings of a certain rank, to later retrieve them without having to walk
33+
the tree each time.
34+
35+
## When should I use this?
1536

16-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
17-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
37+
This is a utility that helps you deal with indexing the tree.
38+
It’s pretty small, and you can definitely do it yourself, but this little
39+
wrapper makes it all a bit easier.
1840

19-
[npm][]:
41+
## Install
42+
43+
This package is [ESM only][esm].
44+
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
2045

2146
```sh
2247
npm install unist-util-index
2348
```
2449

50+
In Deno with [`esm.sh`][esmsh]:
51+
52+
```js
53+
import {Index} from "https://esm.sh/unist-util-index@3"
54+
```
55+
56+
In browsers with [`esm.sh`][esmsh]:
57+
58+
```html
59+
<script type="module">
60+
import {Index} from "https://esm.sh/unist-util-index@3?bundle"
61+
</script>
62+
```
63+
2564
## Use
2665

2766
```js
28-
import fs from 'node:fs'
29-
import {remark} from 'remark'
67+
import fs from 'node:fs/promises'
68+
import {fromMarkdown} from 'mdast-util-from-markdown'
3069
import {toString} from 'mdast-util-to-string'
3170
import {Index} from 'unist-util-index'
3271

3372
// Parse and read this repo’s readme:
34-
const tree = remark.parse(fs.readFileSync('readme.md'))
73+
const tree = fromMarkdown(await fs.readFile('readme.md'))
3574

3675
// Index on heading depth:
3776
const indexOnDepth = new Index('depth', tree, 'heading')
@@ -47,70 +86,109 @@ console.log(indexOnIdentifier.get('unist').map(node => node.url))
4786
Yields:
4887

4988
```js
50-
[ 'Install', 'Use', 'API', 'Related', 'Contribute', 'License' ]
89+
[
90+
'Contents',
91+
'What is this?',
92+
'When should I use this?',
93+
'Install',
94+
'Use',
95+
'API',
96+
'Types',
97+
'Compatibility',
98+
'Related',
99+
'Contribute',
100+
'License'
101+
]
51102
[ 'https://github.com/syntax-tree/unist' ]
52103
```
53104

54105
## API
55106

56-
This package exports the following identifiers: `Index`.
107+
This package exports the identifier `Index`.
57108
There is no default export.
58109

59-
### `class Index(prop|keyFn[, tree[, test]])`
110+
### `Index(prop|keyFunction[, tree[, test]])`
60111

61-
Create an index data structure that maps keys (calculated by `keyFn` function or
62-
the values at `prop` in each node) to a list of nodes.
112+
Create a mutable index data structure, that maps property values or computed
113+
keys, to nodes.
63114

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

67118
###### Parameters
68119

69-
* `prop` (`string`) — Property to look up in each node to find keys
70-
* `keyFn` ([`Function`][keyfn]) — Function called with each node to calculate
71-
keys
72-
* `tree` ([`Node?`][node]) — [Tree][] to index
73-
* `test` ([`Test`][is], optional) — [`is`][is]-compatible test (such as a
74-
[type][])
120+
* `prop` (`string`)
121+
— property to look up in each node to find keys
122+
* `keyFunction` ([`KeyFunction`][key-function])
123+
— function called with each node to calculate keys
124+
* `tree` ([`Node`][node], optional)
125+
— tree to index
126+
* `test` ([`Test`][is], optional)
127+
[`is`][is]-compatible test
75128

76129
###### Returns
77130

78-
`Index` — an index instance.
131+
Instance (`Index`).
79132

80-
#### `function keyFn(node)`
133+
#### `function keyFunction(node)`
81134

82-
Function called with every added [node][] to return the key to index on.
135+
Function called with every added node ([`Node`][node]) to calculate the key to
136+
index on.
137+
138+
###### Returns
139+
140+
Key to index on (`unknown`).
141+
Can be anything that can be used as a key in a [`Map`][map].
83142

84143
#### `Index#get(key)`
85144

86-
Get nodes by `key` (`*`).
87-
Returns a list of zero or more nodes ([`Array<Node>`][node]).
145+
Get nodes by `key` (`unknown`).
146+
147+
###### Returns
148+
149+
List of zero or more nodes ([`Array<Node>`][node]).
88150

89151
#### `Index#add(node)`
90152

91-
Add [`node`][node] to the index (if not already present).
153+
Add `node` ([`Node`][node]) to the index (if not already present).
154+
155+
###### Returns
156+
157+
Nothing (`void`).
92158

93159
#### `Index#remove(node)`
94160

95-
Remove [`node`][node] from the index (if present).
161+
Remove `node` ([`Node`][node]) from the index (if present).
162+
163+
###### Returns
164+
165+
Nothing (`void`).
166+
167+
## Types
168+
169+
This package is fully typed with [TypeScript][].
170+
It exports the additional types `KeyFunction` and `Test`.
171+
172+
## Compatibility
173+
174+
Projects maintained by the unified collective are compatible with all maintained
175+
versions of Node.js.
176+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
177+
Our projects sometimes work with older versions, but this is not guaranteed.
96178

97179
## Related
98180

99181
* [`unist-util-is`](https://github.com/syntax-tree/unist-util-is)
100-
Utility to check if a node passes a test
182+
utility to check if a node passes a test
101183
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
102-
— Utility to recursively walk over nodes
103-
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
104-
— Create a new tree by mapping by the provided function
105-
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
106-
— Create a new tree by mapping and then flattening
184+
— utility to recursively walk over nodes
107185
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
108-
Select nodes with CSS-like selectors
186+
select nodes with CSS-like selectors
109187

110188
## Contribute
111189

112-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
113-
started.
190+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
191+
ways to get started.
114192
See [`support.md`][support] for ways to get help.
115193

116194
This project has a [Code of Conduct][coc].
@@ -151,22 +229,28 @@ abide by its terms.
151229

152230
[npm]: https://docs.npmjs.com/cli/install
153231

232+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
233+
234+
[esmsh]: https://esm.sh
235+
236+
[typescript]: https://www.typescriptlang.org
237+
154238
[license]: license
155239

156-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
240+
[health]: https://github.com/syntax-tree/.github
157241

158-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
242+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
159243

160-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
244+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
245+
246+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
161247

162248
[unist]: https://github.com/syntax-tree/unist
163249

164250
[node]: https://github.com/syntax-tree/unist#node
165251

166-
[tree]: https://github.com/syntax-tree/unist#tree
167-
168-
[type]: https://github.com/syntax-tree/unist#type
169-
170252
[is]: https://github.com/syntax-tree/unist-util-is
171253

172-
[keyfn]: #function-keyfnnode
254+
[map]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map
255+
256+
[key-function]: #function-keyfunctionnode

0 commit comments

Comments
 (0)