move the middleware into jexodus
This commit is contained in:
Родитель
860f80cd22
Коммит
41987092e3
|
@ -9,6 +9,7 @@ const liquid = new Liquid.Engine
|
|||
|
||||
class Jexodus extends EventEmitter {
|
||||
constructor (basedir) {
|
||||
console.time('hugging the Jekyll tree')
|
||||
super()
|
||||
this.basedir = basedir
|
||||
this.config = requireYaml(path.join(this.basedir, '_config.yml'))
|
||||
|
@ -17,16 +18,30 @@ class Jexodus extends EventEmitter {
|
|||
hug(this.pathTo(dir))
|
||||
.on('data', (data) => {
|
||||
this[dir] = data
|
||||
this.check()
|
||||
this.finish()
|
||||
})
|
||||
})
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
check () {
|
||||
get middleware () {
|
||||
var self = this
|
||||
return function (req, res, next) {
|
||||
console.log(req.path)
|
||||
const page = self.routes[req.path]
|
||||
if (page) {
|
||||
return self.render(req.path).then(output => res.send(output))
|
||||
} else {
|
||||
return next()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
finish () {
|
||||
if (this.targets.every(dir => !!this[dir])) {
|
||||
this.buildRoutes()
|
||||
console.timeEnd('hugging the Jekyll tree')
|
||||
this.emit('ready')
|
||||
}
|
||||
}
|
||||
|
@ -34,18 +49,26 @@ class Jexodus extends EventEmitter {
|
|||
buildRoutes () {
|
||||
this.routes = {}
|
||||
const paths = Object.keys(flat(this._pages))
|
||||
.forEach(p => {
|
||||
if (!p.match(/\.content$/)) return
|
||||
p = p.replace(/\.content$/, '')
|
||||
.filter(key => key.match(/\.content$/)) // ignore paths that are not html or markdown files
|
||||
.map(key => key.replace(/\.content$/, ''))
|
||||
.forEach(key => {
|
||||
// use permalink found in frontmater
|
||||
let permalink = get(this._pages, `${key}.data.permalink`)
|
||||
|
||||
// derive permalink from path
|
||||
if (!permalink) {
|
||||
permalink = ('/' + key.split('.').join('/'))
|
||||
.replace(/\/index$/, '/') // /index becomes /
|
||||
.replace(/\/readme$/i, '/') // /readme also becomes /
|
||||
}
|
||||
|
||||
let href = ('/' + p.split('.').join('/'))
|
||||
.replace(/\/index$/, '/')
|
||||
.replace(/\/readme$/i, '/')
|
||||
.replace(/(.+)\/$/, '$1')
|
||||
this.routes[href] = p
|
||||
permalink = permalink
|
||||
.replace(/(.+)\/$/, '$1') // remove trailing slash
|
||||
|
||||
this.routes[permalink] = key
|
||||
})
|
||||
|
||||
// console.log(this.routes)
|
||||
|
||||
console.log(this.routes)
|
||||
}
|
||||
|
||||
render (href) {
|
||||
|
@ -58,7 +81,7 @@ class Jexodus extends EventEmitter {
|
|||
// Wrap page in layout
|
||||
const layout = this._layouts[page.data.layout || 'default']
|
||||
if (layout) {
|
||||
output = layout.content.replace('{{ content }}', output)
|
||||
output = layout.content.replace(/{{ ?content ?}}/, output)
|
||||
}
|
||||
|
||||
// Inject includes
|
||||
|
|
12
server.js
12
server.js
|
@ -14,17 +14,7 @@ app.use(sass({
|
|||
prefix: '/css'
|
||||
}))
|
||||
|
||||
const middleware = function (req, res, next) {
|
||||
const page = jexodus.routes[req.path]
|
||||
console.log(req.path)
|
||||
if (page) {
|
||||
return jexodus.render(req.path).then(output => res.send(output))
|
||||
} else {
|
||||
return next()
|
||||
}
|
||||
}
|
||||
|
||||
app.use(middleware)
|
||||
app.use(jexodus.middleware)
|
||||
app.use(express.static(__dirname))
|
||||
|
||||
function startServer () {
|
||||
|
|
Загрузка…
Ссылка в новой задаче