1
1
/**
2
- * @typedef {import('mdast').Root|import('mdast').Content } Node
2
+ * @typedef {import('mdast').Root } Root
3
+ * @typedef {import('mdast').Content } Content
3
4
* @typedef {import('mdast').Definition } Definition
4
5
*/
5
6
7
+ /**
8
+ * @typedef {Root | Content } Node
9
+ *
10
+ * @callback GetDefinition
11
+ * Get a definition by identifier.
12
+ * @param {string | null | undefined } [identifier]
13
+ * Identifier of definition.
14
+ * @returns {Definition | null }
15
+ * Definition corresponding to `identifier`, if found.
16
+ */
17
+
6
18
import { visit } from 'unist-util-visit'
7
19
8
20
const own = { } . hasOwnProperty
9
21
10
22
/**
11
23
* Find definitions in `node`.
24
+ *
12
25
* Uses CommonMark precedence, which means that earlier definitions are
13
26
* preferred over duplicate later definitions.
14
27
*
15
- * @param {Node } node
28
+ * @param {Node } tree
29
+ * Tree to check.
30
+ * @returns {GetDefinition }
31
+ * Getter.
16
32
*/
17
- export function definitions ( node ) {
33
+ export function definitions ( tree ) {
18
34
/** @type {Record<string, Definition> } */
19
35
const cache = Object . create ( null )
20
36
21
- if ( ! node || ! node . type ) {
37
+ if ( ! tree || ! tree . type ) {
22
38
throw new Error ( 'mdast-util-definitions expected node' )
23
39
}
24
40
25
- visit ( node , 'definition' , ( definition ) => {
41
+ visit ( tree , 'definition' , ( definition ) => {
26
42
const id = clean ( definition . identifier )
27
43
if ( id && ! own . call ( cache , id ) ) {
28
44
cache [ id ] = definition
@@ -31,20 +47,15 @@ export function definitions(node) {
31
47
32
48
return definition
33
49
34
- /**
35
- * Get a node from the bound definition cache.
36
- *
37
- * @param {string } identifier
38
- * @returns {Definition|null }
39
- */
50
+ /** @type {GetDefinition } */
40
51
function definition ( identifier ) {
41
52
const id = clean ( identifier )
42
53
return id && own . call ( cache , id ) ? cache [ id ] : null
43
54
}
44
55
}
45
56
46
57
/**
47
- * @param {string } [value]
58
+ * @param {string | null | undefined } [value]
48
59
* @returns {string }
49
60
*/
50
61
function clean ( value ) {
0 commit comments