1
0
mirror of synced 2025-12-23 21:07:12 -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 type { Context, ExtendedRequest, Tree } from '@/types'
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.
// GitHub Support uses this for internal ZenDesk search functionality.
export default async function categoriesForSupport(req, res) {
const englishSiteTree = req.context.siteTree.en
const allCategories = []
export default async function categoriesForSupport(req: ExtendedRequest, res: Response) {
const englishSiteTree = req.context!.siteTree!.en
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.childPages || !productPage.childPages.length) continue
await Promise.all(
@@ -24,7 +35,7 @@ export default async function categoriesForSupport(req, res) {
allCategories.push({
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)
}
async function findArticlesPerCategory(currentPage, articlesArray, context) {
async function findArticlesPerCategory(
currentPage: Tree,
articlesArray: Article[],
context: Context,
) {
if (currentPage.page.documentType === 'article') {
const title = currentPage.page.title.includes('{')
? await currentPage.page.renderProp('title', context, renderOpts)
@@ -45,7 +60,7 @@ async function findArticlesPerCategory(currentPage, articlesArray, context) {
articlesArray.push({
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 robots from './robots.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 secretScanning from '@/secret-scanning/middleware/secret-scanning.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.
export type ExtendedRequest = Request & {
pagePath?: string
context?: {
currentCategory?: string
error?: Error
}
context?: Context
language?: string
userLanguage?: string
// Add more properties here as needed
}
export type Context = {
currentCategory?: string
error?: Error
siteTree?: SiteTree
}
type Language = {
name: string
code: string
@@ -45,6 +48,7 @@ export type Page = {
mtime: number
permalinks: Permalink[]
fullPath: string
relativePath: string
title: string
shortTitle?: string
intro: string