Skip to content

Commit 9ca5fef

Browse files
authored
Merge pull request #5369 from github/repo-sync
repo sync
2 parents 2244a1f + 64d678f commit 9ca5fef

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
@@ -136,15 +136,8 @@ module.exports = async function renderPage (req, res, next) {
136136
}
137137
}
138138

139-
// Layouts can be specified with a `layout` frontmatter value
140-
// If unspecified, `layouts/default.html` is used.
141-
// Any invalid layout values will be caught by frontmatter schema validation.
142-
const layoutName = context.page.layout || 'default'
143-
144-
// Set `layout: false` to use no layout
145-
const layout = context.page.layout === false ? '' : layouts[layoutName]
146-
147-
const output = await liquid.parseAndRender(layout, context)
139+
// currentLayout is added to the context object in middleware/contextualizers/layouts
140+
const output = await liquid.parseAndRender(req.context.currentLayout, context)
148141

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

0 commit comments

Comments
 (0)