-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathdocs.ts
251 lines (204 loc) · 6.36 KB
/
docs.ts
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
* Docs
*/
import * as fs from 'fs';
import * as path from 'path';
import * as showdown from 'showdown';
import 'showdown-highlightjs-extension';
import ms = require('ms');
import * as Router from 'koa-router';
import * as send from 'koa-send';
import { Context, ObjectContext } from 'cafy';
import * as glob from 'glob';
import * as yaml from 'js-yaml';
import config from '../../config';
import { licenseHtml } from '../../misc/license';
const constants = require('../../const.json');
import endpoints from '../api/endpoints';
const locales = require('../../../locales');
const nestedProperty = require('nested-property');
async function genVars(lang: string): Promise<{ [key: string]: any }> {
const vars = {} as { [key: string]: any };
vars['lang'] = lang;
const cwd = path.resolve(__dirname + '/../../../') + '/';
vars['endpoints'] = endpoints;
const entities = glob.sync('src/docs/api/entities/**/*.yaml', { cwd });
vars['entities'] = entities.map(x => {
const _x = yaml.safeLoad(fs.readFileSync(cwd + x, 'utf-8'));
return _x.name;
});
const docs = glob.sync(`src/docs/**/*.${lang}.md`, { cwd });
vars['docs'] = {};
for (const x of docs) {
const [, name] = x.match(/docs\/(.+?)\.(.+?)\.md$/);
if (vars['docs'][name] == null) {
vars['docs'][name] = {
name,
title: {}
};
}
vars['docs'][name]['title'][lang] = fs.readFileSync(cwd + x, 'utf-8').match(/^# (.+?)\r?\n/)[1];
}
vars['kebab'] = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
vars['config'] = config;
vars['copyright'] = constants.copyright;
vars['license'] = licenseHtml;
vars['i18n'] = (key: string) => nestedProperty.get(locales[lang], key);
return vars;
}
// WIP type
const parseParamDefinition = (key: string, x: any) => {
return Object.assign({
name: key,
type: x.validator.getType()
}, x);
};
const parsePropDefinition = (key: string, prop: any) => {
const id = prop.type.match(/^id\((.+?)\)|^id/);
const entity = prop.type.match(/^entity\((.+?)\)/);
const isObject = /^object/.test(prop.type);
const isDate = /^date/.test(prop.type);
const isArray = /\[\]$/.test(prop.type);
if (id) {
prop.kind = 'id';
prop.type = 'string';
prop.entity = id[1];
if (isArray) {
prop.type += '[]';
}
}
if (entity) {
prop.kind = 'entity';
prop.type = 'object';
prop.entity = entity[1];
if (isArray) {
prop.type += '[]';
}
}
if (isObject) {
prop.kind = 'object';
if (prop.props) {
prop.hasDef = true;
}
}
if (isDate) {
prop.kind = 'date';
prop.type = 'string';
if (isArray) {
prop.type += '[]';
}
}
if (prop.optional) {
prop.type += '?';
}
prop.name = key;
return prop;
};
const sortParams = (params: Array<{ name: string }>) => {
return params;
};
// WIP type
const extractParamDefRef = (params: Context[]) => {
let defs: any[] = [];
for (const param of params) {
if (param.data && param.data.ref) {
const props = (param as ObjectContext<any>).props;
defs.push({
name: param.data.ref,
params: sortParams(Object.keys(props).map(k => parseParamDefinition(k, props[k])))
});
const childDefs = extractParamDefRef(Object.keys(props).map(k => props[k]));
defs = defs.concat(childDefs);
}
}
return sortParams(defs);
};
const extractPropDefRef = (props: any[]) => {
let defs: any[] = [];
for (const [k, v] of Object.entries(props)) {
if (v.props) {
defs.push({
name: k,
props: sortParams(Object.entries(v.props).map(([k, v]) => parsePropDefinition(k, v)))
});
const childDefs = extractPropDefRef(v.props);
defs = defs.concat(childDefs);
}
}
return sortParams(defs);
};
const router = new Router();
router.get('/assets/*', async ctx => {
await send(ctx, ctx.params[0], {
root: `${__dirname}/../../docs/assets/`,
maxage: ms('1 days')
});
});
router.get('/*/api/endpoints/*', async ctx => {
const lang = ctx.params[0];
const name = ctx.params[1];
const ep = endpoints.find(e => e.name === name);
const vars = {
id: `api/endpoints/${name}`,
title: name,
endpoint: ep.meta,
endpointUrl: {
host: config.api_url,
path: name
},
// @ts-ignore
params: ep.meta.params ? sortParams(Object.entries(ep.meta.params).map(([k, v]) => parseParamDefinition(k, v))) : null,
paramDefs: ep.meta.params ? extractParamDefRef(Object.values(ep.meta.params).map(x => x.validator)) : null,
res: ep.meta.res,
resProps: ep.meta.res && ep.meta.res.props ? sortParams(Object.entries(ep.meta.res.props).map(([k, v]) => parsePropDefinition(k, v))) : null,
resDefs: null as any, //extractPropDefRef(Object.entries(ep.res.props).map(([k, v]) => parsePropDefinition(k, v)))
src: `https://github.com/syuilo/misskey/tree/master/src/server/api/endpoints/${name}.ts`
};
await ctx.render('../../../../src/docs/api/endpoints/view', Object.assign(await genVars(lang), vars));
ctx.set('Cache-Control', 'public, max-age=300');
});
router.get('/*/api/entities/*', async ctx => {
const lang = ctx.params[0];
const entity = ctx.params[1];
const x = yaml.safeLoad(fs.readFileSync(path.resolve(`${__dirname}/../../../src/docs/api/entities/${entity}.yaml`), 'utf-8'));
await ctx.render('../../../../src/docs/api/entities/view', Object.assign(await genVars(lang), {
id: `api/entities/${entity}`,
name: x.name,
desc: x.desc,
props: sortParams(Object.entries(x.props).map(([k, v]) => parsePropDefinition(k, v))),
propDefs: extractPropDefRef(x.props)
}));
ctx.set('Cache-Control', 'public, max-age=300');
});
router.get('/*/*', async ctx => {
const lang = ctx.params[0];
const doc = ctx.params[1];
showdown.extension('urlExtension', () => ({
type: 'output',
regex: /%URL%/g,
replace: config.url
}));
showdown.extension('wsUrlExtension', () => ({
type: 'output',
regex: /%WS_URL%/g,
replace: config.ws_url
}));
showdown.extension('apiUrlExtension', () => ({
type: 'output',
regex: /%API_URL%/g,
replace: config.api_url
}));
const conv = new showdown.Converter({
tables: true,
extensions: ['urlExtension', 'apiUrlExtension', 'highlightjs']
});
const md = fs.readFileSync(`${__dirname}/../../../src/docs/${doc}.${lang}.md`, 'utf8');
await ctx.render('../../../../src/docs/article', Object.assign({
id: doc,
html: conv.makeHtml(md),
title: md.match(/^# (.+?)\r?\n/)[1],
src: `https://github.com/syuilo/misskey/tree/master/src/docs/${doc}.${lang}.md`
}, await genVars(lang)));
ctx.set('Cache-Control', 'public, max-age=300');
});
export default router;