1
0
mirror of synced 2025-12-23 11:54:18 -05:00

Add 'url' field to categories.json (#50897)

This commit is contained in:
Peter Bengtsson
2024-05-30 16:18:24 -04:00
committed by GitHub
parent 6581706d54
commit 8f764884d5
3 changed files with 32 additions and 13 deletions

View File

@@ -1,16 +1,27 @@
import path from 'path' import type { Response } from 'express'
import { defaultCacheControl } from './cache-control.js' import { defaultCacheControl } from './cache-control.js'
import type { Context, ExtendedRequest, Tree } from '@/types'
const renderOpts = { textOnly: true } const renderOpts = { textOnly: true }
type Article = {
title: string
url: string
}
type Category = {
name: string
published_articles: Article[]
}
// This middleware exposes a list of all categories and child articles at /categories.json. // This middleware exposes a list of all categories and child articles at /categories.json.
// GitHub Support uses this for internal ZenDesk search functionality. // GitHub Support uses this for internal ZenDesk search functionality.
export default async function categoriesForSupport(req, res) { export default async function categoriesForSupport(req: ExtendedRequest, res: Response) {
const englishSiteTree = req.context.siteTree.en const englishSiteTree = req.context!.siteTree!.en
const allCategories = [] const allCategories: Category[] = []
for (const productPage of Object.values(englishSiteTree['free-pro-team@latest'].childPages)) { const fpt = englishSiteTree['free-pro-team@latest']
for (const productPage of Object.values(fpt.childPages!)) {
if (productPage.page.relativePath.startsWith('early-access')) continue if (productPage.page.relativePath.startsWith('early-access')) continue
if (!productPage.childPages || !productPage.childPages.length) continue if (!productPage.childPages || !productPage.childPages.length) continue
await Promise.all( await Promise.all(
@@ -24,7 +35,7 @@ export default async function categoriesForSupport(req, res) {
allCategories.push({ allCategories.push({
name, name,
published_articles: await findArticlesPerCategory(categoryPage, [], req.context), published_articles: await findArticlesPerCategory(categoryPage, [], req.context!),
}) })
}), }),
) )
@@ -37,7 +48,11 @@ export default async function categoriesForSupport(req, res) {
return res.json(allCategories) return res.json(allCategories)
} }
async function findArticlesPerCategory(currentPage, articlesArray, context) { async function findArticlesPerCategory(
currentPage: Tree,
articlesArray: Article[],
context: Context,
) {
if (currentPage.page.documentType === 'article') { if (currentPage.page.documentType === 'article') {
const title = currentPage.page.title.includes('{') const title = currentPage.page.title.includes('{')
? await currentPage.page.renderProp('title', context, renderOpts) ? await currentPage.page.renderProp('title', context, renderOpts)
@@ -45,7 +60,7 @@ async function findArticlesPerCategory(currentPage, articlesArray, context) {
articlesArray.push({ articlesArray.push({
title, title,
slug: path.basename(currentPage.href), url: currentPage.href.replace('/en/', '/'),
}) })
} }

View File

@@ -34,7 +34,7 @@ import buildInfo from './build-info.js'
import archivedEnterpriseVersions from '@/archives/middleware/archived-enterprise-versions.js' import archivedEnterpriseVersions from '@/archives/middleware/archived-enterprise-versions.js'
import robots from './robots.js' import robots from './robots.js'
import earlyAccessLinks from '@/early-access/middleware/early-access-links.js' import earlyAccessLinks from '@/early-access/middleware/early-access-links.js'
import categoriesForSupport from './categories-for-support.js' import categoriesForSupport from './categories-for-support'
import triggerError from '@/observability/middleware/trigger-error.js' import triggerError from '@/observability/middleware/trigger-error.js'
import secretScanning from '@/secret-scanning/middleware/secret-scanning.js' import secretScanning from '@/secret-scanning/middleware/secret-scanning.js'
import ghesReleaseNotes from '@/release-notes/middleware/ghes-release-notes.js' import ghesReleaseNotes from '@/release-notes/middleware/ghes-release-notes.js'

View File

@@ -6,15 +6,18 @@ import type { Request } from 'express'
// througout the codebase. // througout the codebase.
export type ExtendedRequest = Request & { export type ExtendedRequest = Request & {
pagePath?: string pagePath?: string
context?: { context?: Context
currentCategory?: string
error?: Error
}
language?: string language?: string
userLanguage?: string userLanguage?: string
// Add more properties here as needed // Add more properties here as needed
} }
export type Context = {
currentCategory?: string
error?: Error
siteTree?: SiteTree
}
type Language = { type Language = {
name: string name: string
code: string code: string
@@ -45,6 +48,7 @@ export type Page = {
mtime: number mtime: number
permalinks: Permalink[] permalinks: Permalink[]
fullPath: string fullPath: string
relativePath: string
title: string title: string
shortTitle?: string shortTitle?: string
intro: string intro: string