Skip to content

Commit 12bdd72

Browse files
committed
Add improved docs
1 parent 716d8c7 commit 12bdd72

File tree

2 files changed

+80
-29
lines changed

2 files changed

+80
-29
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const search = /\r?\n|\r/g
1717
* @param {VFile|VFileValue} file
1818
* File in which `value` exists.
1919
* @returns {string|null}
20-
* Source of a node.
20+
* Source of `value` in `doc`, if available.
2121
*/
2222
export function source(value, file) {
2323
const doc = String(file)

readme.md

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,71 +8,116 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

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.
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+
* [`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.
1429

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
1736

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][]:
1939

2040
```sh
2141
npm install unist-util-source
2242
```
2343

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+
2458
## Use
2559

26-
Say we have the following file, `example.md`:
60+
Say our document `example.md` contains:
2761

2862
```markdown
2963
> + **[Hello](./example)**
3064
> world.
3165
```
3266

33-
And our script, `example.js`, looks as follows:
67+
…and our module `example.js` looks as follows:
3468

3569
```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'
3972
import {source} from 'unist-util-source'
4073

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))
4576

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]
4778
console.log(source(strong, file))
4879
```
4980

50-
Now, running `node example` yields:
81+
…now running `node example.js` yields:
5182

5283
```markdown
5384
**[Hello](./example)**
5485
```
5586

5687
## API
5788

89+
This package exports the identifier `source`.
90+
There is no default export.
91+
5892
### `source(value, file)`
5993

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.
6295

6396
###### Parameters
6497

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
67100

68101
###### Returns
69102

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.
71116

72117
## Contribute
73118

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.
76121
See [`support.md`][support] for ways to get help.
77122

78123
This project has a [code of conduct][coc].
@@ -113,15 +158,23 @@ abide by its terms.
113158

114159
[npm]: https://docs.npmjs.com/cli/install
115160

161+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
162+
163+
[esmsh]: https://esm.sh
164+
165+
[typescript]: https://www.typescriptlang.org
166+
116167
[license]: license
117168

118169
[author]: https://wooorm.com
119170

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
121174

122-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
175+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
123176

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
125178

126179
[unist]: https://github.com/syntax-tree/unist
127180

@@ -130,5 +183,3 @@ abide by its terms.
130183
[position]: https://github.com/syntax-tree/unist#position
131184

132185
[vfile]: https://github.com/vfile/vfile
133-
134-
[file]: https://github.com/syntax-tree/unist#file

0 commit comments

Comments
 (0)