Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-humans-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': minor
---

Support resolution of new site URLs with sections
53 changes: 48 additions & 5 deletions packages/gitbook/src/lib/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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,
},
],
Expand Down Expand Up @@ -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',
},
],
});
Expand Down
4 changes: 2 additions & 2 deletions packages/gitbook/src/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down
Loading