From b5117580571fece2257df8dfc439a2c9f5c20fbb Mon Sep 17 00:00:00 2001 From: Sarah Schneider Date: Thu, 14 Jan 2021 13:34:32 -0500 Subject: [PATCH] add remove-fpt helper function --- lib/all-products.js | 3 ++- lib/all-versions.js | 2 +- lib/get-link-data.js | 3 ++- lib/permalink.js | 3 ++- lib/redirects/get-old-paths-from-permalink.js | 26 ++++++------------- lib/redirects/permalinks.js | 7 ++++- lib/redirects/precompile.js | 2 ++ lib/remove-fpt-from-path.js | 9 +++++++ lib/rewrite-local-links.js | 3 ++- lib/site-tree.js | 3 ++- middleware/breadcrumbs.js | 13 +++------- middleware/contextualizers/rest.js | 5 ++-- 12 files changed, 42 insertions(+), 37 deletions(-) create mode 100644 lib/remove-fpt-from-path.js diff --git a/lib/all-products.js b/lib/all-products.js index e92bdf2d55..e56ffc21b3 100644 --- a/lib/all-products.js +++ b/lib/all-products.js @@ -7,6 +7,7 @@ const yaml = require('js-yaml') const contentDir = path.join(process.cwd(), 'content') const frontmatter = require('@github-docs/frontmatter') const getApplicableVersions = require('./get-applicable-versions') +const removeFPTFromPath = require('./remove-fpt-from-path') // the product order is determined by data/products.yml const productsFile = path.join(process.cwd(), 'data/products.yml') @@ -46,7 +47,7 @@ sortedProductIds.forEach(productId => { const toc = slash(path.join(dir, 'index.md')) const { data } = frontmatter(fs.readFileSync(toc, 'utf8')) const applicableVersions = getApplicableVersions(data.versions, toc) - const href = `/${applicableVersions[0]}/${productId}` + const href = removeFPTFromPath(`/${applicableVersions[0]}/${productId}`) internalProducts[productId] = { id: productId, diff --git a/lib/all-versions.js b/lib/all-versions.js index d7fee98899..64fe6c9c8d 100644 --- a/lib/all-versions.js +++ b/lib/all-versions.js @@ -10,7 +10,7 @@ const plans = [ { // free-pro-team is **not** a user-facing version and is stripped from URLs. // See lib/remove-fpt-from-path.js for details. plan: 'free-pro-team', - planTitle: process.env.FEATURE_REMOVE_FPT ? 'GitHub.com' : 'Free, Pro, and Team', + planTitle: 'GitHub.com', releases: [latestNonNumberedRelease], latestRelease: latestNonNumberedRelease, nonEnterpriseDefault: true, // permanent way to refer to this plan if the name changes diff --git a/lib/get-link-data.js b/lib/get-link-data.js index 6647ae85ce..0d7a078d0a 100644 --- a/lib/get-link-data.js +++ b/lib/get-link-data.js @@ -1,6 +1,7 @@ const path = require('path') const findPage = require('./find-page') const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version') +const removeFPTFromPath = require('./remove-fpt-from-path') // rawLinks is an array of paths: [ '/foo' ] // we need to convert it to an array of localized objects: [ { href: '/en/foo', title: 'Foo', intro: 'Description here' } ] @@ -12,7 +13,7 @@ module.exports = async (rawLinks, context, additionalProperties = []) => { for (const link of rawLinks) { const linkPath = link.href || link const version = context.currentVersion === 'homepage' ? nonEnterpriseDefaultVersion : context.currentVersion - const href = path.join('/', context.currentLanguage, version, linkPath) + const href = removeFPTFromPath(path.join('/', context.currentLanguage, version, linkPath)) const linkedPage = findPage(href, context.pages, context.redirects) if (!linkedPage) continue diff --git a/lib/permalink.js b/lib/permalink.js index 6376c237cd..9a4950bda1 100644 --- a/lib/permalink.js +++ b/lib/permalink.js @@ -3,6 +3,7 @@ const path = require('path') const patterns = require('./patterns') const getApplicableVersions = require('./get-applicable-versions') const allVersions = require('./all-versions') +const removeFPTFromPath = require('./remove-fpt-from-path') class Permalink { constructor (languageCode, pageVersion, relativePath, title) { @@ -13,7 +14,7 @@ class Permalink { const permalinkSuffix = this.constructor.relativePathToSuffix(relativePath) - this.href = path.join('/', languageCode, pageVersion, permalinkSuffix) + this.href = removeFPTFromPath(path.join('/', languageCode, pageVersion, permalinkSuffix)) .replace(patterns.trailingSlash, '$1') this.pageVersionTitle = allVersions[pageVersion].versionTitle diff --git a/lib/redirects/get-old-paths-from-permalink.js b/lib/redirects/get-old-paths-from-permalink.js index a8781a72fb..8ee0e73db4 100644 --- a/lib/redirects/get-old-paths-from-permalink.js +++ b/lib/redirects/get-old-paths-from-permalink.js @@ -1,5 +1,5 @@ const { latest, lastReleaseWithLegacyFormat } = require('../enterprise-server-releases') -const { getPathWithoutLanguage, getPathWithLanguage } = require('../path-utils') +const { getPathWithoutLanguage, getPathWithLanguage, getVersionStringFromPath } = require('../path-utils') const patterns = require('../patterns') const versionSatisfiesRange = require('../version-satisfies-range') const currentlySupportedVersions = Object.keys(require('../all-versions')) @@ -11,6 +11,13 @@ const nonEnterpriseDefaultVersion = require('../non-enterprise-default-version') module.exports = function getOldPathsFromPath (currentPath, languageCode, currentVersion) { const oldPaths = new Set() + // This only applies to Dotcom paths, so no need to determine whether the version is deprecated + // create old path /free-pro-team@latest/github from new path /github + if (getVersionStringFromPath(currentPath) === nonEnterpriseDefaultVersion && !currentPath.includes(nonEnterpriseDefaultVersion)) { + oldPaths.add(currentPath + .replace(`/${languageCode}/`, `/${languageCode}/${nonEnterpriseDefaultVersion}/`)) + } + // ------ BEGIN LEGACY VERSION FORMAT REPLACEMENTS ------// // These remain relevant to handle legacy-formatted frontmatter redirects // and archived versions paths. @@ -57,23 +64,6 @@ module.exports = function getOldPathsFromPath (currentPath, languageCode, curren // ------ BEGIN MODERN VERSION FORMAT REPLACEMENTS ------// if (currentlySupportedVersions.includes(currentVersion) || versionSatisfiesRange(currentVersion, `>${lastReleaseWithLegacyFormat}`)) { (new Set(oldPaths)).forEach(oldPath => { - if (!process.env.FEATURE_REMOVE_FPT) { - // create old path /github from new path /free-pro-team@latest/github - oldPaths.add(oldPath - .replace(`/${nonEnterpriseDefaultVersion}`, '')) - - // create old path /free-pro-team/github from new path /free-pro-team@latest/github - oldPaths.add(oldPath - .replace('@latest', '')) - } - - // TODO THIS ONE IS TRICKY BECAUSE OF VERSIONS TO ENABLE - // if (process.env.FEATURE_REMOVE_FPT) { - // // create old path /free-pro-team@latest/github from new path /github - // oldPaths.add(oldPath - // .replace(`/${nonEnterpriseDefaultVersion}`, '')) - // } - // create old path /enterprise/ from new path /enterprise-server@ oldPaths.add(oldPath .replace(/\/enterprise-server@(\d)/, '/enterprise/$1')) diff --git a/lib/redirects/permalinks.js b/lib/redirects/permalinks.js index 82cdf9706e..5b21948568 100644 --- a/lib/redirects/permalinks.js +++ b/lib/redirects/permalinks.js @@ -4,6 +4,7 @@ const supportedVersions = new Set(Object.keys(require('../all-versions'))) const getOldPathsFromPermalink = require('./get-old-paths-from-permalink') const { getVersionStringFromPath } = require('../path-utils') const { getNewVersionedPath } = require('../old-versions-utils') +const removeFPTFromPath = require('../remove-fpt-from-path') module.exports = function generateRedirectsForPermalinks (permalinks, redirectFrontmatter) { // account for Array or String frontmatter entries @@ -18,6 +19,10 @@ module.exports = function generateRedirectsForPermalinks (permalinks, redirectFr // get an array of possible old paths, e.g., /desktop/guides/ from current permalink /desktop/ const possibleOldPaths = getOldPathsFromPermalink(permalink.href, permalink.languageCode, permalink.pageVersion) + if (permalink.href === '/en/rest/reference/users') { + console.log(possibleOldPaths) + } + // for each old path, add a redirect to the current permalink possibleOldPaths.forEach(oldPath => { redirects[oldPath] = permalink.href @@ -37,7 +42,7 @@ module.exports = function generateRedirectsForPermalinks (permalinks, redirectFr // get the old path for the current permalink version let versionedFrontmatterOldPath = path.join('/', permalink.languageCode, getNewVersionedPath(frontmatterOldPath)) const versionFromPath = getVersionStringFromPath(versionedFrontmatterOldPath) - versionedFrontmatterOldPath = versionedFrontmatterOldPath.replace(versionFromPath, permalink.pageVersion) + versionedFrontmatterOldPath = removeFPTFromPath(versionedFrontmatterOldPath.replace(versionFromPath, permalink.pageVersion)) // add it to the redirects object redirects[versionedFrontmatterOldPath] = permalink.href diff --git a/lib/redirects/precompile.js b/lib/redirects/precompile.js index f61f124375..99a1cd87e0 100755 --- a/lib/redirects/precompile.js +++ b/lib/redirects/precompile.js @@ -58,5 +58,7 @@ module.exports = function precompileRedirects (pageList, pageMap) { } }) + require('fs').writeFileSync('.redirect-cache', JSON.stringify(allRedirects, null, 2)) + return allRedirects } diff --git a/lib/remove-fpt-from-path.js b/lib/remove-fpt-from-path.js new file mode 100644 index 0000000000..8bb72f802d --- /dev/null +++ b/lib/remove-fpt-from-path.js @@ -0,0 +1,9 @@ +const slash = require('slash') +const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version') + +// This is a convenience function to remove free-pro-team@latest from all +// **user-facing** aspects of the site (particularly URLs) while continuing to support +// free-pro-team@latest as a version both in the codebase and in content/data files. +module.exports = function removeFPTFromPath (path) { + return slash(path.replace(`/${nonEnterpriseDefaultVersion}`, '')) +} diff --git a/lib/rewrite-local-links.js b/lib/rewrite-local-links.js index a13c08317e..76e30b6264 100644 --- a/lib/rewrite-local-links.js +++ b/lib/rewrite-local-links.js @@ -9,6 +9,7 @@ const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version') const allVersions = require('./all-versions') const supportedVersions = Object.keys(allVersions) const supportedPlans = Object.values(allVersions).map(v => v.plan) +const removeFPTFromPath = require('./remove-fpt-from-path') // Content authors write links like `/some/article/path`, but they need to be // rewritten on the fly to match the current language and page version @@ -79,7 +80,7 @@ function getNewHref (link, languageCode, version) { // ------ END ONE-OFF OVERRIDES ------// // update the version in the link - newHref = newHref.replace(versionFromHref, version) + newHref = removeFPTFromPath(newHref.replace(versionFromHref, version)) } newHref = newHref.replace(patterns.trailingSlash, '$1') diff --git a/lib/site-tree.js b/lib/site-tree.js index 66c64ab2e1..d70cb13213 100644 --- a/lib/site-tree.js +++ b/lib/site-tree.js @@ -6,6 +6,7 @@ const allVersions = Object.keys(require('./all-versions')) const { getVersionStringFromPath } = require('./path-utils') const getApplicableVersions = require('./get-applicable-versions') const findPage = require('./find-page') +const removeFPTFromPath = require('./remove-fpt-from-path') // This module builds a localized tree of every page on the site // It includes single-source pages that have different variants @@ -47,7 +48,7 @@ module.exports = async function buildSiteTree (pageMap, site, redirects) { // item.hrefs have a default version via lib/all-products, so update to the current version const versionFromPath = getVersionStringFromPath(item.href) - const versionedProductHref = path.join('/', languageCode, item.href.replace(versionFromPath, version)) + const versionedProductHref = removeFPTFromPath(path.join('/', languageCode, item.href.replace(versionFromPath, version))) product.categories = buildCategoriesTree(page.tocItems, versionedProductHref, pageMap, redirects, version) diff --git a/middleware/breadcrumbs.js b/middleware/breadcrumbs.js index bc77e0cfc8..6d5b90cc8c 100644 --- a/middleware/breadcrumbs.js +++ b/middleware/breadcrumbs.js @@ -29,19 +29,12 @@ module.exports = async (req, res, next) => { title: product.title } - // drop the version segment so pathParts now starts with /product - if (!process.env.FEATURE_REMOVE_FPT) { + // if this is not FPT, drop the version segment so pathParts now starts with /product + // if this is FPT, there is no version segment so pathParts already starts with /product + if (req.context.currentVersion !== nonEnterpriseDefaultVersion) { pathParts.shift() } - if (process.env.FEATURE_REMOVE_FPT) { - // if this is not FPT, drop the version segment so pathParts now starts with /product - // if this is FPT, there is no version segment so pathParts already starts with /product - if (req.context.currentVersion !== nonEnterpriseDefaultVersion) { - pathParts.shift() - } - } - if (!pathParts[1]) return next() // get category path diff --git a/middleware/contextualizers/rest.js b/middleware/contextualizers/rest.js index 81da836594..198a4d312a 100644 --- a/middleware/contextualizers/rest.js +++ b/middleware/contextualizers/rest.js @@ -1,17 +1,18 @@ const path = require('path') const rest = require('../../lib/rest') +const removeFPTFromPath = require('../../lib/remove-fpt-from-path') module.exports = async function (req, res, next) { req.context.rest = rest // link to include in `Works with GitHub Apps` notes // e.g. /ja/rest/reference/apps or /en/enterprise/2.20/user/rest/reference/apps - req.context.restGitHubAppsLink = path.join( + req.context.restGitHubAppsLink = removeFPTFromPath(path.join( '/', req.context.currentLanguage, req.context.currentVersion, '/developers/apps' - ) + )) // ignore requests to non-REST reference paths if (!req.path.includes('rest/reference')) return next()