Skip to content

Commit b44aa8b

Browse files
committed
Add improved docs
1 parent e2a3079 commit b44aa8b

File tree

2 files changed

+86
-33
lines changed

2 files changed

+86
-33
lines changed

index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ import {visit} from 'unist-util-visit'
88
const own = {}.hasOwnProperty
99

1010
/**
11+
* Find definitions in `node`.
12+
* Uses CommonMark precedence, which means that earlier definitions are
13+
* preferred over duplicate later definitions.
1114
*
1215
* @param {Node} node
1316
*/
1417
export function definitions(node) {
15-
/** @type {Object.<string, Definition>} */
18+
/** @type {Record<string, Definition>} */
1619
const cache = Object.create(null)
1720

1821
if (!node || !node.type) {
@@ -26,15 +29,15 @@ export function definitions(node) {
2629
}
2730
})
2831

29-
return getDefinition
32+
return definition
3033

3134
/**
32-
* Get a node from the bound definition-cache.
35+
* Get a node from the bound definition cache.
3336
*
3437
* @param {string} identifier
3538
* @returns {Definition|null}
3639
*/
37-
function getDefinition(identifier) {
40+
function definition(identifier) {
3841
const id = clean(identifier)
3942
return id && own.call(cache, id) ? cache[id] : null
4043
}

readme.md

+79-29
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,64 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**mdast**][mdast] utility to get definitions by `identifier`.
11+
[mdast][] utility to find definitions by `identifier`.
1212

13-
Supports funky keys, like `__proto__` or `toString`.
13+
## Contents
1414

15-
## Install
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+
* [`definitions(node)`](#definitionsnode)
21+
* [`definition(identifier)`](#definitionidentifier)
22+
* [Types](#types)
23+
* [Compatibility](#compatibility)
24+
* [Security](#security)
25+
* [Related](#related)
26+
* [Contribute](#contribute)
27+
* [License](#license)
28+
29+
## What is this?
1630

17-
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
18-
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
31+
This package is a tiny utility that lets you find definitions.
1932

20-
[npm][]:
33+
## When should I use this?
34+
35+
This utility can be useful because definitions can occur after the things that
36+
reference them.
37+
It’s small and protects against prototype pollution.
38+
39+
## Install
40+
41+
This package is [ESM only][esm].
42+
In Node.js (version 12.20+, 14.14+, or 16.0+), install with [npm][]:
2143

2244
```sh
2345
npm install mdast-util-definitions
2446
```
2547

48+
In Deno with [`esm.sh`][esmsh]:
49+
50+
```js
51+
import {definitions} from 'https://esm.sh/mdast-util-definitions@5'
52+
```
53+
54+
In browsers with [`esm.sh`][esmsh]:
55+
56+
```html
57+
<script type="module">
58+
import {definitions} from 'https://esm.sh/mdast-util-definitions@5?bundle'
59+
</script>
60+
```
61+
2662
## Use
2763

2864
```js
29-
import {remark} from 'remark'
65+
import {fromMarkdown} from 'mdast-util-from-markdown'
3066
import {definitions} from 'mdast-util-definitions'
3167

32-
const tree = remark().parse('[example]: https://example.com "Example"')
68+
const tree = fromMarkdown('[example]: https://example.com "Example"')
3369

3470
const definition = definitions(tree)
3571

@@ -42,35 +78,43 @@ definition('foo')
4278

4379
## API
4480

45-
This package exports the following identifiers: `definitions`.
81+
This package exports the identifier `definitions`.
4682
There is no default export.
4783

48-
### `definitions(tree)`
84+
### `definitions(node)`
4985

50-
Create a cache of all [definition][]s in [`tree`][node].
51-
52-
Uses CommonMark precedence: prefers the first definitions for duplicate
53-
definitions.
86+
Find [definition][]s in `node` ([`Node`][node]).
87+
Uses CommonMark precedence, which means that earlier definitions are preferred
88+
over duplicate later definitions.
5489

5590
###### Returns
5691

57-
[`Function`][fn-definition]
92+
`definition` ([`Function`][fn-definition]).
5893

5994
### `definition(identifier)`
6095

61-
###### Parameters
62-
63-
* `identifier` (`string`) — [Identifier][] of [definition][].
96+
Get a node from the bound definition cache by its `identifier` (`string`).
6497

6598
###### Returns
6699

67-
[`Node?`][node][Definition][], if found.
100+
[Definition][], if found ([`Node?`][node])
68101

69-
## Security
102+
## Types
103+
104+
This package is fully typed with [TypeScript][].
105+
There are no additional exported types.
106+
107+
## Compatibility
108+
109+
Projects maintained by the unified collective are compatible with all maintained
110+
versions of Node.js.
111+
As of now, that is Node.js 12.20+, 14.14+, and 16.0+.
112+
Our projects sometimes work with older versions, but this is not guaranteed.
70113

71-
Use of `mdast-util-definitions` does not involve [**hast**][hast] or user
72-
content so there are no openings for [cross-site scripting (XSS)][xss] attacks.
114+
## Security
73115

116+
Use of `mdast-util-definitions` does not involve **[hast][]** or user content so
117+
there are no openings for [cross-site scripting (XSS)][xss] attacks.
74118
Additionally, safe guards are in place to protect against prototype poisoning.
75119

76120
## Related
@@ -80,8 +124,8 @@ Additionally, safe guards are in place to protect against prototype poisoning.
80124

81125
## Contribute
82126

83-
See [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get
84-
started.
127+
See [`contributing.md`][contributing] in [`syntax-tree/.github`][health] for
128+
ways to get started.
85129
See [`support.md`][support] for ways to get help.
86130

87131
This project has a [code of conduct][coc].
@@ -126,11 +170,19 @@ abide by its terms.
126170

127171
[npm]: https://docs.npmjs.com/cli/install
128172

129-
[contributing]: https://github.com/syntax-tree/.github/blob/HEAD/contributing.md
173+
[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
174+
175+
[esmsh]: https://esm.sh
176+
177+
[typescript]: https://www.typescriptlang.org
130178

131-
[support]: https://github.com/syntax-tree/.github/blob/HEAD/support.md
179+
[health]: https://github.com/syntax-tree/.github
132180

133-
[coc]: https://github.com/syntax-tree/.github/blob/HEAD/code-of-conduct.md
181+
[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md
182+
183+
[support]: https://github.com/syntax-tree/.github/blob/main/support.md
184+
185+
[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md
134186

135187
[mdast]: https://github.com/syntax-tree/mdast
136188

@@ -140,8 +192,6 @@ abide by its terms.
140192

141193
[definition]: https://github.com/syntax-tree/mdast#definition
142194

143-
[identifier]: https://github.com/syntax-tree/mdast#association
144-
145195
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
146196

147197
[hast]: https://github.com/syntax-tree/hast

0 commit comments

Comments
 (0)