-
Notifications
You must be signed in to change notification settings - Fork 485
/
Copy pathhtml_helpers.js
179 lines (164 loc) · 4.96 KB
/
html_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict';
var slugg = require('slugg'),
getGlobalExternalLink = require('globals-docs').getDoc,
mdast = require('mdast'),
html = require('mdast-html'),
inlineLex = require('jsdoc-inline-lex');
/**
* Make slugg a unary so we can use it in functions
*
* @private
* @param {string} input text
* @returns {string} output
*/
function slug(input) {
return input ? slugg(input) : '';
}
/**
* Format a description and target as a Markdown link.
*
* @param {string} description the text seen as the link
* @param {string} href where the link goes
* @return {string} markdown formatted link
*/
function markdownLink(description, href) {
return '[`' + description + '`](' + href + ')';
}
/**
* Format link & tutorial tags with simple code inline tags.
*
* @param {string} text input - typically a description
* @returns {string} markdown-friendly output
* @private
* @example
* formatInlineTags('{@link Foo}'); // "[Foo](#foo)"
*/
function formatInlineTags(text) {
var output = '';
var tokens = inlineLex(text);
for (var i = 0; i < tokens.length; i++) {
if (tokens[i].type === 'text') {
output += tokens[i].capture[0];
} else if (tokens[i].type === 'link') {
var parts = tokens[i].capture[1].split(/\s|\|/);
if (parts.length === 1) {
output += markdownLink(tokens[i].capture[1], slug(tokens[i].capture[1]));
} else {
output += markdownLink(parts.slice(1).join(' '), slug(parts[0]));
}
} else if (tokens[i].type === 'prefixLink') {
output += markdownLink(tokens[i].capture[1], slug(tokens[i].capture[2]));
}
}
return output;
}
/**
* Link text to this page or to a central resource.
* @param {Array<string>} paths list of valid namespace paths that are linkable
* @param {string} text inner text of the link
* @returns {string} potentially linked HTML
*/
function autolink(paths, text) {
if (paths.indexOf(slug(text)) !== -1) {
return '<a href="#' + slug(text) + '">' + text + '</a>';
} else if (getGlobalExternalLink(text)) {
return '<a href="' + getGlobalExternalLink(text) + '">' + text + '</a>';
}
return text;
}
/**
* Helper used to format JSDoc-style type definitions into HTML.
*
* @name formatType
* @param {Object} type type object in doctrine style
* @param {Array<string>} paths valid namespace paths that can be linked
* @returns {string} string
* @example
* var x = { type: 'NameExpression', name: 'String' };
* // in template
* // {{ type x }}
* // generates String
*/
function formatType(type, paths) {
if (!type) {
return '';
}
function recurse(element) {
return formatType(element, paths);
}
switch (type.type) {
case 'NameExpression':
return '<code>' + autolink(paths, type.name) + '</code>';
case 'UnionType':
return type.elements.map(recurse).join(' or ');
case 'AllLiteral':
return 'Any';
case 'OptionalType':
return '<code>[' + formatType(type.expression, paths) + ']</code>';
case 'TypeApplication':
return formatType(type.expression, paths) + '<' +
type.applications.map(recurse).join(', ') + '>';
case 'UndefinedLiteral':
return 'undefined';
}
}
/**
* Format a parameter name. This is used in formatParameters
* and just needs to be careful about differentiating optional
* parameters
*
* @param {Object} param a param as a type spec
* @returns {string} formatted parameter representation.
*/
function formatParameter(param) {
return (param.type && param.type.type === 'OptionalType') ?
'[' + param.name + ']' : param.name;
}
/**
* Format the parameters of a function into a quickly-readable
* summary that resembles how you would call the function
* initially.
*
* @returns {string} formatted parameters
*/
function formatParameters() {
if (!this.params) {
return '';
}
return '(' + this.params.map(function (param) {
return formatParameter(param);
}).join(', ') + ')';
}
/**
* Given a Handlebars instance, register helpers
*
* @param {Object} Handlebars template instance
* @param {Array<string>} paths list of valid namespace paths that are linkable
* @returns {undefined} invokes side effects on Handlebars
*/
module.exports = function (Handlebars, paths) {
Handlebars.registerHelper('permalink', function () {
return this.path.map(slug).join('/');
});
Handlebars.registerHelper('autolink', autolink.bind(autolink, paths));
Handlebars.registerHelper('format_params', formatParameters);
/**
* This helper is exposed in templates as `md` and is useful for showing
* Markdown-formatted text as proper HTML.
*
* @name formatMarkdown
* @param {string} string
* @returns {string} string
* @example
* var x = '## foo';
* // in template
* // {{ md x }}
* // generates <h2>foo</h2>
*/
Handlebars.registerHelper('md', function formatMarkdown(string) {
return new Handlebars.SafeString(mdast().use(html).process(formatInlineTags(string)));
});
Handlebars.registerHelper('format_type', function (type) {
return formatType(type, paths);
});
};