Skip to content

Commit

Permalink
Refactor prose
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 21, 2019
1 parent 3ae6a2d commit 265ed40
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,61 @@
[![Downloads][downloads-badge]][downloads]
[![Size][size-badge]][size]

[**Unist**][unist] utility to find a node before another node.
[**unist**][unist] utility to find a node before another node.

## Installation
## Install

[npm][]:

```bash
```sh
npm install unist-util-find-before
```

## Usage

```js
var remark = require('remark')
var u = require('unist-builder')
var findBefore = require('unist-util-find-before')

var tree = remark().parse('Some _emphasis_, **importance**, and `code`.')
var paragraph = tree.children[0]
var code = paragraph.children[paragraph.children.length - 1]
var tree = u('tree', [
u('leaf', 'leaf 1'),
u('node', [u('leaf', 'leaf 2'), u('leaf', 'leaf 3')]),
u('leaf', 'leaf 4'),
u('node', [u('leaf', 'leaf 5')]),
u('leaf', 'leaf 6'),
u('void'),
u('leaf', 'leaf 7')
])

console.log(findBefore(paragraph, code, 'emphasis'))
var empty = tree.children[5]

console.log(findBefore(tree, empty, 'node'))
```

Yields:

```js
{ type: 'emphasis',
children: [ { type: 'text', value: 'emphasis' } ] }
{ type: 'node', children: [ { type: 'leaf', value: 'leaf 5' } ] }
```

## API

### `findBefore(parent, node|index[, test])`

Find the first child before `index` (or `node`) in `parent`, that passes `test`
(when given).
Find the first [child][] before `index` (or `node`) in `parent`, that passes
`test`.

###### Parameters

* `parent` ([`Node`][node]) — Context node
* `node` ([`Node`][node]) — Node in `parent`
* `index` (`number`, optional) — Position of a `node` in `parent`
* `test` (`Function`, `string`, or `Node`, optional)
* `parent` ([`Node`][node]) — [Parent][] node
* `node` ([`Node`][node]) — [Child][] of `parent`
* `index` (`number`, optional) — [Index][] in `parent`
* `test` (`Function`, `string`, `Object`, `Array`, optional)
— See [`unist-util-is`][is]

###### Returns

[`Node?`][node] — Child node of `parent` passing `test`.
[`Node?`][node][Child][] of `parent` passing `test`.

## Related

Expand All @@ -69,11 +76,13 @@ Find the first child before `index` (or `node`) in `parent`, that passes `test`

## Contribute

See [`contributing.md` in `syntax-tree/unist`][contributing] for ways to get
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
started.
See [`support.md`][support] for ways to get help.

This organisation has a [Code of Conduct][coc]. By interacting with this
repository, organisation, or community you agree to abide by its terms.
This project has a [Code of Conduct][coc].
By interacting with this repository, organisation, or community you agree to
abide by its terms.

## License

Expand Down Expand Up @@ -107,8 +116,16 @@ repository, organisation, or community you agree to abide by its terms.

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

[parent]: https://github.com/syntax-tree/unist#parent-1

[child]: https://github.com/syntax-tree/unist#child

[index]: https://github.com/syntax-tree/unist#index

[is]: https://github.com/syntax-tree/unist-util-is

[contributing]: https://github.com/syntax-tree/unist/blob/master/contributing.md
[contributing]: https://github.com/syntax-tree/.github/blob/master/contributing.md

[support]: https://github.com/syntax-tree/.github/blob/master/support.md

[coc]: https://github.com/syntax-tree/unist/blob/master/code-of-conduct.md
[coc]: https://github.com/syntax-tree/.github/blob/master/code-of-conduct.md

0 comments on commit 265ed40

Please sign in to comment.