8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
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.
13
12
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?
15
36
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.
18
40
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] [ ] :
20
45
21
46
``` sh
22
47
npm install unist-util-index
23
48
```
24
49
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
+
25
64
## Use
26
65
27
66
``` 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 '
30
69
import {toString } from ' mdast-util-to-string'
31
70
import {Index } from ' unist-util-index'
32
71
33
72
// 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' ))
35
74
36
75
// Index on heading depth:
37
76
const indexOnDepth = new Index (' depth' , tree, ' heading' )
@@ -47,70 +86,109 @@ console.log(indexOnIdentifier.get('unist').map(node => node.url))
47
86
Yields:
48
87
49
88
``` 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
+ ]
51
102
[ ' https://github.com/syntax-tree/unist' ]
52
103
```
53
104
54
105
## API
55
106
56
- This package exports the following identifiers: ` Index ` .
107
+ This package exports the identifier ` Index ` .
57
108
There is no default export.
58
109
59
- ### ` class Index(prop|keyFn [, tree[, test]])`
110
+ ### ` Index(prop|keyFunction [, tree[, test]]) `
60
111
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.
63
114
64
115
If ` tree ` is given, the index is initialized with all nodes, optionally filtered
65
116
by ` test ` .
66
117
67
118
###### Parameters
68
119
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
75
128
76
129
###### Returns
77
130
78
- ` Index ` — an index instance .
131
+ Instance ( ` Index ` ) .
79
132
80
- #### ` function keyFn (node) `
133
+ #### ` function keyFunction (node) `
81
134
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 ] .
83
142
84
143
#### ` Index#get(key) `
85
144
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 ] ).
88
150
89
151
#### ` Index#add(node) `
90
152
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 ` ).
92
158
93
159
#### ` Index#remove(node) `
94
160
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.
96
178
97
179
## Related
98
180
99
181
* [ ` 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
101
183
* [ ` 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
107
185
* [ ` 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
109
187
110
188
## Contribute
111
189
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.
114
192
See [ ` support.md ` ] [ support ] for ways to get help.
115
193
116
194
This project has a [ Code of Conduct] [ coc ] .
@@ -151,22 +229,28 @@ abide by its terms.
151
229
152
230
[ npm ] : https://docs.npmjs.com/cli/install
153
231
232
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
233
+
234
+ [ esmsh ] : https://esm.sh
235
+
236
+ [ typescript ] : https://www.typescriptlang.org
237
+
154
238
[ license ] : license
155
239
156
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
240
+ [ health ] : https://github.com/syntax-tree/.github
157
241
158
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD/support .md
242
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing .md
159
243
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
161
247
162
248
[ unist ] : https://github.com/syntax-tree/unist
163
249
164
250
[ node ] : https://github.com/syntax-tree/unist#node
165
251
166
- [ tree ] : https://github.com/syntax-tree/unist#tree
167
-
168
- [ type ] : https://github.com/syntax-tree/unist#type
169
-
170
252
[ is ] : https://github.com/syntax-tree/unist-util-is
171
253
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