Skip to content

Commit 8898081

Browse files
committed
Add more basic example and update README
1 parent 22a526a commit 8898081

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed

README.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,46 @@ Add parent references to [unist] nodes.
1313
[david]: https://david-dm.org/eush77/unist-util-parents
1414
[david-badge]: https://david-dm.org/eush77/unist-util-parents.png
1515

16-
## Example
16+
## Examples
17+
18+
Follow parent links:
19+
20+
```js
21+
var ast = mdast.parse('- top-level item\n' +
22+
' - subitem 1\n' +
23+
' - subitem 2\n');
24+
25+
// "subitem 2"
26+
var item = parents(ast).children[0].children[0].children[1].children[1];
27+
28+
var chain = [];
29+
while (item) {
30+
chain.unshift(item.type);
31+
item = item.parent;
32+
}
33+
chain
34+
//=> [ 'root', 'list', 'listItem', 'list', 'listItem' ]
35+
```
1736

1837
Unwrap all `emphasis` and `strong` nodes:
1938

2039
```js
2140
var parents = require('unist-util-parents'),
2241
select = require('unist-util-select');
2342

24-
var ast = mdast.parse(src);
2543
select(parents(ast), 'emphasis, strong').forEach(function (em) {
2644
var siblings = em.parent.node.children;
2745
var index = siblings.indexOf(em.node);
2846
[].splice.apply(siblings, [index, 1].concat(em.node.children));
2947
});
3048
```
3149

32-
Which turns this:
50+
input:
3351

3452
```md
3553
# Drop highlight
3654

37-
Drop __all__ the [*emphasis*][1] and **bold** highlighting, leaving
55+
Drop __all__ the [*emphasis*][1] and **[bold][1]** highlighting, leaving
3856

3957
| _everything_ |
4058
| :----------: |
@@ -45,12 +63,12 @@ Drop __all__ the [*emphasis*][1] and **bold** highlighting, leaving
4563
[1]: https://ddg.gg/?q=emphasis
4664
```
4765

48-
into this:
66+
output:
4967

5068
```md
5169
# Drop highlight
5270

53-
Drop all the [emphasis][1] and bold highlighting, leaving
71+
Drop all the [emphasis][1] and [bold][1] highlighting, leaving
5472

5573
| everything |
5674
| :--------: |
@@ -59,21 +77,22 @@ Drop all the [emphasis][1] and bold highlighting, leaving
5977
## intact
6078

6179
[1]: https://ddg.gg/?q=emphasis
62-
6380
```
6481

6582
## API
6683

6784
#### `parents(ast) -> wrappedAst`
6885

69-
Wraps AST with a proxy that imposes two additional properties on all nodes:
86+
Wraps AST with a proxy that imposes two additional properties on all of its nodes:
7087

7188
- `parent` — parent link, `null` for the root node.
72-
- `node` — link to the original AST node, e.g. for adding or changing attributes.
89+
- `node` — link to the original AST node.
7390

7491
None of these properties are enumerable, and the original AST is _not changed_. This means you can JSON.stringify the wrapped tree and it is just the same.
7592

76-
Remember to access `.node` before you commit any changes to a node, including its `children` array.
93+
`wrappedAst.children` returns array of wrapped child nodes, so that any recursive algorithm will work on a wrapped tree just as well.
94+
95+
Remember to access `.node` before you commit any changes to a node.
7796

7897
## Install
7998

example/doc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Drop highlight
22

3-
Drop __all__ the [*emphasis*][1] and **bold** highlighting, leaving
3+
Drop __all__ the [*emphasis*][1] and **[bold][1]** highlighting, leaving
44

55
| _everything_ |
66
| :----------: |

example/list.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
var mdast = require('mdast'),
4+
parents = require('..');
5+
6+
7+
var ast = mdast.parse('- top-level item\n' +
8+
' - subitem 1\n' +
9+
' - subitem 2\n');
10+
11+
var item = parents(ast).children[0].children[0].children[1].children[1];
12+
13+
var chain = [];
14+
while (item) {
15+
chain.unshift(item.type);
16+
item = item.parent;
17+
}
18+
19+
console.log(chain);

0 commit comments

Comments
 (0)