8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** unist** ] [ unist ] utility to get the positional info of nodes.
11
+ [ unist] [ ] utility to get positional info of nodes.
12
12
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.
14
47
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
17
49
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] [ ] :
19
52
20
53
``` sh
21
54
npm install unist-util-position
22
55
```
23
56
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
+
24
71
## Use
25
72
26
73
``` js
27
- import {remark } from ' remark '
74
+ import {fromMarkdown } from ' mdast-util-from-markdown '
28
75
import {position , pointStart , pointEnd } from ' unist-util-position'
29
76
30
- const tree = remark (). parse (' # foo\n\n * bar\n ' )
77
+ const tree = fromMarkdown (' # foo\n\n * bar\n ' )
31
78
32
79
console .log (position (tree))
33
80
console .log (pointStart (tree))
34
- console .log (pointEnd (tree))
35
-
36
- console .log (position ())
37
- console .log (pointStart ())
38
- console .log (pointEnd ())
39
81
```
40
82
41
83
Yields:
@@ -44,33 +86,55 @@ Yields:
44
86
{start: {line: 1 , column: 1 , offset: 0 }, end: {line: 4 , column: 1 , offset: 13 }}
45
87
{line: 1 , column: 1 , offset: 0 }
46
88
{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 }
50
89
```
51
90
52
91
## API
53
92
54
- This package exports the following identifiers: ` position ` , ` pointStart ` , and
93
+ This package exports the identifiers ` position ` , ` pointStart ` , and
55
94
` pointEnd ` .
56
95
There is no default export.
57
96
58
97
### ` position(node?) `
59
98
60
99
Get the positional info of ` node ` ([ ` Node? ` ] [ node ] ).
61
- Returns [ ` Position ` ] [ position ] .
100
+ Returns a proper [ ` Position ` ] [ position ] .
62
101
63
102
### ` pointStart(node?) `
64
103
65
104
### ` pointEnd(node?) `
66
105
67
106
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
69
133
70
134
## Contribute
71
135
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.
74
138
See [ ` support.md ` ] [ support ] for ways to get help.
75
139
76
140
This project has a [ code of conduct] [ coc ] .
@@ -109,17 +173,25 @@ abide by its terms.
109
173
110
174
[ chat ] : https://github.com/syntax-tree/unist/discussions
111
175
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
+
112
184
[ license ] : license
113
185
114
186
[ author ] : https://wooorm.com
115
187
116
- [ npm ] : https://docs.npmjs. com/cli/install
188
+ [ health ] : https://github. com/syntax-tree/.github
117
189
118
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD /contributing.md
190
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main /contributing.md
119
191
120
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
192
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
121
193
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
123
195
124
196
[ unist ] : https://github.com/syntax-tree/unist
125
197
@@ -128,3 +200,7 @@ abide by its terms.
128
200
[ position ] : https://github.com/syntax-tree/unist#position
129
201
130
202
[ 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