1
0
mirror of synced 2025-12-22 19:34:15 -05:00
Files
docs/lib/redirects/precompile.js
Kevin Heis 122da9610c Move redirect calculation from loadPages to loadRedirects (#16918)
* Move redirect calculation from loadPages to loadRedirects

* Make buildredirects a method for tests

* Update redirects.js

Co-authored-by: Chiedo John <2156688+chiedo@users.noreply.github.com>
2020-12-15 18:40:50 +00:00

87 lines
4.6 KiB
JavaScript
Executable File

const path = require('path')
const slash = require('slash')
const patterns = require('../patterns')
const { latest } = require('../enterprise-server-releases')
const getOldPathsFromPermalink = require('../redirects/get-old-paths-from-permalink')
const getDocsPathFromDevPath = require('../redirects/get-docs-path-from-developer-path')
const DEVELOPER_ROUTES = require('../redirects/static/developer-docs-routes-for-supported-versions')
const ARCHIVED_ROUTES = require('../redirects/static/archived-routes-from-213-to-217')
const nonEnterpriseDefaultVersion = require('../non-enterprise-default-version')
// This function runs at server warmup and precompiles possible redirect routes.
// It outputs them in key-value pairs within a neat Javascript object: { oldPath: newPath }
module.exports = async function precompileRedirects (pageList, pageMap) {
const allRedirects = {}
// 1. CURRENT PAGES PERMALINKS AND FRONTMATTER
// create backwards-compatible old paths for page permalinks and frontmatter redirects
pageList.forEach(page => Object.assign(allRedirects, page.buildRedirects()))
// 2. REDIRECTS FOR ARCHIVED ROUTES FROM 2.13 TO 2.17
// Starting with 2.18, we updated the archival script to create stubbed HTML redirect files,
// so those files do the heavy lifting. The handling here is only for blanket redirects
// (like /articles -> /github) between 2.13 and 2.17. This works by creating possible
// old/incoming paths from the archived routes JSON. Frontmatter redirects were lost
// for 2.13-2.17, but the archived middleware tries to find fallbacks.
ARCHIVED_ROUTES.forEach(archivedRoute => {
const languageFromPath = archivedRoute.match(patterns.getLanguageCode)[1]
const versionFromPath = archivedRoute.match(patterns.getEnterpriseVersionNumber)[1]
// get an array of possible old paths, e.g., /desktop/guides/ from /desktop/
const possibleOldPaths = getOldPathsFromPermalink(archivedRoute, languageFromPath, versionFromPath)
// for each old path, add a redirect to the current route
possibleOldPaths.forEach(oldPath => {
allRedirects[oldPath] = archivedRoute
})
})
// 3. DEVELOPER ROUTES FOR CURRENTLY SUPPORTED VERSIONS
// From the list of developer docs routes, create new docs.github.com-style paths.
// Note that the list only includes supported enterprise paths up to 2.21, which is
// the last version that was available on developer.github.com before the migration.
DEVELOPER_ROUTES.forEach(developerRoute => {
const newPath = getDocsPathFromDevPath(developerRoute, allRedirects, pageMap)
// add the redirect to the object
allRedirects[developerRoute] = newPath
// also add a variation with language code
const developerRouteWithLanguage = `/en${developerRoute}`
allRedirects[developerRouteWithLanguage] = newPath
// although we only support developer Enterprise paths up to 2.21, we make
// an exception to always redirect versionless paths to the latest version
if (developerRoute.includes('/2.21/')) {
const newPathOnLatestVersion = newPath.replace('@2.21/', `@${latest}/`)
const developerRouteWithoutVersion = developerRoute.replace('/2.21/', '/')
const developerRouteWithLanguageWithoutVersion = `/en${developerRouteWithoutVersion}`
allRedirects[developerRouteWithoutVersion] = newPathOnLatestVersion
allRedirects[developerRouteWithLanguageWithoutVersion] = newPathOnLatestVersion
}
// given a developer route like `/enterprise/2.19/v3/activity`,
// add a veriation like `/enterprise/2.19/user/v3/activity`;
// we need to do this because all links in content get rewritten
// by lib/rewrite-local-links to include `/user`
if (developerRoute.includes('/enterprise/')) {
const developerRouteWithUserPath = developerRoute.replace(/\/(v[34])/, '/user/$1')
const developerRouteWithUserPathAndLanguage = `/en${developerRouteWithUserPath}`
allRedirects[developerRouteWithUserPath] = newPath
allRedirects[developerRouteWithUserPathAndLanguage] = newPath
}
// given a developer route like `/v3/gists/comments`,
// add a veriation like `/free-pro-team@latest/v3/gists/comments`;
// again, we need to do this because all links in content get rewritten
if (!developerRoute.startsWith('/enterprise/')) {
const developerRouteWithVersion = slash(path.join(nonEnterpriseDefaultVersion, developerRoute))
const developerRouteWithVersionAndLanguage = `/en/${developerRouteWithVersion}`
allRedirects[developerRouteWithVersion] = newPath
allRedirects[developerRouteWithVersionAndLanguage] = newPath
}
})
return allRedirects
}