-
Notifications
You must be signed in to change notification settings - Fork 10
/
hbs.js
74 lines (58 loc) · 1.35 KB
/
hbs.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
const hbs = require('handlebars')
const path = require('path');
function isArray(x) {
return x.constructor === Array.prototype.constructor;
}
function print(xs) {
if (isArray(xs)) {
return new hbs.SafeString(xs.join('\n'));
}
return new hbs.SafeString(xs);
}
function split(comment) {
const result = [];
if (comment) {
if (comment.shortText) {
result.push(comment.shortText);
}
if (comment.text) {
const lines = comment.text.split(/\r?\n/);
result.push(...lines);
}
}
return result;
};
hbs.registerHelper('api-title', unit => {
return print(unit.children.map(x => x.name).join(', '));
});
hbs.registerHelper('api-path', file => {
const name = path.basename(file).slice(0, -'.d.ts'.length).replace(/\./g, '-');
return print(`source/api/${name}.json`);
});
hbs.registerHelper('api-description', comment => {
const lines = split(comment);
const result = [];
for (const line of lines) {
if (line[0] === '#') {
break;
}
result.push(line);
}
return print(result);
});
hbs.registerHelper('api-comment', comment => {
const lines = split(comment);
const result = [];
let isComment = false;
for (const line of lines) {
if (!isComment && line[0] === '#') {
isComment = true;
}
if (isComment) {
result.push(line);
}
}
return print(result);
});
hbs.registerHelper('api-order', unit => unit.order);
module.exports = hbs;