-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathdocs.js
22 lines (20 loc) · 797 Bytes
/
docs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import path from 'path';
import { existsSync, readFileSync } from 'fs';
import '../utils/marked.min.js';
export default (fastify, options) => {
fastify.get('/docs/*', async (request, reply) => {
const fullPath = path.resolve(options.docsDir, request.params['*']);
if (!fullPath.startsWith(options.docsDir) || !existsSync(fullPath)) {
reply.status(403).send('<h1>403 Forbidden</h1>');
return;
}
const ext = path.extname(fullPath).toLowerCase();
if (ext === '.md') {
const markdownContent = readFileSync(fullPath, 'utf8');
const htmlContent = marked.parse(markdownContent);
reply.type('text/html').send(htmlContent);
} else {
reply.sendFile(fullPath);
}
});
};