-
Notifications
You must be signed in to change notification settings - Fork 7
/
markdown.js
84 lines (68 loc) · 1.82 KB
/
markdown.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
'use strict';
var marked = require('marked');
module.exports = function (ctx) {
if (ctx.package && ctx.package.description) {
ctx.package.htmlDescription = marked(ctx.package.description);
}
/**
* Wrapper for `marked` that takes only one argument to avoid
* problem with `map` additional arguments.
*/
function md(str) {
return marked(str);
}
/**
* Return a function that will apply `fn` on `obj[key]` to generate
* `obj[newKey]`.
*/
function applyKey(fn, key, newKey) {
return function (obj) {
if (key in obj) {
obj[newKey] = fn(obj[key]);
}
return obj;
};
}
for (var type in ctx.data) {
for (var name in ctx.data[type]) {
var item = ctx.data[type][name];
if ('description' in item) {
item.htmlDescription = marked(item.description);
}
if ('author' in item) {
item.htmlAuthor = item.author.map(md);
}
if ('throws' in item) {
item.htmlThrows = item.throws.map(md);
}
if ('todo' in item) {
item.htmlTodo = item.todo.map(md);
}
if ('returns' in item) {
item.htmlReturns = item.returns.map(
applyKey(md, 'description', 'htmlDescription')
);
}
if ('example' in item) {
item.example = item.example.map(
applyKey(md, 'description', 'htmlDescription')
);
}
if ('parameters' in item) {
item.parameters = item.parameters.map(
applyKey(md, 'description', 'htmlDescription')
);
}
if ('prop' in item) {
item.prop = item.prop.map(
applyKey(md, 'description', 'htmlDescription')
);
}
if ('content' in item) {
item.content = item.content.map(
applyKey(md, 'description', 'htmlDescription')
);
}
}
}
};