(req, res)
| 7 | // This middleware exposes a list of all categories and child articles at /categories.json. |
| 8 | // GitHub Support uses this for internal ZenDesk search functionality. |
| 9 | export default async function categoriesForSupport(req, res) { |
| 10 | const englishSiteTree = req.context.siteTree.en |
| 11 | const allCategories = [] |
| 12 | |
| 13 | for (const productPage of Object.values(englishSiteTree['free-pro-team@latest'].childPages)) { |
| 14 | if (productPage.page.relativePath.startsWith('early-access')) continue |
| 15 | if (!productPage.childPages || !productPage.childPages.length) continue |
| 16 | await Promise.all( |
| 17 | productPage.childPages.map(async (categoryPage) => { |
| 18 | // We can't get the rendered titles from middleware/render-tree-titles |
| 19 | // here because that middleware only runs on the current version, and this |
| 20 | // middleware processes all versions. |
| 21 | const name = categoryPage.page.title.includes('{') |
| 22 | ? await categoryPage.page.renderProp('title', req.context, renderOpts) |
| 23 | : categoryPage.page.title |
| 24 | |
| 25 | allCategories.push({ |
| 26 | name, |
| 27 | published_articles: await findArticlesPerCategory(categoryPage, [], req.context), |
| 28 | }) |
| 29 | }) |
| 30 | ) |
| 31 | } |
| 32 | |
| 33 | // Cache somewhat aggressively but note that it will be soft-purged |
| 34 | // in every prod deployment. |
| 35 | defaultCacheControl(res) |
| 36 | |
| 37 | return res.json(allCategories) |
| 38 | } |
| 39 | |
| 40 | async function findArticlesPerCategory(currentPage, articlesArray, context) { |
| 41 | if (currentPage.page.documentType === 'article') { |
nothing calls this directly
no test coverage detected