Skip to content

Commit ca18c7a

Browse files
committed
Simplify
1 parent 775d5ea commit ca18c7a

File tree

4 files changed

+22
-59
lines changed

4 files changed

+22
-59
lines changed
+18-28
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import { describe, expect, it } from 'bun:test';
2-
import { appendBasePathToLinker, createLinker } from './links';
2+
import { createLinker } from './links';
33

44
const root = createLinker({
55
host: 'docs.company.com',
6-
pathname: '/',
6+
spaceBasePath: '/',
7+
siteBasePath: '/',
78
});
89

910
const variantInSection = createLinker({
1011
host: 'docs.company.com',
11-
pathname: '/section/variant',
12+
spaceBasePath: '/section/variant',
13+
siteBasePath: '/',
14+
});
15+
16+
const siteGitBookIO = createLinker({
17+
host: 'org.gitbook.io',
18+
spaceBasePath: '/sitename/variant/',
19+
siteBasePath: '/sitename/',
1220
});
1321

1422
describe('toPathInContent', () => {
@@ -23,6 +31,13 @@ describe('toPathInContent', () => {
2331
});
2432
});
2533

34+
describe('toPathInSite', () => {
35+
it('should return the correct path', () => {
36+
expect(root.toPathInSite('some/path')).toBe('/some/path');
37+
expect(siteGitBookIO.toPathInSite('some/path')).toBe('/sitename/some/path');
38+
});
39+
});
40+
2641
describe('toAbsoluteURL', () => {
2742
it('should return the correct path', () => {
2843
expect(root.toAbsoluteURL('some/path')).toBe('https://docs.company.com/some/path');
@@ -31,28 +46,3 @@ describe('toAbsoluteURL', () => {
3146
);
3247
});
3348
});
34-
35-
describe('appendBasePathToLinker', () => {
36-
const prefixedRoot = appendBasePathToLinker(root, '/section/variant');
37-
const prefixedVariantInSection = appendBasePathToLinker(variantInSection, '/base');
38-
39-
describe('toPathInContent', () => {
40-
it('should return the correct path', () => {
41-
expect(prefixedRoot.toPathInSpace('some/path')).toBe('/section/variant/some/path');
42-
expect(prefixedVariantInSection.toPathInSpace('some/path')).toBe(
43-
'/section/variant/base/some/path'
44-
);
45-
});
46-
});
47-
48-
describe('toAbsoluteURL', () => {
49-
it('should return the correct path', () => {
50-
expect(prefixedRoot.toAbsoluteURL('some/path')).toBe(
51-
'https://docs.company.com/some/path'
52-
);
53-
expect(prefixedVariantInSection.toAbsoluteURL('some/path')).toBe(
54-
'https://docs.company.com/some/path'
55-
);
56-
});
57-
});
58-
});

packages/gitbook-v2/src/lib/links.ts

-28
Original file line numberDiff line numberDiff line change
@@ -93,34 +93,6 @@ export function createLinker(
9393
return linker;
9494
}
9595

96-
/**
97-
* Append a prefix to a linker.
98-
*/
99-
export function appendBasePathToLinker(linker: GitBookLinker, basePath: string): GitBookLinker {
100-
const linkerWithPrefix: GitBookLinker = {
101-
toPathInSpace(relativePath: string): string {
102-
return linker.toPathInSpace(joinPaths(basePath, relativePath));
103-
},
104-
105-
toAbsoluteURL(absolutePath: string): string {
106-
return linker.toAbsoluteURL(absolutePath);
107-
},
108-
109-
toPathForPage({ pages, page, anchor }) {
110-
return (
111-
linkerWithPrefix.toPathInSpace(getPagePath(pages, page)) +
112-
(anchor ? `#${anchor}` : '')
113-
);
114-
},
115-
116-
toLinkForContent(url: string): string {
117-
return linker.toLinkForContent(url);
118-
},
119-
};
120-
121-
return linkerWithPrefix;
122-
}
123-
12496
function joinPaths(prefix: string, path: string): string {
12597
const prefixPath = prefix.endsWith('/') ? prefix : `${prefix}/`;
12698
const suffixPath = path.startsWith('/') ? path.slice(1) : path;

packages/gitbook-v2/src/middleware.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
163163
const requestHeaders = new Headers(request.headers);
164164
requestHeaders.set(MiddlewareHeaders.RouteType, routeType);
165165
requestHeaders.set(MiddlewareHeaders.URLMode, mode);
166-
requestHeaders.set(MiddlewareHeaders.SiteURL, `${siteURL.origin}${data.basePath}`);
167-
requestHeaders.set(MiddlewareHeaders.SiteURLData, JSON.stringify(data));
166+
requestHeaders.set(MiddlewareHeaders.SiteURL, `${siteURL.origin}${siteURLData.basePath}`);
167+
requestHeaders.set(MiddlewareHeaders.SiteURLData, JSON.stringify(siteURLData));
168168

169169
// Preview of customization/theme
170170
const customization = siteURL.searchParams.get('customization');

packages/gitbook/src/lib/references.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ async function resolveContentRefInSpace(
323323
);
324324
const linker = createLinker({
325325
host: baseURL.host,
326-
pathname: baseURL.pathname,
326+
spaceBasePath: baseURL.pathname,
327+
siteBasePath: baseURL.pathname,
327328
});
328329

329330
const resolved = await resolveContentRef(

0 commit comments

Comments
 (0)