Skip to content

Commit e08d4bc

Browse files
committed
readme: add examples
1 parent a7da5ea commit e08d4bc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,45 @@ Create mutable index mapping property values or computed keys back to [Unist] no
1313
[david]: https://david-dm.org/eush77/unist-util-index
1414
[david-badge]: https://david-dm.org/eush77/unist-util-index.png
1515

16+
## Example
17+
18+
Headings by depth:
19+
20+
```js
21+
var Index = require('unist-util-index'),
22+
remark = require('remark'),
23+
toString = require('mdast-util-to-string');
24+
25+
var ast = remark.parse(fs.readFileSync('README.md', 'utf8'));
26+
var index = Index(ast, 'heading', 'depth');
27+
28+
index.get(1).map(toString)
29+
//=> [ 'unist-util-index' ]
30+
31+
index.get(2).map(toString)
32+
//=> [ 'Example', 'API', 'Install', 'License' ]
33+
```
34+
35+
Definitions by identifier:
36+
37+
```js
38+
var index = Index(ast, 'definition', 'identifier');
39+
40+
index.get('unist')
41+
//=> [ { type: 'definition',
42+
identifier: 'unist',
43+
title: null,
44+
link: 'https://github.com/wooorm/unist',
45+
position: Position { start: [Object], end: [Object], indent: [] } } ]
46+
47+
index.get('travis')
48+
//=> [ { type: 'definition',
49+
identifier: 'travis',
50+
title: null,
51+
link: 'https://travis-ci.org/eush77/unist-util-index',
52+
position: Position { start: [Object], end: [Object], indent: [] } } ]
53+
```
54+
1655
## API
1756

1857
### `index = Index([ast, [filter]], key)`

0 commit comments

Comments
 (0)