generate upto URL with 4 segments to support sites with sections (#2532)

This commit is contained in:
Taran Vohra
2024-10-18 16:34:25 +05:30
committed by GitHub
parent c3675fdfe7
commit a4b63b88c8
3 changed files with 55 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'gitbook': minor
---
Support resolution of new site URLs with sections
+48 -5
View File
@@ -31,6 +31,39 @@ describe('getURLLookupAlternatives', () => {
},
],
});
expect(getURLLookupAlternatives(new URL('https://docs.mycompany.com/a/b/c/d'))).toEqual({
revision: undefined,
changeRequest: undefined,
basePath: undefined,
urls: [
{
extraPath: 'a/b/c/d',
url: 'https://docs.mycompany.com/',
primary: false,
},
{
extraPath: 'b/c/d',
url: 'https://docs.mycompany.com/a',
primary: false,
},
{
extraPath: 'c/d',
url: 'https://docs.mycompany.com/a/b',
primary: false,
},
{
extraPath: 'd',
url: 'https://docs.mycompany.com/a/b/c',
primary: false,
},
{
extraPath: '',
url: 'https://docs.mycompany.com/a/b/c/d',
primary: true,
},
],
});
});
it('should not match before the variant for a variant url', () => {
@@ -216,29 +249,34 @@ describe('getURLLookupAlternatives', () => {
});
it('should limit depth', () => {
expect(getURLLookupAlternatives(new URL('https://docs.mycompany.com/a/b/c/d'))).toEqual({
expect(getURLLookupAlternatives(new URL('https://docs.mycompany.com/a/b/c/d/e'))).toEqual({
revision: undefined,
changeRequest: undefined,
basePath: undefined,
urls: [
{
extraPath: 'a/b/c/d',
extraPath: 'a/b/c/d/e',
url: 'https://docs.mycompany.com/',
primary: false,
},
{
extraPath: 'b/c/d',
extraPath: 'b/c/d/e',
url: 'https://docs.mycompany.com/a',
primary: false,
},
{
extraPath: 'c/d',
extraPath: 'c/d/e',
url: 'https://docs.mycompany.com/a/b',
primary: false,
},
{
extraPath: 'd',
extraPath: 'd/e',
url: 'https://docs.mycompany.com/a/b/c',
primary: false,
},
{
extraPath: 'e',
url: 'https://docs.mycompany.com/a/b/c/d',
primary: true,
},
],
@@ -269,7 +307,12 @@ describe('getURLLookupAlternatives', () => {
{
extraPath: 'c/d',
url: 'https://docs.mycompany.com/a/~/b',
primary: false,
},
{
extraPath: 'd',
primary: true,
url: 'https://docs.mycompany.com/a/~/b/c',
},
],
});
+2 -2
View File
@@ -90,8 +90,8 @@ export function getURLLookupAlternatives(input: URL) {
pushAlternative(noPathURL, url.pathname.slice(1));
}
// Otherwise match with the first two segments of the path
for (let i = 1; i <= 3; i++) {
// Otherwise match with the first four segments of the path
for (let i = 1; i <= 4; i++) {
if (pathSegments.length >= i) {
const shortURL = new URL(url);
shortURL.pathname = pathSegments.slice(0, i).join('/');