Skip to content

Commit

Permalink
Port featured-links.js to TypeScript (#51385)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Jun 25, 2024
1 parent bc58017 commit 8a6cc59
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/frame/middleware/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import renderProductName from './context/render-product-name'
import features from '@/versions/middleware/features.js'
import productExamples from './context/product-examples'
import productGroups from './context/product-groups'
import featuredLinks from '@/landings/middleware/featured-links.js'
import featuredLinks from '@/landings/middleware/featured-links'
import learningTrack from '@/learning-track/middleware/learning-track.js'
import next from './next.js'
import renderPage from './render-page.js'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import getLinkData from '#src/learning-track/lib/get-link-data.js'
import { renderContent } from '#src/content-render/index.js'
import type { Response, NextFunction } from 'express'

import type { ExtendedRequest, FeaturedLinkExpanded } from '@/types'
import getLinkData from '@/learning-track/lib/get-link-data.js'
import { renderContent } from '@/content-render/index.js'

/**
* This is the max. number of featured links, by any category, that we
Expand All @@ -24,7 +27,12 @@ import { renderContent } from '#src/content-render/index.js'
const MAX_FEATURED_LINKS = 4

// this middleware adds properties to the context object
export default async function featuredLinks(req, res, next) {
export default async function featuredLinks(
req: ExtendedRequest,
res: Response,
next: NextFunction,
) {
if (!req.context) throw new Error('request is not contextualized')
if (!req.context.page) return next()

if (
Expand All @@ -46,7 +54,9 @@ export default async function featuredLinks(req, res, next) {
// the provided string title or an empty title. When the title is empty,
// it indicates the video is not versioned for the current version
req.context.featuredLinks[key] = []
for (const featuredLink of req.context.page.featuredLinks[key]) {
if (!(key in req.context.page.featuredLinks))
throw new Error('featureLinks key not found in Page')
for (const featuredLink of req.context.page.featuredLinks[key]!) {
const title = await renderContent(featuredLink.title, req.context, {
textOnly: true,
})
Expand All @@ -60,12 +70,15 @@ export default async function featuredLinks(req, res, next) {
}
}
} else {
req.context.featuredLinks[key] = await getLinkData(
req.context.page.featuredLinks[key],
if (!(key in req.context.page.featuredLinks))
throw new Error('featureLinks key not found in Page')
const pageFeaturedLink = req.context.page.featuredLinks[key]
req.context.featuredLinks[key] = (await getLinkData(
pageFeaturedLink,
req.context,
{ title: true, intro: true, fullTitle: true },
MAX_FEATURED_LINKS,
)
)) as FeaturedLinkExpanded[] // Remove ones `getLinkData` is TS
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test, vi } from 'vitest'

import { getDOM } from '#src/tests/helpers/e2etest.js'
import enterpriseServerReleases from '#src/versions/lib/enterprise-server-releases.js'
import { getDOM } from '@/tests/helpers/e2etest.js'
import enterpriseServerReleases from '@/versions/lib/enterprise-server-releases.js'

describe('featuredLinks', () => {
vi.setConfig({ testTimeout: 60 * 1000 })
Expand Down
41 changes: 28 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,7 @@ export type PageFrontmatter = {
authors?: string[]
examples_source?: string
effectiveDate?: string

featuredLinks?: {
gettingStarted?: string[]
startHere?: string[]
guideCards?: string[]
popular?: string[]
popularHeading?: string
videos?: {
title: string
href: string
}[]
videoHeadings?: string
}[]
featuredLinks?: FeaturedLinks
changelog?: ChangeLog
type?: string
topics?: string[]
Expand All @@ -67,6 +55,19 @@ export type PageFrontmatter = {
childGroups?: ChildGroup[]
}

type FeaturedLinks = {
gettingStarted?: string[]
startHere?: string[]
guideCards?: string[]
popular?: string[]
popularHeading?: string
videos?: {
title: string
href: string
}[]
videoHeadings?: string
}

export type ChildGroup = {
name: string
octicon: string
Expand Down Expand Up @@ -157,6 +158,19 @@ export type Context = {
productCommunityExamples?: ProductExample[]
productUserExamples?: ProductExample[]
productGroups?: ProductGroup[]
featuredLinks?: FeaturedLinksExpanded
}

export type FeaturedLinkExpanded = {
href: string
title: string
page?: Page
fullTitle?: string
intro?: string
}

type FeaturedLinksExpanded = {
[key: string]: FeaturedLinkExpanded[]
}

export type ProductGroup = {
Expand Down Expand Up @@ -296,6 +310,7 @@ export type Page = {
layout?: string | boolean
earlyAccessToc?: boolean
autogenerated?: string
featuredLinks?: FeaturedLinksExpanded
}

type ChangeLog = {
Expand Down

0 comments on commit 8a6cc59

Please sign in to comment.