8
8
[ ![ Backers] [ backers-badge ]] [ collective ]
9
9
[ ![ Chat] [ chat-badge ]] [ chat ]
10
10
11
- [ ** unist** ] [ unist ] utility to get the source of a node or at a position.
11
+ [ unist] [ ] utility to get the source code of a node or position.
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
+ * [ ` source(value, file) ` ] ( #sourcevalue-file )
21
+ * [ Types] ( #types )
22
+ * [ Compatibility] ( #compatibility )
23
+ * [ Contribute] ( #contribute )
24
+ * [ License] ( #license )
25
+
26
+ ## What is this?
27
+
28
+ This is a tiny utility that lets you get the source code of a node or position.
14
29
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.
30
+ ## When should I use this?
31
+
32
+ This is super tiny utility useful when you want to display the source code
33
+ of something in a file.
34
+
35
+ ## Install
17
36
18
- [ npm] [ ] :
37
+ This package is [ ESM only] [ esm ] .
38
+ In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [ npm] [ ] :
19
39
20
40
``` sh
21
41
npm install unist-util-source
22
42
```
23
43
44
+ In Deno with [ ` esm.sh ` ] [ esmsh ] :
45
+
46
+ ``` js
47
+ import {source } from " https://esm.sh/unist-util-source@4"
48
+ ```
49
+
50
+ In browsers with [ ` esm.sh ` ] [ esmsh ] :
51
+
52
+ ``` html
53
+ <script type =" module" >
54
+ import {source } from " https://esm.sh/unist-util-source@4?bundle"
55
+ </script >
56
+ ```
57
+
24
58
## Use
25
59
26
- Say we have the following file, ` example.md ` :
60
+ Say our document ` example.md ` contains :
27
61
28
62
``` markdown
29
63
> + **[Hello](./example)**
30
64
> world.
31
65
```
32
66
33
- And our script, ` example.js ` , looks as follows:
67
+ …and our module ` example.js ` looks as follows:
34
68
35
69
``` js
36
- import {readSync } from ' to-vfile'
37
- import {unified } from ' unified'
38
- import remarkParse from ' remark-parse'
70
+ import {read } from ' to-vfile'
71
+ import {fromMarkdown } from ' mdast-util-from-markdown'
39
72
import {source } from ' unist-util-source'
40
73
41
- const file = readSync (' example.md' )
42
- const tree = unified ()
43
- .use (remarkParse)
44
- .parse (file)
74
+ const file = await read (' example.md' )
75
+ const tree = fromMarkdown (String (file))
45
76
46
- var strong = tree .children [0 ].children [0 ].children [0 ].children [0 ].children [0 ]
77
+ const strong = tree .children [0 ].children [0 ].children [0 ].children [0 ].children [0 ]
47
78
console .log (source (strong, file))
48
79
```
49
80
50
- Now, running ` node example ` yields:
81
+ …now running ` node example.js ` yields:
51
82
52
83
``` markdown
53
84
**[Hello](./example)**
54
85
```
55
86
56
87
## API
57
88
89
+ This package exports the identifier ` source ` .
90
+ There is no default export.
91
+
58
92
### ` source(value, file) `
59
93
60
- This package exports the following identifiers: ` source ` .
61
- There is no default export.
94
+ Get the source of a node or at a position.
62
95
63
96
###### Parameters
64
97
65
- * ` value ` ([ ` Node ` ] [ node ] or [ ` Position ` ] [ position ] ) — Value to get
66
- * ` file ` ([ ` VFile ` ] [ vfile ] or ` string ` ) — [ file] [ ] in which ` value ` exists
98
+ * ` value ` ([ ` Node ` ] [ node ] or [ ` Position ` ] [ position ] ) — value to get
99
+ * ` file ` ([ ` VFile ` ] [ vfile ] or ` string ` ) — file in which ` value ` exists
67
100
68
101
###### Returns
69
102
70
- ` string? ` — Source of ` value ` in ` doc ` , if available.
103
+ Source of ` value ` in ` doc ` , if available (` string? ` ).
104
+
105
+ ## Types
106
+
107
+ This package is fully typed with [ TypeScript] [ ] .
108
+ It exports no additional types.
109
+
110
+ ## Compatibility
111
+
112
+ Projects maintained by the unified collective are compatible with all maintained
113
+ versions of Node.js.
114
+ As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+.
115
+ Our projects sometimes work with older versions, but this is not guaranteed.
71
116
72
117
## Contribute
73
118
74
- See [ ` contributing.md ` in ` syntax-tree/.github ` ] [ contributing ] for ways to get
75
- started.
119
+ See [ ` contributing.md ` ] [ contributing ] in [ ` syntax-tree/.github ` ] [ health ] for
120
+ ways to get started.
76
121
See [ ` support.md ` ] [ support ] for ways to get help.
77
122
78
123
This project has a [ code of conduct] [ coc ] .
@@ -113,15 +158,23 @@ abide by its terms.
113
158
114
159
[ npm ] : https://docs.npmjs.com/cli/install
115
160
161
+ [ esm ] : https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
162
+
163
+ [ esmsh ] : https://esm.sh
164
+
165
+ [ typescript ] : https://www.typescriptlang.org
166
+
116
167
[ license ] : license
117
168
118
169
[ author ] : https://wooorm.com
119
170
120
- [ contributing ] : https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
171
+ [ health ] : https://github.com/syntax-tree/.github
172
+
173
+ [ contributing ] : https://github.com/syntax-tree/.github/blob/main/contributing.md
121
174
122
- [ support ] : https://github.com/syntax-tree/.github/blob/HEAD /support.md
175
+ [ support ] : https://github.com/syntax-tree/.github/blob/main /support.md
123
176
124
- [ coc ] : https://github.com/syntax-tree/.github/blob/HEAD /code-of-conduct.md
177
+ [ coc ] : https://github.com/syntax-tree/.github/blob/main /code-of-conduct.md
125
178
126
179
[ unist ] : https://github.com/syntax-tree/unist
127
180
@@ -130,5 +183,3 @@ abide by its terms.
130
183
[ position ] : https://github.com/syntax-tree/unist#position
131
184
132
185
[ vfile ] : https://github.com/vfile/vfile
133
-
134
- [ file ] : https://github.com/syntax-tree/unist#file
0 commit comments