From c3897e1d50d015b9c460db7c8635357137819bd4 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Mon, 17 Jun 2024 17:05:25 -0400 Subject: [PATCH] Port `context/layout.js` to TypeScript (#51241) --- src/frame/middleware/context/layout.js | 20 -------------------- src/frame/middleware/context/layout.ts | 24 ++++++++++++++++++++++++ src/frame/middleware/index.ts | 2 +- src/types.ts | 2 ++ 4 files changed, 27 insertions(+), 21 deletions(-) delete mode 100644 src/frame/middleware/context/layout.js create mode 100644 src/frame/middleware/context/layout.ts diff --git a/src/frame/middleware/context/layout.js b/src/frame/middleware/context/layout.js deleted file mode 100644 index c0cda4a35d5a..000000000000 --- a/src/frame/middleware/context/layout.js +++ /dev/null @@ -1,20 +0,0 @@ -export default function layoutContext(req, res, next) { - if (!req.context.page) return next() - - const layoutOptsByType = { - // Layouts can be specified with a `layout` frontmatter value. - // Any invalid layout values will be caught by frontmatter schema validation. - string: req.context.page.layout, - // A `layout: false` value means use no layout. - boolean: '', - // For all other files (like articles and the homepage), use the `default` layout. - undefined: 'default', - } - - const layoutName = layoutOptsByType[typeof req.context.page.layout] - - // Attach to the context object - req.context.currentLayoutName = layoutName - - return next() -} diff --git a/src/frame/middleware/context/layout.ts b/src/frame/middleware/context/layout.ts new file mode 100644 index 000000000000..447955cc8917 --- /dev/null +++ b/src/frame/middleware/context/layout.ts @@ -0,0 +1,24 @@ +import type { Response, NextFunction } from 'express' + +import type { ExtendedRequest } from '@/types' + +export default function layoutContext(req: ExtendedRequest, res: Response, next: NextFunction) { + if (!req.context) throw new Error('request is not contextualized') + if (!req.context.page) return next() + + let layoutName = 'default' + if (req.context.page.layout) { + if (typeof req.context.page.layout === 'boolean') { + // A `layout: false` value means use no layout. + layoutName = '' + } else if (typeof req.context.page.layout === 'string') { + layoutName = req.context.page.layout + } else { + throw new Error(`Invalid layout value type: ${req.context.page.layout}`) + } + } + + req.context.currentLayoutName = layoutName + + return next() +} diff --git a/src/frame/middleware/index.ts b/src/frame/middleware/index.ts index 58272f3b19ad..336187516933 100644 --- a/src/frame/middleware/index.ts +++ b/src/frame/middleware/index.ts @@ -39,7 +39,7 @@ import triggerError from '@/observability/middleware/trigger-error' import secretScanning from '@/secret-scanning/middleware/secret-scanning' import ghesReleaseNotes from '@/release-notes/middleware/ghes-release-notes' import whatsNewChangelog from './context/whats-new-changelog' -import layout from './context/layout.js' +import layout from './context/layout' import currentProductTree from './context/current-product-tree.js' import genericToc from './context/generic-toc.js' import breadcrumbs from './context/breadcrumbs.js' diff --git a/src/types.ts b/src/types.ts index ca839791c884..29271a80f6b4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -82,6 +82,7 @@ export type Context = { autotitleLanguage?: string latestPatch?: string latestRelease?: string + currentLayoutName?: string } export type GHESRelease = { @@ -185,6 +186,7 @@ export type Page = { versions: FrontmatterVersions applicableVersions: string[] changelog?: ChangeLog + layout?: string | boolean } type ChangeLog = {