diff --git a/.changeset/grumpy-humans-yawn.md b/.changeset/grumpy-humans-yawn.md new file mode 100644 index 0000000000..7180eb8207 --- /dev/null +++ b/.changeset/grumpy-humans-yawn.md @@ -0,0 +1,5 @@ +--- +'gitbook': minor +--- + +Support resolution of new site URLs with sections diff --git a/packages/gitbook/src/lib/middleware.test.ts b/packages/gitbook/src/lib/middleware.test.ts index 6723c65982..86f03ca5b9 100644 --- a/packages/gitbook/src/lib/middleware.test.ts +++ b/packages/gitbook/src/lib/middleware.test.ts @@ -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', }, ], }); diff --git a/packages/gitbook/src/lib/middleware.ts b/packages/gitbook/src/lib/middleware.ts index c3aef41a97..88d50562f9 100644 --- a/packages/gitbook/src/lib/middleware.ts +++ b/packages/gitbook/src/lib/middleware.ts @@ -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('/');