Skip to content

Commit 1623191

Browse files
committed
extract layout handling into separate middleware
1 parent 2c881b6 commit 1623191

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const layouts = require('../../lib/layouts')
2+
3+
module.exports = function layoutContext (req, res, next) {
4+
if (!req.context.page) return next()
5+
6+
let layoutName
7+
8+
if (req.context.page.layout) {
9+
// Layouts can be specified with a `layout` frontmatter value.
10+
// Any invalid layout values will be caught by frontmatter schema validation.
11+
layoutName = req.context.page.layout
12+
// A `layout: false` value means use no layout.
13+
} else if (req.context.page.layout === false) {
14+
layoutName = ''
15+
// If undefined, use either the default layout or the generic-toc layout.
16+
} else if (req.context.page.layout === undefined) {
17+
layoutName = 'default'
18+
}
19+
20+
// Attach to the context object
21+
req.context.currentLayoutName = layoutName
22+
req.context.currentLayout = layouts[layoutName]
23+
24+
return next()
25+
}

middleware/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ module.exports = function (app) {
110110
app.use(instrument('./contextualizers/rest'))
111111
app.use(instrument('./contextualizers/webhooks'))
112112
app.use(asyncMiddleware(instrument('./contextualizers/whats-new-changelog')))
113+
app.use(instrument('./contextualizers/layout'))
113114
app.use(asyncMiddleware(instrument('./breadcrumbs')))
114115
app.use(asyncMiddleware(instrument('./early-access-breadcrumbs')))
115116
app.use(asyncMiddleware(instrument('./enterprise-server-releases')))

middleware/render-page.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,8 @@ module.exports = async function renderPage (req, res, next) {
129129
}
130130
}
131131

132-
// Layouts can be specified with a `layout` frontmatter value
133-
// If unspecified, `layouts/default.html` is used.
134-
// Any invalid layout values will be caught by frontmatter schema validation.
135-
const layoutName = context.page.layout || 'default'
136-
137-
// Set `layout: false` to use no layout
138-
const layout = context.page.layout === false ? '' : layouts[layoutName]
139-
140-
const output = await liquid.parseAndRender(layout, context)
132+
// currentLayout is added to the context object in middleware/contextualizers/layouts
133+
const output = await liquid.parseAndRender(req.context.currentLayout, context)
141134

142135
// First, send the response so the user isn't waiting
143136
// NOTE: Do NOT `return` here as we still need to cache the response afterward!

0 commit comments

Comments
 (0)