2017-12-16 09:41:22 -07:00
|
|
|
/**
|
2018-04-12 15:06:18 -06:00
|
|
|
* Docs
|
2017-12-16 09:41:22 -07:00
|
|
|
*/
|
|
|
|
|
2018-07-06 05:27:48 -06:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
2018-07-15 03:28:08 -06:00
|
|
|
import * as showdown from 'showdown';
|
2018-07-16 10:11:36 -06:00
|
|
|
import 'showdown-highlightjs-extension';
|
2018-04-12 21:05:24 -06:00
|
|
|
import ms = require('ms');
|
2018-04-12 15:06:18 -06:00
|
|
|
import * as Router from 'koa-router';
|
|
|
|
import * as send from 'koa-send';
|
2018-07-06 05:27:48 -06:00
|
|
|
import * as glob from 'glob';
|
2018-07-05 11:58:29 -06:00
|
|
|
import config from '../../config';
|
2018-07-07 12:13:20 -06:00
|
|
|
import { licenseHtml } from '../../misc/license';
|
2019-02-03 09:47:12 -07:00
|
|
|
import { copyright } from '../../const.json';
|
2019-02-11 06:37:20 -07:00
|
|
|
import * as locales from '../../../locales';
|
2019-02-01 05:08:49 -07:00
|
|
|
import * as nestedProperty from 'nested-property';
|
2017-12-16 09:41:22 -07:00
|
|
|
|
2019-02-02 00:16:31 -07:00
|
|
|
function getLang(lang: string): string {
|
|
|
|
if (['en-US', 'ja-JP'].includes(lang)) {
|
|
|
|
return lang;
|
|
|
|
} else {
|
|
|
|
return 'en-US';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 05:27:48 -06:00
|
|
|
async function genVars(lang: string): Promise<{ [key: string]: any }> {
|
|
|
|
const vars = {} as { [key: string]: any };
|
|
|
|
|
|
|
|
vars['lang'] = lang;
|
|
|
|
|
2018-07-15 04:35:20 -06:00
|
|
|
const cwd = path.resolve(__dirname + '/../../../') + '/';
|
2018-07-15 04:29:15 -06:00
|
|
|
|
2018-07-15 07:00:05 -06:00
|
|
|
const docs = glob.sync(`src/docs/**/*.${lang}.md`, { cwd });
|
2018-07-06 05:27:48 -06:00
|
|
|
vars['docs'] = {};
|
2018-12-11 04:36:55 -07:00
|
|
|
for (const x of docs) {
|
2018-07-15 07:00:05 -06:00
|
|
|
const [, name] = x.match(/docs\/(.+?)\.(.+?)\.md$/);
|
2018-07-06 05:27:48 -06:00
|
|
|
if (vars['docs'][name] == null) {
|
|
|
|
vars['docs'][name] = {
|
|
|
|
name,
|
|
|
|
title: {}
|
|
|
|
};
|
|
|
|
}
|
2018-07-15 04:35:20 -06:00
|
|
|
vars['docs'][name]['title'][lang] = fs.readFileSync(cwd + x, 'utf-8').match(/^# (.+?)\r?\n/)[1];
|
2018-12-11 04:36:55 -07:00
|
|
|
}
|
2018-07-06 05:27:48 -06:00
|
|
|
|
|
|
|
vars['kebab'] = (string: string) => string.replace(/([a-z])([A-Z])/g, '$1-$2').replace(/\s+/g, '-').toLowerCase();
|
|
|
|
|
|
|
|
vars['config'] = config;
|
|
|
|
|
2019-02-03 09:47:12 -07:00
|
|
|
vars['copyright'] = copyright;
|
2018-07-06 05:27:48 -06:00
|
|
|
|
|
|
|
vars['license'] = licenseHtml;
|
|
|
|
|
2018-11-09 06:02:48 -07:00
|
|
|
vars['i18n'] = (key: string) => nestedProperty.get(locales[lang], key);
|
2018-07-06 05:27:48 -06:00
|
|
|
|
|
|
|
return vars;
|
|
|
|
}
|
|
|
|
|
2018-04-12 15:06:18 -06:00
|
|
|
const router = new Router();
|
2017-12-16 09:41:22 -07:00
|
|
|
|
2018-04-12 21:05:24 -06:00
|
|
|
router.get('/assets/*', async ctx => {
|
2019-01-22 05:42:05 -07:00
|
|
|
await send(ctx as any, ctx.params[0], {
|
2018-07-15 03:28:08 -06:00
|
|
|
root: `${__dirname}/../../docs/assets/`,
|
2018-09-18 23:22:46 -06:00
|
|
|
maxage: ms('1 days')
|
2018-04-12 21:05:24 -06:00
|
|
|
});
|
2018-04-12 15:06:18 -06:00
|
|
|
});
|
2017-12-16 09:41:22 -07:00
|
|
|
|
2018-07-06 08:52:47 -06:00
|
|
|
router.get('/*/*', async ctx => {
|
2019-02-02 00:16:31 -07:00
|
|
|
const lang = getLang(ctx.params[0]);
|
2018-07-06 08:52:47 -06:00
|
|
|
const doc = ctx.params[1];
|
|
|
|
|
2018-07-15 04:05:19 -06:00
|
|
|
showdown.extension('urlExtension', () => ({
|
|
|
|
type: 'output',
|
|
|
|
regex: /%URL%/g,
|
|
|
|
replace: config.url
|
|
|
|
}));
|
|
|
|
|
2018-10-29 04:11:01 -06:00
|
|
|
showdown.extension('wsUrlExtension', () => ({
|
|
|
|
type: 'output',
|
|
|
|
regex: /%WS_URL%/g,
|
|
|
|
replace: config.ws_url
|
|
|
|
}));
|
|
|
|
|
2018-07-15 04:05:19 -06:00
|
|
|
showdown.extension('apiUrlExtension', () => ({
|
|
|
|
type: 'output',
|
|
|
|
regex: /%API_URL%/g,
|
|
|
|
replace: config.api_url
|
|
|
|
}));
|
|
|
|
|
|
|
|
const conv = new showdown.Converter({
|
|
|
|
tables: true,
|
2018-07-16 10:11:36 -06:00
|
|
|
extensions: ['urlExtension', 'apiUrlExtension', 'highlightjs']
|
2018-07-15 04:05:19 -06:00
|
|
|
});
|
2018-07-15 03:28:08 -06:00
|
|
|
const md = fs.readFileSync(`${__dirname}/../../../src/docs/${doc}.${lang}.md`, 'utf8');
|
|
|
|
|
|
|
|
await ctx.render('../../../../src/docs/article', Object.assign({
|
2018-07-30 01:24:46 -06:00
|
|
|
id: doc,
|
2018-07-15 06:48:57 -06:00
|
|
|
html: conv.makeHtml(md),
|
2018-07-16 12:57:34 -06:00
|
|
|
title: md.match(/^# (.+?)\r?\n/)[1],
|
|
|
|
src: `https://github.com/syuilo/misskey/tree/master/src/docs/${doc}.${lang}.md`
|
2018-07-15 03:28:08 -06:00
|
|
|
}, await genVars(lang)));
|
2018-11-19 13:29:51 -07:00
|
|
|
|
|
|
|
ctx.set('Cache-Control', 'public, max-age=300');
|
2018-07-06 08:52:47 -06:00
|
|
|
});
|
|
|
|
|
2018-04-12 21:05:24 -06:00
|
|
|
export default router;
|