зеркало из https://github.com/github/docs.git
correct how legacy enterprise links are rewritten (#25183)
Co-authored-by: Mike Surowiec <mikesurowiec@users.noreply.github.com>
This commit is contained in:
Родитель
64f9826bdf
Коммит
987fef9315
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Загрузка…
Ссылка в новой задаче