-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent-server.js
173 lines (167 loc) · 6.34 KB
/
content-server.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const express = require('express')
const fs = require('fs')
const chokidar = require('chokidar')
const path = require('path')
const MDProvider = require('./md-provider')
class ContentServer {
constructor(container) {
this.container = container
this.pluginFiles = MDProvider.pluginFiles
this.basePath = global.slidePath
this.init()
this.createServer()
}
createServer() {
const router = express.Router()
router.use('/:uuid', (req, res, next) => {
if (!this.container.has(req.params.uuid)) {
res.sendStatus(404)
} else {
if (req.path == '/') {
res.render(
'index.ejs',
Object.assign(
{
title: this.container.get(req.params.uuid)
.provider.meta.title,
content: this.container
.get(req.params.uuid)
.provider.get(),
config: JSON.stringify(
this.container.get(req.params.uuid).provider
.meta.config
),
uuid: JSON.stringify(req.params.uuid),
},
MDProvider.env
)
)
} else if (req.path == '/index.md') {
res.set('Content-Type', 'text/markdown; charset=UTF-8')
res.send(this.container.get(req.params.uuid).provider.raw)
} else if (req.path == '/offline.json') {
let offline = this.container
.get(req.params.uuid)
.provider.getOffline()
res.app.render(
'index-offline.ejs',
Object.assign(
{
title: this.container.get(req.params.uuid)
.provider.meta.title,
content: offline.article,
config: JSON.stringify(
this.container.get(req.params.uuid).provider
.meta.config
),
uuid: JSON.stringify(req.params.uuid),
},
MDProvider.offlineEnv
),
(err, html) => {
if (err) {
console.log(err)
res.sendStatus(500)
} else {
res.json({
article: html,
resource: offline.resource,
meta: Object.assign(
{
uuid: req.params.uuid,
},
offline.meta
),
})
}
}
)
} else {
this.container.get(req.params.uuid).router(req, res, next)
}
}
})
this.server = router
}
async slideLoad(file) {
let isFile = await new Promise((resolve, reject) => {
fs.stat(path.resolve(this.basePath, file), (err, stats) => {
if (!err) {
resolve(stats.isFile())
} else {
return null
}
})
})
if (isFile == null) return null
if (isFile) {
if (path.extname(file) !== '.md') return null
return [
file.split('.')[0],
{
router: express.Router(),
provider: new MDProvider(path.resolve(this.basePath, file)),
},
]
} else {
let indexPath = path.resolve(this.basePath, file, 'index.md')
try {
if (
!fs.existsSync(indexPath) ||
!fs.statSync(indexPath).isFile()
) {
return null
}
} catch (e) {
return null
}
return [
file,
{
router: express.static(
path.resolve(this.basePath, file, 'public')
),
provider: new MDProvider(indexPath),
},
]
}
}
init() {
this.watcher = chokidar.watch(this.basePath)
this.watcher
.on('add', async (filePath, stats) => {
if (this.basePath == path.dirname(filePath)) {
let slide = await this.slideLoad(path.basename(filePath))
if (slide != null) {
this.container.set(slide[0], slide[1])
}
}
})
.on('addDir', async (dirPath, stats) => {
if (this.basePath == path.dirname(dirPath)) {
let slide = await this.slideLoad(path.basename(dirPath))
if (slide != null) {
this.container.set(slide[0], slide[1])
}
}
})
.on('unlink', async (filePath, stats) => {
if (this.basePath == path.dirname(filePath)) {
this.container.delete(path.basename(filePath).split('.')[0])
}
})
.on('unlinkDir', async (dirPath, stats) => {
if (this.basePath == path.dirname(dirPath)) {
this.container.delete(path.basename(dirPath))
}
})
}
info() {
let result = []
for (let key of this.container.keys()) {
result.push([key, this.container.get(key).provider.meta])
}
return result
}
}
module.exports = ContentServer