Skip to content

Commit

Permalink
Merge pull request #290 from evilz/Koa-static-server
Browse files Browse the repository at this point in the history
Change static middleware
  • Loading branch information
evilz authored Sep 22, 2019
2 parents 7dd5fe4 + 204825d commit 34b9c66
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 86 deletions.
93 changes: 23 additions & 70 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@
},
"dependencies": {
"@evilz/markdown-it-attrs": "^3.0.1",
"@types/koa-static-server": "^1.3.0",
"fs-jetpack": "^2.2.2",
"gray-matter": "^4.0.2",
"koa": "^2.8.1",
"koa-ejs": "^4.2.0",
"koa-logger": "^3.2.1",
"koa-router": "^7.4.0",
"koa-static": "^5.0.0",
"koa-static-server": "^1.4.0",
"markdown-it": "^10.0.0",
"markdown-it-block-embed": "0.0.3",
"markdown-it-container": "^2.0.0",
Expand Down
34 changes: 20 additions & 14 deletions src/RevealServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as http from 'http'
import * as Koa from 'koa'
import * as render from 'koa-ejs'
import * as koalogger from 'koa-logger'
import * as Router from 'koa-router'
import * as koastatic from 'koa-static'
import * as serve from 'koa-static-server'
import * as path from 'path'

import markdown from './Markdown-it'
Expand Down Expand Up @@ -66,14 +65,11 @@ export class RevealServer {

// LOG REQUEST
app.use(koalogger((str, args) => {this.logger.log(str)}))
app.use(this.exportMiddleware(exportHTML, () => this.isInExport()))

// For static media or else
const rootDir = this.getRootDir()
if (rootDir) {
app.use(koastatic(rootDir));
}
// EXPORT
app.use(this.exportMiddleware(exportHTML, () => this.isInExport()))

// EJS VIEW engine
render(app, {
root: path.resolve(this.extensionPath, 'views'),
layout: 'template',
Expand All @@ -82,26 +78,36 @@ export class RevealServer {
debug: true
});

const router = new Router();
router.get('/', async (ctx, next) => {

// MAIN FILE
app.use( async (ctx, next) => {

if(ctx.path !== '/') { return next()}

const htmlSlides = this.getSlides().map(s => (
{
...s,
html: markdown.render(s.text),
children: s.verticalChildren.map(c => ( {...c, html: markdown.render(c.text) }))
}))

ctx.state = { slides: htmlSlides, ...this.getConfiguration() }
await ctx.render('reveal');
});


// STATIC LIBS
const libsPAth = path.join(this.extensionPath, 'libs')
router.get('/libs', koastatic(libsPAth));

app.use(router.routes());
app.use(serve({ rootDir:libsPAth, rootPath: '/libs' }))


// STATIC RELATIVE TO MD FILE
const rootDir = this.getRootDir()
if (rootDir) {
app.use(serve({rootDir, rootPath : '/' }))
}


// ERROR HANDLER
app.on('error', err => {
this.logger.error(err)
})
Expand Down

0 comments on commit 34b9c66

Please sign in to comment.