Skip to content

Commit

Permalink
Align the revisions/changes path for multi-id with the normal one (#2769
Browse files Browse the repository at this point in the history
)
  • Loading branch information
SamyPesse authored Jan 23, 2025
1 parent d3e573c commit d370a3f
Showing 2 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-pillows-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gitbook': patch
---

Update the routes for changes/revisions in multi-id mode to match the normal mode
20 changes: 12 additions & 8 deletions packages/gitbook/src/middleware.ts
Original file line number Diff line number Diff line change
@@ -482,8 +482,8 @@ async function lookupSiteInMultiMode(request: NextRequest, url: URL): Promise<Lo
*
* The format of the path is:
* - /~space|~site/:id/:path
* - /~space|~site/:id/~changes/:changeId/:path
* - /~space|~site/:id/~revisions/:revisionId/:path
* - /~space|~site/:id/~/changes/:changeId/:path
* - /~space|~site/:id/~/revisions/:revisionId/:path
*/
async function lookupSiteOrSpaceInMultiIdMode(
request: NextRequest,
@@ -492,13 +492,17 @@ async function lookupSiteOrSpaceInMultiIdMode(
const basePathParts: string[] = [];
const pathSegments = url.pathname.slice(1).split('/');

const eatPathId = (prefix: string): string | undefined => {
if (pathSegments[0] !== prefix || pathSegments.length < 2) {
const eatPathId = (...prefixes: string[]): string | undefined => {
const match = prefixes.every((prefix, index) => pathSegments[index] === prefix);
if (!match || pathSegments.length < prefixes.length + 1) {
return;
}

const prefixSegment = pathSegments.shift();
basePathParts.push(prefixSegment!);
// Remove the prefix from the path segments
pathSegments.splice(0, prefixes.length);

// Add the prefix to the base path
basePathParts.push(...prefixes);

const id = pathSegments.shift();
basePathParts.push(id!);
@@ -524,8 +528,8 @@ async function lookupSiteOrSpaceInMultiIdMode(
}

// Extract the change request or revision ID from the path
const changeRequestId = eatPathId('~changes');
const revisionId = eatPathId('~revisions');
const changeRequestId = eatPathId('~', 'changes');
const revisionId = eatPathId('~', 'revisions');

// Get the auth token from the URL query
const AUTH_TOKEN_QUERY = 'token';

0 comments on commit d370a3f

Please sign in to comment.