@@ -3,27 +3,53 @@ import path from 'path'
3
3
import { productMap , data } from './all-products.js'
4
4
import { renderContentWithFallback } from './render-with-fallback.js'
5
5
import removeFPTFromPath from './remove-fpt-from-path.js'
6
- import getApplicableVersions from './get-applicable-versions.js'
7
6
8
7
async function getPage ( id , lang , pageMap , context ) {
9
8
const productId = id . split ( '/' ) [ 0 ]
10
9
const product = productMap [ productId ]
11
10
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
+
17
27
let name = product . name
18
- let versions = getApplicableVersions ( '*' )
19
28
20
29
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
+ }
21
39
const page = pageMap [ href ]
22
40
if ( ! page ) {
23
41
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 .` ,
25
43
)
26
44
}
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
+
27
53
if ( page . rawShortTitle ) {
28
54
name = await renderContentWithFallback ( page , 'rawShortTitle' , context , {
29
55
textOnly : true ,
@@ -37,16 +63,13 @@ async function getPage(id, lang, pageMap, context) {
37
63
textOnly : true ,
38
64
} )
39
65
}
40
-
41
- versions = page . applicableVersions
42
66
}
43
67
// Return only the props needed for the ProductSelectionCard, since
44
68
// that's the only place this is ever used.
45
69
return {
46
70
id,
47
71
name,
48
- href : href . replace ( `/${ lang } /` , '/' ) ,
49
- versions,
72
+ href,
50
73
external,
51
74
}
52
75
}
@@ -59,9 +82,9 @@ export async function getProductGroups(pageMap, lang, context) {
59
82
icon : group . icon || null ,
60
83
octicon : group . octicon || null ,
61
84
// 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 ) ,
65
88
}
66
89
} ) ,
67
90
)
0 commit comments