Fix share links resulting in errors because of (#177)

This commit is contained in:
Samy Pessé
2024-02-20 14:29:14 +01:00
committed by GitHub
parent a2d967e98e
commit 09de40002a
4 changed files with 39 additions and 6 deletions
+13
View File
@@ -8,14 +8,17 @@ describe('getURLLookupAlternatives', () => {
{
extraPath: 'a/b/c',
url: 'https://docs.mycompany.com/',
primary: false,
},
{
extraPath: 'b/c',
url: 'https://docs.mycompany.com/a',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/a/b/c',
primary: true,
},
]);
});
@@ -27,10 +30,12 @@ describe('getURLLookupAlternatives', () => {
{
url: 'https://test.gitbook.io/v/variant',
extraPath: 'space',
primary: false,
},
{
url: 'https://test.gitbook.io/v/variant/space',
extraPath: '',
primary: true,
},
]);
});
@@ -42,10 +47,12 @@ describe('getURLLookupAlternatives', () => {
{
extraPath: 'hello',
url: 'https://docs.mycompany.com/~/revisions/id',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/~/revisions/id/hello',
primary: true,
},
]);
});
@@ -57,10 +64,12 @@ describe('getURLLookupAlternatives', () => {
{
extraPath: 'hello',
url: 'https://docs.mycompany.com/~/changes/id',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/~/changes/id/hello',
primary: true,
},
]);
});
@@ -70,10 +79,12 @@ describe('getURLLookupAlternatives', () => {
{
extraPath: 'hello',
url: 'https://docs.mycompany.com/',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/hello',
primary: true,
},
]);
});
@@ -83,10 +94,12 @@ describe('getURLLookupAlternatives', () => {
{
extraPath: 'hello',
url: 'https://docs.mycompany.com/',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/hello',
primary: true,
},
]);
});
+6 -5
View File
@@ -4,14 +4,14 @@
*/
export function getURLLookupAlternatives(input: URL) {
const url = normalizeURL(input);
const alternatives: Array<{ url: string; extraPath: string }> = [];
const alternatives: Array<{ url: string; extraPath: string; primary: boolean }> = [];
const pushAlternative = (url: URL, extraPath: string) => {
const existing = alternatives.find((alt) => alt.url === url.toString());
const pushAlternative = (adding: URL, extraPath: string) => {
const existing = alternatives.find((alt) => alt.url === adding.toString());
if (existing) {
if (existing.extraPath !== extraPath) {
throw new Error(
`Invalid extraPath ${extraPath} for url ${url.toString()}, already set to ${
`Invalid extraPath ${extraPath} for url ${adding.toString()}, already set to ${
existing.extraPath
}`,
);
@@ -20,8 +20,9 @@ export function getURLLookupAlternatives(input: URL) {
}
alternatives.push({
url: url.toString(),
url: adding.toString(),
extraPath,
primary: adding.toString() === url.toString(),
});
};
+6 -1
View File
@@ -480,7 +480,12 @@ async function lookupSpaceByAPI(
});
if ('error' in data) {
return data;
if (alternative.primary) {
// We only return an error for the primary alternative (full URL),
// as other parts could result in errors due to the URL being incomplete (share links, etc).
return data;
}
return null;
}
if ('redirect' in data) {
+14
View File
@@ -219,6 +219,20 @@ const testCases: TestsCase[] = [
},
],
},
{
name: 'Share links',
baseUrl: 'https://gitbook.gitbook.io/test-share-links/',
tests: [
{
name: 'Valid link',
url: 'Fc6mMII9FKgnwm7qqynx/',
},
{
name: 'Invalid link',
url: 'invalid/',
},
],
},
];
console.log(`Starting visual testing with ${getTargetURL()}...`);