forked from Zambonilli/node-apidoc-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·89 lines (74 loc) · 2.12 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
#!/usr/bin/env node
"use strict";
var fs = require('fs'),
ejs = require('ejs'),
_ = require('underscore');
var argv = require('optimist')
.usage('Generate documentation from apidoc data.\nUsage: apidoc-markdown -p [path] -o [output file]')
.demand(['path', 'output'])
.alias({
'path': 'p',
'output': 'o',
'template': 't'
})
.describe({
'path': 'Path to generated apidoc output. Where api_data.json & api_project.json resides.',
'output': 'Output file to write.',
'template': 'Path to EJS template file, if not specified default template will be used.',
'prepend': 'Prepend file after TOC.'
}).argv;
ejs.filters.undef = function (obj) {
return obj ? obj : '';
};
ejs.filters.mlink = function (obj) {
return (obj || '').toLowerCase().replace(/\s/g, '-').replace(/\;|\/|\?|:|@|&|=|\+|\$|\,/g, '');
};
ejs.filters.badge = function (t) {
var type = t ? t.toUpperCase() : 'GET';
var img = 'https://img.shields.io/badge/method-' + type + '-';
switch (type) {
case 'GET':
img += 'brightgreen';
break;
case 'POST':
img += 'orange';
break;
case 'PUT':
img += 'blue';
break;
case 'PATCH':
img += '00aeef';
break;
case 'DELETE':
img += 'red';
break;
default:
img += 'blue';
break;
}
img += '.svg';
return img;
};
var tmplFile = argv.template ? argv.template : __dirname + '/templates/github.md',
apiData = JSON.parse(fs.readFileSync(argv.path + '/api_data.json')),
projData = JSON.parse(fs.readFileSync(argv.path + '/api_project.json')),
template = ejs.compile(fs.readFileSync(tmplFile).toString());
apiData = _.filter(apiData, function (entry) {
return entry.type;
});
var apiByGroup = _.groupBy(apiData, function (entry) {
return entry.group;
});
var apiByGroupAndName = {};
Object.keys(apiByGroup).forEach(function (key) {
apiByGroupAndName[key] = _.groupBy(apiByGroup[key], function (entry) {
return entry.name;
});
});
var data = {
project: projData,
data: apiByGroupAndName
};
data.prepend = argv.prepend ? fs.readFileSync(argv.prepend).toString() : null;
fs.writeFileSync(argv.output, template(data));
console.log('Wrote apidoc-markdown template output to: ' + argv.output);