Skip to content

Commit c3897e1

Browse files
author
Peter Bengtsson
authored
Port context/layout.js to TypeScript (#51241)
1 parent 26e79d8 commit c3897e1

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

src/frame/middleware/context/layout.js

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Response, NextFunction } from 'express'
2+
3+
import type { ExtendedRequest } from '@/types'
4+
5+
export default function layoutContext(req: ExtendedRequest, res: Response, next: NextFunction) {
6+
if (!req.context) throw new Error('request is not contextualized')
7+
if (!req.context.page) return next()
8+
9+
let layoutName = 'default'
10+
if (req.context.page.layout) {
11+
if (typeof req.context.page.layout === 'boolean') {
12+
// A `layout: false` value means use no layout.
13+
layoutName = ''
14+
} else if (typeof req.context.page.layout === 'string') {
15+
layoutName = req.context.page.layout
16+
} else {
17+
throw new Error(`Invalid layout value type: ${req.context.page.layout}`)
18+
}
19+
}
20+
21+
req.context.currentLayoutName = layoutName
22+
23+
return next()
24+
}

src/frame/middleware/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import triggerError from '@/observability/middleware/trigger-error'
3939
import secretScanning from '@/secret-scanning/middleware/secret-scanning'
4040
import ghesReleaseNotes from '@/release-notes/middleware/ghes-release-notes'
4141
import whatsNewChangelog from './context/whats-new-changelog'
42-
import layout from './context/layout.js'
42+
import layout from './context/layout'
4343
import currentProductTree from './context/current-product-tree.js'
4444
import genericToc from './context/generic-toc.js'
4545
import breadcrumbs from './context/breadcrumbs.js'

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export type Context = {
8282
autotitleLanguage?: string
8383
latestPatch?: string
8484
latestRelease?: string
85+
currentLayoutName?: string
8586
}
8687

8788
export type GHESRelease = {
@@ -185,6 +186,7 @@ export type Page = {
185186
versions: FrontmatterVersions
186187
applicableVersions: string[]
187188
changelog?: ChangeLog
189+
layout?: string | boolean
188190
}
189191

190192
type ChangeLog = {

0 commit comments

Comments
 (0)