-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (42 loc) · 1.38 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
const fs = require('fs')
const path = require('path')
const { createServer } = require('./devserver')
const { createMiddleware } = require('./menu')
const provider = require('./markdown')()
const ssr = require('./ssr')
module.exports.wrapCommand = options => {
provider.resolvePath = filePath => path.resolve(options.sourceDir, './' + filePath)
const app = createServer({ sourceDir: options.sourceDir }) //监听文件修改
app.use(createMiddleware(options)) // 往 ctx 注入 menu 文件列表
//处理静态资源
app.use(async (ctx, next) => {
if (ctx.url.startsWith('/assets')) {
try {
const buffer = fs.readFileSync(path.resolve(__dirname, './' + ctx.url))
ctx.type = path.extname(ctx.url).slice(1)
ctx.body = buffer
} catch(e) {
ctx.body = ''
}
} else {
await next()
}
})
app.use(async (ctx, next) => {
await provider.patch(ctx.menu)
const { request: { url, query } } = ctx
const reqPath = url.split('?')[0]
const reqFile = path.extname(reqPath) === '' ? reqPath + '/README.md' : reqPath
const template = path.resolve(__dirname, './template/App.vue')
ctx.body = await ssr.renderMarkdown({
reqFile,
provider,
template,
options
})
await next()
})
app.start(options.port, () => {
console.log('编译的docs目录: ' + path.resolve(options.sourceDir))
})
}