/
helpers.js
58 lines (53 loc) · 1.58 KB
/
helpers.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
48
49
50
51
52
53
54
55
56
57
58
const dmd = require('dmd/helpers/ddata')
exports.link = function(longname, options) {
return options.fn(_link(longname, options))
}
/**
* e.g. namepaths `module:Something` or type expression `Array.<module:Something>`
* @static
* @param {string} - namepath or type expression
* @param {object} - the handlebars helper options object
*/
function _link(input, options) {
if (typeof input !== 'string') return null
var linked, matches, namepath
var output = {}
/*
test input for
1. A type expression containing a namepath, e.g. Array.<module:Something>
2. a namepath referencing an `id`
3. a namepath referencing a `longname`
*/
if ((matches = input.match(/.*?<(.*?)>/))) {
namepath = matches[1]
} else {
namepath = input
}
options.hash = { id: namepath }
linked = dmd._identifier(options)
if (!linked) {
options.hash = { longname: namepath }
linked = dmd._identifier(options)
}
if (!linked) {
output = { name: input, url: null }
} else {
output.name = input.replace(namepath, linked.name)
if (dmd.isExternal.call(linked)) {
if (linked.description) {
output.url = 'external#' + dmd.anchorName.call(linked, options)
} else {
if (linked.see && linked.see.length) {
var firstLink = dmd.arseLink(linked.see[0])[0]
output.url = firstLink ? firstLink.url : linked.see[0]
} else {
output.url = null
}
}
} else {
const pre = linked.scope === 'global' ? linked.kind : ''
output.url = pre + '#' + dmd.anchorName.call(linked, options)
}
}
return output
}