File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -114,4 +114,27 @@ describe('linkerWithOtherSpaceBasePath', () => {
114
114
'/section/variant/some/path'
115
115
) ;
116
116
} ) ;
117
+
118
+ it ( 'should return a new linker that resolves links relative to a new spaceBasePath in the current site' , ( ) => {
119
+ const otherSpaceBasePathLinker = linkerWithOtherSpaceBasePath ( root , {
120
+ spaceBasePath : '/section/variant' ,
121
+ } ) ;
122
+ expect ( otherSpaceBasePathLinker . toPathInSpace ( 'some/path' ) ) . toBe (
123
+ '/section/variant/some/path'
124
+ ) ;
125
+ } ) ;
126
+
127
+ it ( 'should use a basepath relative to the site' , ( ) => {
128
+ const otherSpaceBasePathLinker = linkerWithOtherSpaceBasePath ( siteGitBookIO , {
129
+ spaceBasePath : 'a/b' ,
130
+ } ) ;
131
+ expect ( otherSpaceBasePathLinker . toPathInSpace ( 'some/path' ) ) . toBe ( '/sitename/a/b/some/path' ) ;
132
+ } ) ;
133
+
134
+ it ( 'should use a basepath relative to the site (with trailing slash)' , ( ) => {
135
+ const otherSpaceBasePathLinker = linkerWithOtherSpaceBasePath ( siteGitBookIO , {
136
+ spaceBasePath : '/a/b' ,
137
+ } ) ;
138
+ expect ( otherSpaceBasePathLinker . toPathInSpace ( 'some/path' ) ) . toBe ( '/sitename/a/b/some/path' ) ;
139
+ } ) ;
117
140
} ) ;
Original file line number Diff line number Diff line change @@ -142,13 +142,22 @@ export function linkerWithAbsoluteURLs(linker: GitBookLinker): GitBookLinker {
142
142
*/
143
143
export function linkerWithOtherSpaceBasePath (
144
144
linker : GitBookLinker ,
145
- { spaceBasePath } : { spaceBasePath : string }
145
+ {
146
+ spaceBasePath,
147
+ } : {
148
+ /**
149
+ * The base path of the space. It should be relative to the root of the site.
150
+ */
151
+ spaceBasePath : string ;
152
+ }
146
153
) : GitBookLinker {
147
154
const newLinker : GitBookLinker = {
148
155
...linker ,
149
156
toPathInSpace ( relativePath : string ) : string {
150
- return joinPaths ( spaceBasePath , relativePath ) ;
157
+ return linker . toPathInSite ( joinPaths ( spaceBasePath , relativePath ) ) ;
151
158
} ,
159
+ // implementation matches the base linker toPathForPage, but decouples from using `this` to
160
+ // ensure we always use the updates `toPathInSpace` method.
152
161
toPathForPage ( { pages, page, anchor } ) {
153
162
return newLinker . toPathInSpace ( getPagePath ( pages , page ) ) + ( anchor ? `#${ anchor } ` : '' ) ;
154
163
} ,
You can’t perform that action at this time.
0 commit comments