-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
62 lines (53 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const express = require('express'),
app = express(),
fs = require('fs'),
showdown = require('showdown'),
converter = new showdown.Converter()
app.set('view engine', 'ejs')
app.get('/favicon.ico', (req, res) => {
res.end()
})
app.use('/static', express.static('static'))
function error(res) {
let md = fs.readFileSync(`./views/error.md`).toString()
res.render('page', {
md: converter.makeHtml(md),
dir: '404',
title: '404',
files: []
})
}
app.get('/', (req, res) => {
res.redirect('/home')
})
app.get('/:id', (req, res) => {
try {
let md = fs.readFileSync(`./pages/${req.params.id}.md`).toString()
md = converter.makeHtml(md)
let files = fs.readdirSync('./pages')
var file = [
...files.map((file) => {
let name = file.split('.')[0]
return {
name: name,
url: `/${name}`
}
}),
]
res.render('page', {
md: md,
dir: req.params.id,
title: md.split('\n')[0].split('>')[1].split('<')[0],
files: file
})
} catch (err) {
// console.log(err)
error(res)
}
})
app.get('*', (req, res) => {
error(res)
})
app.listen(3000, () => {
console.log('listening on port 3000')
})