MCPcopy Index your code
hub / github.com/github/docs / permalinkRedirects

Function permalinkRedirects

lib/redirects/permalinks.js:4–46  ·  view source on GitHub ↗
(permalinks, redirectFrom)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 1

getPathWithoutVersionFunction · 0.90

Tested by

no test coverage detected