diff --git a/lib/render-content/plugins/rewrite-local-links.js b/lib/render-content/plugins/rewrite-local-links.js index b3c9e9859c..b2b6fe2a5d 100644 --- a/lib/render-content/plugins/rewrite-local-links.js +++ b/lib/render-content/plugins/rewrite-local-links.js @@ -8,8 +8,8 @@ import nonEnterpriseDefaultVersion from '../../non-enterprise-default-version.js import { allVersions } from '../../all-versions.js' import removeFPTFromPath from '../../remove-fpt-from-path.js' import readJsonFile from '../../read-json-file.js' -const supportedVersions = Object.keys(allVersions) -const supportedPlans = Object.values(allVersions).map((v) => v.plan) + +const supportedPlans = new Set(Object.values(allVersions).map((v) => v.plan)) const externalRedirects = readJsonFile('./lib/redirects/external-sites.json') // Matches any tags with an href that starts with `/` @@ -36,18 +36,6 @@ export default function rewriteLocalLinks({ languageCode, version }) { } } -// The versions that some links might start with, which we need to know -// because if it's the case, we inject the language into the link as -// the first prefix. -// E.g. It can turn `/enterprise-cloud@latest/foo` -// into `/en/enterprise-cloud@latest/foo`. -// Using a Set to turn any lookups on this into a O(1) operation. -const allSupportedVersionsPrefixes = new Set([ - ...supportedPlans, - ...supportedVersions, - 'enterprise-server@latest', -]) - function getNewHref(node, languageCode, version) { const { href } = node.properties // Exceptions to link rewriting @@ -62,7 +50,7 @@ function getNewHref(node, languageCode, version) { // /enterprise-server/rest/reference/oauth-authorizations (this redirects to the latest version) // /enterprise-server@latest/rest/reference/oauth-authorizations (this redirects to the latest version) const firstLinkSegment = href.split('/')[1] - if (allSupportedVersionsPrefixes.has(firstLinkSegment)) { + if (supportedPlans.has(firstLinkSegment.split('@')[0])) { newHref = path.posix.join('/', languageCode, href) } diff --git a/tests/fixtures/page-with-deprecated-enterprise-links.md b/tests/fixtures/page-with-deprecated-enterprise-links.md new file mode 100644 index 0000000000..a2dc958c7d --- /dev/null +++ b/tests/fixtures/page-with-deprecated-enterprise-links.md @@ -0,0 +1,11 @@ +--- +title: Page with deprecated enterprise links +versions: + free-pro-team: '*' + enterprise-server: '*' +--- + + +- [Version 2.22](/enterprise-server@2.22) + +- [Version 3.2](/enterprise-server@3.2) diff --git a/tests/unit/page.js b/tests/unit/page.js index 8ee036dba4..5a397e4148 100644 --- a/tests/unit/page.js +++ b/tests/unit/page.js @@ -113,6 +113,32 @@ describe('Page class', () => { ).toBeGreaterThan(0) }) + // Much of this test is based on making sure we don't + // repeat the bug introduced in issue 1545. + test('rewrites links correctly for unsupported enterprise-server links', async () => { + const page = await Page.init({ + relativePath: 'page-with-deprecated-enterprise-links.md', + basePath: path.join(__dirname, '../fixtures'), + languageCode: 'en', + }) + const context = { + page: { version: `enterprise-server@${enterpriseServerReleases.latest}` }, + currentVersion: `enterprise-server@${enterpriseServerReleases.latest}`, + currentPath: '/en/page-with-deprecated-enterprise-links', + currentLanguage: 'en', + } + const rendered = await page.render(context) + // That page only contains exactly 2 links. And we can know + // exactly what we expect each one to be. + const $ = cheerio.load(rendered) + const first = $('a[href]').first() + expect(first.text()).toBe('Version 2.22') + expect(first.attr('href')).toBe('/en/enterprise-server@2.22') + const last = $('a[href]').last() + expect(last.text()).toBe('Version 3.2') + expect(last.attr('href')).toBe('/en/enterprise-server@3.2') + }) + test('rewrites links on prerendered GraphQL page include the current language prefix and version', async () => { const graphqlVersion = allVersions[`enterprise-server@${enterpriseServerReleases.latest}`].miscVersionName