misskey/src/server/web/docs.ts

108 lines
2.6 KiB
TypeScript
Raw Normal View History

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