Skip to content

Commit a4b63b8

Browse files
authored
generate upto URL with 4 segments to support sites with sections (#2532)
1 parent c3675fd commit a4b63b8

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

.changeset/grumpy-humans-yawn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'gitbook': minor
3+
---
4+
5+
Support resolution of new site URLs with sections

packages/gitbook/src/lib/middleware.test.ts

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,39 @@ describe('getURLLookupAlternatives', () => {
3131
},
3232
],
3333
});
34+
35+
expect(getURLLookupAlternatives(new URL('https://docs.mycompany.com/a/b/c/d'))).toEqual({
36+
revision: undefined,
37+
changeRequest: undefined,
38+
basePath: undefined,
39+
urls: [
40+
{
41+
extraPath: 'a/b/c/d',
42+
url: 'https://docs.mycompany.com/',
43+
primary: false,
44+
},
45+
{
46+
extraPath: 'b/c/d',
47+
url: 'https://docs.mycompany.com/a',
48+
primary: false,
49+
},
50+
{
51+
extraPath: 'c/d',
52+
url: 'https://docs.mycompany.com/a/b',
53+
primary: false,
54+
},
55+
{
56+
extraPath: 'd',
57+
url: 'https://docs.mycompany.com/a/b/c',
58+
primary: false,
59+
},
60+
{
61+
extraPath: '',
62+
url: 'https://docs.mycompany.com/a/b/c/d',
63+
primary: true,
64+
},
65+
],
66+
});
3467
});
3568

3669
it('should not match before the variant for a variant url', () => {
@@ -216,29 +249,34 @@ describe('getURLLookupAlternatives', () => {
216249
});
217250

218251
it('should limit depth', () => {
219-
expect(getURLLookupAlternatives(new URL('https://docs.mycompany.com/a/b/c/d'))).toEqual({
252+
expect(getURLLookupAlternatives(new URL('https://docs.mycompany.com/a/b/c/d/e'))).toEqual({
220253
revision: undefined,
221254
changeRequest: undefined,
222255
basePath: undefined,
223256
urls: [
224257
{
225-
extraPath: 'a/b/c/d',
258+
extraPath: 'a/b/c/d/e',
226259
url: 'https://docs.mycompany.com/',
227260
primary: false,
228261
},
229262
{
230-
extraPath: 'b/c/d',
263+
extraPath: 'b/c/d/e',
231264
url: 'https://docs.mycompany.com/a',
232265
primary: false,
233266
},
234267
{
235-
extraPath: 'c/d',
268+
extraPath: 'c/d/e',
236269
url: 'https://docs.mycompany.com/a/b',
237270
primary: false,
238271
},
239272
{
240-
extraPath: 'd',
273+
extraPath: 'd/e',
241274
url: 'https://docs.mycompany.com/a/b/c',
275+
primary: false,
276+
},
277+
{
278+
extraPath: 'e',
279+
url: 'https://docs.mycompany.com/a/b/c/d',
242280
primary: true,
243281
},
244282
],
@@ -269,7 +307,12 @@ describe('getURLLookupAlternatives', () => {
269307
{
270308
extraPath: 'c/d',
271309
url: 'https://docs.mycompany.com/a/~/b',
310+
primary: false,
311+
},
312+
{
313+
extraPath: 'd',
272314
primary: true,
315+
url: 'https://docs.mycompany.com/a/~/b/c',
273316
},
274317
],
275318
});

packages/gitbook/src/lib/middleware.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ export function getURLLookupAlternatives(input: URL) {
9090
pushAlternative(noPathURL, url.pathname.slice(1));
9191
}
9292

93-
// Otherwise match with the first two segments of the path
94-
for (let i = 1; i <= 3; i++) {
93+
// Otherwise match with the first four segments of the path
94+
for (let i = 1; i <= 4; i++) {
9595
if (pathSegments.length >= i) {
9696
const shortURL = new URL(url);
9797
shortURL.pathname = pathSegments.slice(0, i).join('/');

0 commit comments

Comments
 (0)