Skip to content

Commit 92dc390

Browse files
committed
Refactor readme.md
1 parent 894cb02 commit 92dc390

File tree

2 files changed

+80
-69
lines changed

2 files changed

+80
-69
lines changed

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@
3737
"devDependencies": {
3838
"nyc": "^12.0.2",
3939
"prettier": "^1.14.2",
40+
"remark-cli": "^5.0.0",
41+
"remark-preset-wooorm": "^4.0.0",
4042
"tape": "^4.4.0",
4143
"unist-builder": "^1.0.1",
4244
"unist-util-select": "^1.5.0",
4345
"xo": "^0.22.0"
4446
},
4547
"scripts": {
46-
"format": "prettier --write \"**/*.js\" && xo --fix",
48+
"format": "remark . -qfo && prettier --write \"**/*.js\" && xo --fix",
4749
"test-api": "node test",
4850
"test-coverage": "nyc --reporter lcov tape test.js",
4951
"test": "npm run format && npm run test-coverage"
@@ -65,5 +67,10 @@
6567
"xo": {
6668
"prettier": true,
6769
"esnext": false
70+
},
71+
"remarkConfig": {
72+
"plugins": [
73+
"preset-wooorm"
74+
]
6875
}
6976
}

readme.md

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,110 @@
1-
[![npm](https://nodei.co/npm/unist-util-index.png)](https://npmjs.com/package/unist-util-index)
1+
# unist-util-index [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov]
22

3-
# unist-util-index
3+
Create mutable index mapping property values or computed keys back to
4+
[**unist**][unist] nodes.
45

5-
[![Build Status][travis-badge]][travis] [![Dependency Status][david-badge]][david]
6+
## Installation
67

7-
Create mutable index mapping property values or computed keys back to [Unist] nodes.
8+
[npm][]:
89

9-
[unist]: https://github.com/wooorm/unist
10+
```bash
11+
npm install unist-util-index
12+
```
1013

11-
[travis]: https://travis-ci.org/eush77/unist-util-index
12-
[travis-badge]: https://travis-ci.org/eush77/unist-util-index.svg?branch=master
13-
[david]: https://david-dm.org/eush77/unist-util-index
14-
[david-badge]: https://david-dm.org/eush77/unist-util-index.png
14+
## Usage
1515

16-
## Example
16+
## API
1717

18-
Headings by depth:
18+
### `Index([tree[, filter]], prop|keyFn)`
1919

20-
```js
21-
var Index = require('unist-util-index'),
22-
fs = require('fs'),
23-
remark = require('remark'),
24-
toString = require('mdast-util-to-string');
20+
Create an index data structure that maps keys (calculated by `keyFn` function
21+
or the values at `prop` in each node) to a list of nodes.
2522

26-
var ast = remark.parse(fs.readFileSync('README.md', 'utf8'));
27-
var index = Index(ast, 'heading', 'depth');
23+
If `tree` is given, the index is initialised with all nodes, optionally
24+
filtered by `filter`.
2825

29-
index.get(1).map(toString)
30-
//=> [ 'unist-util-index' ]
26+
###### Signatures
3127

32-
index.get(2).map(toString)
33-
//=> [ 'Example', 'API', 'Install', 'License' ]
34-
```
28+
* `Index(prop|keyFn)`
29+
* `Index(tree, prop|keyFn)`
30+
* `Index(tree, filter, prop|keyFn)`
3531

36-
Definitions by identifier:
32+
###### Parameters
3733

38-
```js
39-
var index = Index(ast, 'definition', 'identifier');
34+
* `tree` ([`Node`][node])
35+
* `filter` (`*`) — [`is`][is]-compatible test
36+
* `prop` (`string`) — Property to look up in each node to find keys
37+
* `keyFn` ([`Function`][keyfn]) — Function called with each node to calculate
38+
keys
4039

41-
index.get('unist')
42-
//=> [ { type: 'definition',
43-
// identifier: 'unist',
44-
// title: null,
45-
// url: 'https://github.com/wooorm/unist',
46-
// position: Position { start: [Object], end: [Object], indent: [] } } ]
40+
###### Returns
4741

48-
index.get('travis')
49-
//=> [ { type: 'definition',
50-
// identifier: 'travis',
51-
// title: null,
52-
// url: 'https://travis-ci.org/eush77/unist-util-index',
53-
// position: Position { start: [Object], end: [Object], indent: [] } } ]
54-
```
42+
`Index` — an index instance.
5543

56-
## API
44+
#### `function keyFn(node)`
5745

58-
### `index = Index([ast, [filter]], key)`
46+
Function called with every added [node][] to return the value to index on.
5947

60-
- `ast`[Unist] tree.
48+
#### `Index#get(key)`
6149

62-
- `filter` — one of:
63-
- node type (string);
64-
- function invoked with arguments `(node, index?, parent?)`.
50+
Get nodes by `key` (`*`).
51+
Returns a list of zero or more nodes ([`Array.<Node>`][node]).
6552

66-
- `key` — one of:
67-
- property name (string);
68-
- function invoked with argument `(node)`.
53+
#### `Index#add(node)`
6954

70-
Create index data structure that maps keys (returned by `key` function or property) to nodes.
55+
Add [`node`][node] to the index (if not already present).
7156

72-
If `ast` argument is given, initialize index with `ast` nodes (recursively), optionally filter by node type or predicate.
57+
#### `Index#remove(node)`
7358

74-
### `index.get(key)`
59+
Remove [`node`][node] from the index (if present).
7560

76-
- `key` — index key.
61+
## Related
7762

78-
Get nodes by `key`.
63+
* [`unist-util-is`](https://github.com/syntax-tree/unist-util-is)
64+
— Utility to check if a node passes a test
65+
* [`unist-util-visit`](https://github.com/syntax-tree/unist-util-visit)
66+
— Utility to recursively walk over nodes
67+
* [`unist-util-map`](https://github.com/syntax-tree/unist-util-map)
68+
— Create a new tree by mapping by the provided function
69+
* [`unist-util-flatmap`](https://gitlab.com/staltz/unist-util-flatmap)
70+
— Create a new tree by mapping and then flattening
71+
* [`unist-util-select`](https://github.com/syntax-tree/unist-util-select)
72+
— Select nodes with CSS-like selectors
7973

80-
Returns array of nodes.
74+
## Contribute
8175

82-
### `index.add(node)`
76+
See [`contributing.md` in `syntax-tree/unist`][contributing] for ways to get
77+
started.
8378

84-
- `node`[Unist] node.
79+
This organisation has a [Code of Conduct][coc]. By interacting with this
80+
repository, organisation, or community you agree to abide by its terms.
8581

86-
Add `node` to index. No-op if `node` is already present in index.
82+
## License
8783

88-
Returns `index`.
84+
[MIT][license] © Eugene Sharygin
8985

90-
### `index.remove(node)`
86+
<!-- Definitions -->
9187

92-
- `node`[Unist] node.
88+
[travis-badge]: https://img.shields.io/travis/syntax-tree/unist-util-index.svg
9389

94-
Remove `node` from index. No-op if `node` is not present in index.
90+
[travis]: https://travis-ci.org/syntax-tree/unist-util-index
9591

96-
Returns `index`.
92+
[codecov-badge]: https://img.shields.io/codecov/c/github/syntax-tree/unist-util-index.svg
9793

98-
## Install
94+
[codecov]: https://codecov.io/github/syntax-tree/unist-util-indexs
9995

100-
```
101-
npm install unist-util-index
102-
```
96+
[npm]: https://docs.npmjs.com/cli/install
10397

104-
## License
98+
[license]: license
99+
100+
[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md
101+
102+
[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md
103+
104+
[unist]: https://github.com/syntax-tree/unist
105+
106+
[node]: https://github.com/syntax-tree/unist#node
107+
108+
[is]: https://github.com/syntax-tree/unist-util-is
105109

106-
MIT
110+
[keyfn]: #function-keyfnnode

0 commit comments

Comments
 (0)