Skip to content

Commit 234bc27

Browse files
authored
Fix mdserve crash on non-existing files
1 parent 3fccbd4 commit 234bc27

2 files changed

Lines changed: 45 additions & 44 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@elementbound/mdview",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "View markdown files from your terminal!",
55
"keywords": [
66
"markdown",

src/routes/render.js

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,44 @@
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

Comments
 (0)