Skip to content

Commit acca063

Browse files
authored
Merge pull request #27337 from github/repo-sync
Repo sync
2 parents 1422b1a + 0212751 commit acca063

File tree

10 files changed

+520
-716
lines changed

10 files changed

+520
-716
lines changed

components/context/MainContext.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export type ProductT = {
99
href: string
1010
id: string
1111
name: string
12-
versions?: Array<string>
1312
}
1413

1514
type VersionItem = {
@@ -70,7 +69,6 @@ export type MainContextT = {
7069
currentProduct?: ProductT
7170
currentLayoutName: string
7271
isHomepageVersion: boolean
73-
isFPT: boolean
7472
data: DataT
7573
error: string
7674
currentCategory?: string
@@ -132,7 +130,6 @@ export const getMainContext = async (req: any, res: any): Promise<MainContextT>
132130
currentProduct: req.context.productMap[req.context.currentProduct] || null,
133131
currentLayoutName: req.context.currentLayoutName,
134132
isHomepageVersion: req.context.page?.documentType === 'homepage',
135-
isFPT: req.context.currentVersion === 'free-pro-team@latest',
136133
error: req.context.error ? req.context.error.toString() : '',
137134
data: {
138135
ui: req.context.site.data.ui,

lib/all-products.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ for (const productId of productIds) {
3838
toc,
3939
wip: data.wip || false,
4040
hidden: data.hidden || false,
41+
versions: applicableVersions,
4142
}
42-
43-
internalProducts[productId].versions = applicableVersions
4443
}
4544

4645
export const productMap = Object.assign({}, internalProducts, externalProducts)

lib/get-product-groups.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,53 @@ import path from 'path'
33
import { productMap, data } from './all-products.js'
44
import { renderContentWithFallback } from './render-with-fallback.js'
55
import removeFPTFromPath from './remove-fpt-from-path.js'
6-
import getApplicableVersions from './get-applicable-versions.js'
76

87
async function getPage(id, lang, pageMap, context) {
98
const productId = id.split('/')[0]
109
const product = productMap[productId]
1110
const external = product.external || false // undefined becomes false
12-
// Adding external links to a product group here due to accessibility design changes
13-
// where we removed the sidebar on the homepage - docs-team 2965
14-
const href = external
15-
? product.href
16-
: removeFPTFromPath(path.posix.join('/', lang, product.versions[0], id))
11+
12+
// The href depends. Initially all we have is an `id` which might be
13+
// something like 'code-security/dependabot'.
14+
// To get from that to a Page instance, we have to "guess" what the
15+
// href might be.
16+
// If URL currently is `/fr/[email protected]`,
17+
// we're going to try `/fr/[email protected]/code-security/dependabot`
18+
// first. And if the URL is `/en` we'll try `/en/code-security/dependabot`.
19+
// But some pages are not available in all versions.
20+
// So we have to try `product.versions[0]` which comes from the `productMap`
21+
// (not be confused with the `pageMap`!)
22+
// Once we have a page, we can get to its `.applicableVersions`.
23+
// This way, we get the best of both worlds. We use the `currentVersion`
24+
// if it's applicable, but we fall back to the first version if it's not.
25+
let href = product.href
26+
1727
let name = product.name
18-
let versions = getApplicableVersions('*')
1928

2029
if (!external) {
30+
// First we have to find it as a page object based on its ID.
31+
href = removeFPTFromPath(path.posix.join('/', lang, context.currentVersion, id))
32+
if (!pageMap[href]) {
33+
// If the page is not available in the *current* version, we
34+
// fall back it its default version, which is `product.versions[0]`.
35+
// For example, you're on `/en/[email protected]` and you're
36+
// but a `/foo/bar` is only available in `enterprise-cloud@latest`.
37+
href = removeFPTFromPath(path.posix.join('/', lang, product.versions[0], id))
38+
}
2139
const page = pageMap[href]
2240
if (!page) {
2341
throw new Error(
24-
`Unable to find a page by the href '${href}'. Review your 'childGroups' frontmatter maybe.`,
42+
`Unable to find a page by the href '${href}'. Review your 'childGroups' front matter.`,
2543
)
2644
}
45+
46+
// Some should not be included for the current version, and returning
47+
// undefined here means this entry will be filtered out by the caller.
48+
const isFPT = context.currentVersion === 'free-pro-team@latest'
49+
if (!isFPT && !page.applicableVersions.includes(context.currentVersion)) {
50+
return
51+
}
52+
2753
if (page.rawShortTitle) {
2854
name = await renderContentWithFallback(page, 'rawShortTitle', context, {
2955
textOnly: true,
@@ -37,16 +63,13 @@ async function getPage(id, lang, pageMap, context) {
3763
textOnly: true,
3864
})
3965
}
40-
41-
versions = page.applicableVersions
4266
}
4367
// Return only the props needed for the ProductSelectionCard, since
4468
// that's the only place this is ever used.
4569
return {
4670
id,
4771
name,
48-
href: href.replace(`/${lang}/`, '/'),
49-
versions,
72+
href,
5073
external,
5174
}
5275
}
@@ -59,9 +82,9 @@ export async function getProductGroups(pageMap, lang, context) {
5982
icon: group.icon || null,
6083
octicon: group.octicon || null,
6184
// Typically the children are product IDs, but we support deeper page paths too
62-
children: await Promise.all(
63-
group.children.map((id) => getPage(id, lang, pageMap, context)),
64-
),
85+
children: (
86+
await Promise.all(group.children.map((id) => getPage(id, lang, pageMap, context)))
87+
).filter(Boolean),
6588
}
6689
}),
6790
)

0 commit comments

Comments
 (0)