forked from h5bp/ant-build-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown.js
101 lines (87 loc) · 3.01 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
@overview Translate doclet descriptions from MarkDown into HTML.
@module plugins/markdown
@author Michael Mathews <[email protected]>
@author Ben Blank <[email protected]>
*/
var conf = env.conf.markdown;
var defaultTags = [ "classdesc", "description", "params", "properties", "returns" ];
var parse;
var tags;
/**
Pull in the selected parser and wrap it in a common interface.
@param {String} parser The name of the selected parser.
@param {Object} [conf] Configuration for the selected parser, if any.
@returns {Function} A function which accepts markdown source, feeds it to
the selected parser, and returns the resulting HTML.
@throws {Exception} If the name does not correspond to a known parser.
*/
function getParser(parser, conf) {
conf = conf || {};
if (parser === "gfm") {
parser = new (require("gfm/showdown").Converter)();
parser.githubRepoOwner = conf.githubRepoOwner;
parser.githubRepoName = conf.githubRepoName;
parser.hardwrap = !!conf.hardwrap;
return function(source) {
return parser.makeHtml(source);
};
} else if (parser === "evilstreak") {
parser = require("evilstreak/markdown");
return function(source) {
return parser.renderJsonML(parser.toHTMLTree(source, conf.dialect));
}
} else {
throw "unknown Markdown parser: '" + parser + "'";
}
}
/**
Process the markdown source in a doclet. The properties which should be
processed are configurable, but always include "classdesc", "description",
"params", "properties", and "returns". Handled properties can be bare
strings, objects, or arrays of objects.
*/
function process(doclet) {
tags.forEach(function(tag) {
if (!doclet.hasOwnProperty(tag)) {
return;
}
if (typeof doclet[tag] === "string") {
doclet[tag] = parse(doclet[tag]);
} else if (doclet[tag] instanceof Array) {
doclet[tag].forEach(process);
} else if (doclet[tag]) {
process(doclet[tag]);
}
});
}
// determine which parser should be used based on configuration options, if any
if (conf && conf.parser) {
parse = getParser(conf.parser, conf);
} else if (conf && conf.githubRepoOwner && conf.githubRepoName) {
// use GitHub-friendly parser if GitHub-specific options are present
parse = getParser("gfm", conf);
} else {
// evilstreak is the default parser
parse = getParser("evilstreak", conf);
}
// set up the list of "tags" (properties) to process
if (conf && conf.tags) {
tags = conf.tags.slice();
defaultTags.forEach(function(tag) {
if (tags.indexOf(tag) === -1) {
tags.push(tag);
}
});
} else {
tags = defaultTags;
}
exports.handlers = {
/**
Translate markdown syntax in a new doclet's description into HTML. Is run
by JSDoc 3 whenever a "newDoclet" event fires.
*/
newDoclet: function(e) {
process(e.doclet);
}
};