forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermalinks.js
46 lines (39 loc) · 2.11 KB
/
permalinks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import nonEnterpriseDefaultVersion from '../../../lib/non-enterprise-default-version.js'
import { getPathWithoutVersion } from '../../../lib/path-utils.js'
export default function permalinkRedirects(permalinks, redirectFrom) {
const redirects = {}
if (!permalinks.length) return redirects
// The following is handling for versionless redirect fallbacks!
// We put an entry into `redirects` without any version prefix that goes to the first supported
// version in the lib/all-versions.js order. For example, we want this versionless path:
// /billing/managing-billing-for-your-github-account/managing-invoices-for-your-enterprise
// to redirect to its first supported version, which is GHEC:
// /enterprise-cloud@latest/billing/managing-billing-for-your-github-account/managing-invoices-for-your-enterprise
if (permalinks[0].pageVersion !== nonEnterpriseDefaultVersion) {
redirects[getPathWithoutVersion(permalinks[0].hrefWithoutLanguage)] =
permalinks[0].hrefWithoutLanguage
}
// For every "old" path in a content file's redirect_from frontmatter, also add that path to
// the redirects object as a key, where the value is the content file's permalink.
redirectFrom.forEach((frontmatterOldPath) => {
if (!frontmatterOldPath.startsWith('/')) {
throw new Error(
`'${frontmatterOldPath}' is not a valid redirect_from frontmatter value because it doesn't start with a /`,
)
}
// Exceptions where the `redirect_from` entries are too old
frontmatterOldPath = frontmatterOldPath
.replace('/admin/guides/', '/admin/')
.replace('/enterprise/', '/')
permalinks.forEach((permalink, index) => {
// For the first supported permalink (the order is determined by lib/all-versions),
// put an entry into `redirects` without any version prefix.
if (index === 0) {
redirects[frontmatterOldPath] = permalink.hrefWithoutLanguage
}
// For every permalink, put an entry into `redirects` with the version prefix.
redirects[`/${permalink.pageVersion}${frontmatterOldPath}`] = permalink.hrefWithoutLanguage
})
})
return redirects
}