forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
146 lines (127 loc) · 3.65 KB
/
api.js
File metadata and controls
146 lines (127 loc) · 3.65 KB
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
'use strict';
/*!
* Module dependencies
*/
const _ = require('lodash');
const dox = require('dox');
const fs = require('fs');
const link = require('../helpers/linktype');
const hl = require('highlight.js');
const md = require('markdown');
const files = [
'lib/index.js',
'lib/schema.js',
'lib/connection.js',
'lib/document.js',
'lib/model.js',
'lib/query.js',
'lib/cursor/QueryCursor.js',
'lib/aggregate.js',
'lib/cursor/AggregationCursor.js',
'lib/schematype.js',
'lib/virtualtype.js',
'lib/error/index.js'
];
module.exports = {
docs: [],
github: 'https://github.com/Automattic/mongoose/blob/',
title: 'API docs'
};
const out = module.exports.docs;
let combinedFiles = [];
for (const file of files) {
const comments = dox.parseComments(fs.readFileSync(`./${file}`, 'utf8'));
comments.file = file;
combinedFiles.push(comments);
}
parse();
function parse() {
for (const props of combinedFiles) {
const data = {
name: _.capitalize(props.file.replace('lib/', '').replace('.js', '').replace('/index', '')),
props: []
};
for (const prop of props) {
if (prop.ignore || prop.isPrivate) {
continue;
}
const ctx = prop.ctx || {};
for (const tag of prop.tags) {
switch (tag.type) {
case 'receiver':
ctx.constructor = tag.string;
break;
case 'property':
ctx.type = 'property';
ctx.name = tag.string;
ctx.string = `${ctx.constructor}.prototype.${ctx.name}`;
break;
case 'static':
ctx.type = 'property';
ctx.static = true;
ctx.name = tag.string;
ctx.string = `${ctx.constructor}.${ctx.name}`;
break;
case 'return':
ctx.return = tag;
break;
case 'inherits':
ctx[tag.type] = tag.string;
break;
case 'event':
case 'param':
ctx[tag.type] = (ctx[tag.type] || []);
if (tag.types) {
tag.types = tag.types.join('|');
}
ctx[tag.type].push(tag);
tag.description = tag.description ?
md.parse(tag.description).replace(/^<p>/, '').replace(/<\/p>$/, '') :
'';
break;
case 'method':
ctx.type = 'method';
ctx.name = tag.string;
ctx.string = `${ctx.constructor}.prototype.${ctx.name}()`;
break;
case 'memberOf':
ctx.constructor = tag.parent;
ctx.string = `${ctx.constructor}.prototype.${ctx.name}`;
if (ctx.type === 'method') {
ctx.string += '()';
}
break;
}
}
console.log(ctx);
// Backwards compat
if (typeof ctx.constructor === 'string') {
ctx.anchorId = `${ctx.constructor.toLowerCase()}_${ctx.constructor}-${ctx.name}`;
} else {
ctx.anchorId = `${ctx.name.toLowerCase()}_${ctx.name}`;
}
ctx.description = prop.description.full.
replace(/<br \/>/ig, ' ').
replace(/>/i, '>');
ctx.description = highlight(ctx.description);
data.props.push(ctx);
}
data.props.sort(function(a, b) {
if (a.string < b.string) {
return -1;
} else {
return 1;
}
});
out.push(data);
}
}
function highlight(str) {
return str.replace(/(<pre><code>)([^<]+)(<\/code)/gm, function (_, $1, $2, $3) {
const code = /^(?:`{3}([^\n]+)\n)?([\s\S]*)/gm.exec($2);
if ('js' === code[1] || !code[1]) {
code[1] = 'javascript';
}
return $1 + hl.highlight(code[1], code[2]).value.trim() + $3;
});
}