Skip to content

Commit 8fb90c4

Browse files
committed
Add improved docs
1 parent a1c42d0 commit 8fb90c4

File tree

1 file changed

+100
-24
lines changed

1 file changed

+100
-24
lines changed

readme.md

Lines changed: 100 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,76 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**unist**][unist] utility to get the positional info of nodes.
11+
[unist][] utility to get positional info of nodes.
1212

13-
## 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+
* [`position(node?)`](#positionnode)
21+
* [`pointStart(node?)`](#pointstartnode)
22+
* [`pointEnd(node?)`](#pointendnode)
23+
* [Types](#types)
24+
* [Compatibility](#compatibility)
25+
* [Related](#related)
26+
* [Contribute](#contribute)
27+
* [License](#license)
28+
29+
## What is this?
30+
31+
This utility helps with accessing positional info on a potentially dirty tree.
32+
33+
## When should I use this?
34+
35+
The positional info is typically consistent and proper in unist trees generated
36+
by our ecosystem, but, user plugins could mess that up.
37+
If you’re making a reusable plugin, and accessing the positional info often, you
38+
might want to guard against that by using this utility.
39+
40+
You might also find the utility [`unist-util-generated`][unist-util-generated]
41+
useful to check whether a node is considered to be generated (not in the
42+
original input file).
43+
44+
You might also enjoy
45+
[`unist-util-stringify-position`][unist-util-stringify-position] when you want
46+
to display positional info to users.
1447

15-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
48+
## Install
1749

18-
[npm][]:
50+
This package is [ESM only][esm].
51+
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]:
1952

2053
```sh
2154
npm install unist-util-position
2255
```
2356

57+
In Deno with [`esm.sh`][esmsh]:
58+
59+
```js
60+
import {position, pointStart, pointEnd} from "https://esm.sh/unist-util-position@4"
61+
```
62+
63+
In browsers with [`esm.sh`][esmsh]:
64+
65+
```html
66+
<script type="module">
67+
import {position, pointStart, pointEnd} from "https://esm.sh/unist-util-position@4?bundle"
68+
</script>
69+
```
70+
2471
## Use
2572

2673
```js
27-
import {remark} from 'remark'
74+
import {fromMarkdown} from 'mdast-util-from-markdown'
2875
import {position, pointStart, pointEnd} from 'unist-util-position'
2976

30-
const tree = remark().parse('# foo\n\n* bar\n')
77+
const tree = fromMarkdown('# foo\n\n* bar\n')
3178

3279
console.log(position(tree))
3380
console.log(pointStart(tree))
34-
console.log(pointEnd(tree))
35-
36-
console.log(position())
37-
console.log(pointStart())
38-
console.log(pointEnd())
3981
```
4082

4183
Yields:
@@ -44,33 +86,55 @@ Yields:
4486
{start: {line: 1, column: 1, offset: 0}, end: {line: 4, column: 1, offset: 13}}
4587
{line: 1, column: 1, offset: 0}
4688
{line: 4, column: 1, offset: 13}
47-
{start: {line: null, column: null, offset: null}, end: {line: null, column: null, offset: null}}
48-
{line: null, column: null, offset: null}
49-
{line: null, column: null, offset: null}
5089
```
5190

5291
## API
5392

54-
This package exports the following identifiers: `position`, `pointStart`, and
93+
This package exports the identifiers `position`, `pointStart`, and
5594
`pointEnd`.
5695
There is no default export.
5796

5897
### `position(node?)`
5998

6099
Get the positional info of `node` ([`Node?`][node]).
61-
Returns [`Position`][position].
100+
Returns a proper [`Position`][position].
62101

63102
### `pointStart(node?)`
64103

65104
### `pointEnd(node?)`
66105

67106
Get the start or end points in the positional info of `node` ([`Node?`][node]).
68-
Returns [`Point`][point].
107+
Returns a proper [`Point`][point].
108+
109+
## Types
110+
111+
This package is fully typed with [TypeScript][].
112+
It exports no additional types.
113+
114+
## Compatibility
115+
116+
Projects maintained by the unified collective are compatible with all maintained
117+
versions of Node.js.
118+
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
119+
Our projects sometimes work with older versions, but this is not guaranteed.
120+
121+
## Related
122+
123+
* [`unist-util-stringify-position`](https://github.com/syntax-tree/unist-util-stringify-position)
124+
— serialize a node, position, or point as a human readable location
125+
* [`unist-util-position-from-estree`](https://github.com/syntax-tree/unist-util-position-from-estree)
126+
— get a position from an estree node
127+
* [`unist-util-remove-position`](https://github.com/syntax-tree/unist-util-remove-position)
128+
— remove positions from tree
129+
* [`unist-util-generated`](https://github.com/syntax-tree/unist-util-generated)
130+
— check if a node is generated
131+
* [`unist-util-source`](https://github.com/syntax-tree/unist-util-source)
132+
— get the source of a node
69133

70134
## Contribute
71135

72-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
73-
started.
136+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
137+
ways to get started.
74138
See [`support.md`][support] for ways to get help.
75139

76140
This project has a [code of conduct][coc].
@@ -109,17 +173,25 @@ abide by its terms.
109173

110174
[chat]: https://github.com/syntax-tree/unist/discussions
111175

176+
[npm]: https://docs.npmjs.com/cli/install
177+
178+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
179+
180+
[esmsh]: https://esm.sh
181+
182+
[typescript]: https://www.typescriptlang.org
183+
112184
[license]: license
113185

114186
[author]: https://wooorm.com
115187

116-
[npm]: https://docs.npmjs.com/cli/install
188+
[health]: https://github.com/syntax-tree/.github
117189

118-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
190+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
119191

120-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
192+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
121193

122-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
194+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
123195

124196
[unist]: https://github.com/syntax-tree/unist
125197

@@ -128,3 +200,7 @@ abide by its terms.
128200
[position]: https://github.com/syntax-tree/unist#position
129201

130202
[point]: https://github.com/syntax-tree/unist#point
203+
204+
[unist-util-generated]: https://github.com/syntax-tree/unist-util-generated
205+
206+
[unist-util-stringify-position]: https://github.com/syntax-tree/unist-util-stringify-position

0 commit comments

Comments
 (0)