-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
47 lines (35 loc) · 1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'use strict'
var visit = require('unist-util-visit')
module.exports = getDefinitionFactory
var own = {}.hasOwnProperty
// Get a definition in `node` by `identifier`.
function getDefinitionFactory(node, options) {
return getterFactory(gather(node, options))
}
// Gather all definitions in `node`
function gather(node) {
var cache = {}
if (!node || !node.type) {
throw new Error('mdast-util-definitions expected node')
}
visit(node, 'definition', ondefinition)
return cache
function ondefinition(definition) {
var id = normalise(definition.identifier)
if (!own.call(cache, id)) {
cache[id] = definition
}
}
}
// Factory to get a node from the given definition-cache.
function getterFactory(cache) {
return getter
// Get a node from the bound definition-cache.
function getter(identifier) {
var id = identifier && normalise(identifier)
return id && own.call(cache, id) ? cache[id] : null
}
}
function normalise(identifier) {
return identifier.toUpperCase()
}