/
index.js
183 lines (152 loc) · 5.19 KB
/
index.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
180
181
182
183
var path = require('path');
var scan = require('./scan');
var generateStats = require('./generateStats');
var render = require('./render');
var assign = require('lodash').assign;
var assert = require('assert');
var defaults = require('./config');
var reduceDocuments = require('./reduce');
var EventEmitter = require('events');
/**
* @param {Config} userConfig
*/
function Plugin(userConfig) {
var config = assign({}, defaults, userConfig);
var parserConfig = {
strict: config.strict,
inferModuleIdFromFileName: config.inferModuleIdFromFileName,
customTags: config.customTags,
namespaceDirMap: config.namespaceDirMap,
alias: config.alias,
parse: config.parse,
parserOptions: config.parserOptions,
namedReturnTags: config.namedReturnTags,
nodeAnalyzers: config.nodeAnalyzers || [],
docstringProcessors: config.docstringProcessors || [],
tagProcessors: config.tagProcessors || [],
postProcessors: config.postProcessors || [],
tagAliases: config.tagAliases,
};
var emitter = new EventEmitter();
assert(typeof config.id === 'string',
"You must specify an @id to the megadoc-plugin-js plugin."
);
if (config.builtInTypes) {
config.builtInTypes.forEach(function(x) {
if (x && typeof x === 'object') {
assert(typeof x.name === 'string',
'An object for a built-in type must define a @name string.');
assert(typeof x.href === 'string',
'An object for a built-in type must specify @href with a URL to the built-in type documentation');
}
else {
assert(typeof x === 'string',
'Built-in type must be either a string or an object, not "' + typeof x + '"');
}
});
}
Object.keys(config.alias).forEach(function(key) {
assert(Array.isArray(config.alias[key]),
"megadoc-plugin-js: OptionError: expected alias '" + key + "' entry to " +
" be an array, got '" + typeof config.alias[key] + "'."
);
config.alias[key].forEach(function(value) {
assert(typeof value === 'string',
"megadoc-plugin-js: OptionError: expected alias entry to be a string " +
", not '" + typeof value + "' (key '" + key + "')"
);
});
});
var plugin = {
id: config.id,
name: 'megadoc-plugin-js',
/**
* Add a custom-tag definition.
*
* @param {String} tagName
* @param {Object} definition
*
* @param {String[]} definition.attributes
* A whitelist of all the custom attributes the tag defines.
*
* Attempting to define an attribute that is not listed here
* will raise an error.
*
* @param {Boolean} [definition.withTypeInfo=false]
* Whether the tag is expected to have type info, like:
*
* @tag {types} [name=defaultValue]
*/
defineCustomTag: function(tagName, definition) {
assert(!parserConfig.customTags.hasOwnProperty(tagName),
"Tag '" + tagName + "' already has a definition!"
);
parserConfig.customTags[tagName] = definition;
},
addNodeAnalyzer: function(analyzer) {
emitter.on('process-node', analyzer);
// parserConfig.nodeAnalyzers.push(analyzer);
},
addDocstringProcessor: function(processor) {
emitter.on('process-docstring', processor);
// parserConfig.docstringProcessors.push(processor);
},
addTagProcessor: function(processor) {
emitter.on('process-tag', processor);
// parserConfig.tagProcessors.push(processor);
},
addPostProcessor: function(fn) {
emitter.on('postprocess', fn);
// parserConfig.postProcessors.push(postProcessor);
},
on: emitter.addListener.bind(emitter),
off: emitter.removeListener.bind(emitter),
run: function(compiler) {
var database;
var documents;
compiler.on('scan', function(done) {
scan({
config: config,
parserConfig: parserConfig,
utils: compiler.utils,
assetRoot: compiler.config.assetRoot,
emitter: emitter,
}, function(err, _documents) {
if (err) {
return done(err);
}
documents = _documents;
database = reduceDocuments({
documents: documents,
namespaceId: config.id,
namespaceTitle: config.title || config.corpusContext,
baseURL: config.url,
config: config
});
compiler.corpus.add(database);
done();
});
});
compiler.on('render', function(renderMarkdown, linkify, done) {
render(compiler, database, renderMarkdown, linkify, config);
done();
});
compiler.on('generateStats', function(stats, done) {
stats['js:' + config.id] = generateStats(database);
done();
});
compiler.on('write', function(done) {
compiler.assets.addStyleSheet(
path.resolve(__dirname, '..', 'ui', 'css', 'index.less')
);
compiler.assets.addPluginScript(
path.resolve(__dirname, '..', 'dist', plugin.name + '.js')
);
// compiler.assets.addPluginRuntimeConfig(plugin.name, config);
done();
});
}
};
return plugin;
}
module.exports = Plugin;