|
1 | | -const fs = require('fs') |
2 | | -const fsp = fs.promises |
3 | | -const path = require('path') |
4 | | -const express = require('express') |
5 | | - |
6 | | -const config = require('../config') |
7 | | -const transformMarkdown = require('../transforms/markdown') |
8 | | -const processImages = require('../postprocess/image') |
9 | | - |
10 | | -const router = express.Router() |
11 | | - |
12 | | -router.get('/*', async (req, res) => { |
13 | | - const file = req.path |
14 | | - const basedir = path.dirname(file) |
15 | | - console.log('Rendering', file) |
16 | | - |
17 | | - if (!fs.existsSync(file)) { |
18 | | - res.status(404) |
19 | | - .send(`File doesn't exist: ${file}`) |
20 | | - } |
21 | | - |
22 | | - const data = await fsp.readFile(file, 'utf-8') |
23 | | - const extension = path.extname(file) |
24 | | - |
25 | | - if (config.extensions.includes(extension)) { |
26 | | - let html = transformMarkdown(data) |
27 | | - |
28 | | - html = config.renderImages |
29 | | - ? await processImages(html, basedir) |
30 | | - : html |
31 | | - |
32 | | - res.status(200) |
33 | | - .send(html) |
34 | | - } else { |
35 | | - console.log(`Unknown extension ${extension}, passing through`) |
36 | | - |
37 | | - res.status(200) |
38 | | - .contentType('text/plain') |
39 | | - .send(`${data}`) |
40 | | - } |
41 | | -}) |
42 | | - |
43 | | -module.exports = router |
| 1 | +const fs = require('fs') |
| 2 | +const fsp = fs.promises |
| 3 | +const path = require('path') |
| 4 | +const express = require('express') |
| 5 | + |
| 6 | +const config = require('../config') |
| 7 | +const transformMarkdown = require('../transforms/markdown') |
| 8 | +const processImages = require('../postprocess/image') |
| 9 | + |
| 10 | +const router = express.Router() |
| 11 | + |
| 12 | +router.get('/*', async (req, res) => { |
| 13 | + const file = req.path |
| 14 | + const basedir = path.dirname(file) |
| 15 | + console.log('Rendering', file) |
| 16 | + |
| 17 | + if (!fs.existsSync(file)) { |
| 18 | + res.status(404) |
| 19 | + .send(`File doesn't exist: ${file}`) |
| 20 | + return |
| 21 | + } |
| 22 | + |
| 23 | + const data = await fsp.readFile(file, 'utf-8') |
| 24 | + const extension = path.extname(file) |
| 25 | + |
| 26 | + if (config.extensions.includes(extension)) { |
| 27 | + let html = transformMarkdown(data) |
| 28 | + |
| 29 | + html = config.renderImages |
| 30 | + ? await processImages(html, basedir) |
| 31 | + : html |
| 32 | + |
| 33 | + res.status(200) |
| 34 | + .send(html) |
| 35 | + } else { |
| 36 | + console.log(`Unknown extension ${extension}, passing through`) |
| 37 | + |
| 38 | + res.status(200) |
| 39 | + .contentType('text/plain') |
| 40 | + .send(`${data}`) |
| 41 | + } |
| 42 | +}) |
| 43 | + |
| 44 | +module.exports = router |
0 commit comments