correct how legacy enterprise links are rewritten (#25183)

Co-authored-by: Mike Surowiec <mikesurowiec@users.noreply.github.com>
This commit is contained in:
Peter Bengtsson 2022-02-09 11:09:42 -05:00 коммит произвёл GitHub
Родитель 64f9826bdf
Коммит 987fef9315
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 40 добавлений и 15 удалений

Просмотреть файл

@ -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 <a> 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)
}

11
tests/fixtures/page-with-deprecated-enterprise-links.md поставляемый Normal file
Просмотреть файл

@ -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)

Просмотреть файл

@ -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