File tree Expand file tree Collapse file tree 2 files changed +79
-0
lines changed
packages/gatsby-plugin-page-creator/src Expand file tree Collapse file tree 2 files changed +79
-0
lines changed Original file line number Diff line number Diff line change @@ -265,6 +265,74 @@ describe(`derive-path`, () => {
265
265
) . toEqual ( `foo/dolores/[...name]` )
266
266
} )
267
267
268
+ it ( `supports index paths` , ( ) => {
269
+ expect (
270
+ derivePath (
271
+ `{Page.path}` ,
272
+ {
273
+ path : `/` ,
274
+ } ,
275
+ reporter
276
+ ) . derivedPath
277
+ ) . toEqual ( `index` )
278
+ expect (
279
+ derivePath (
280
+ `{Page.path}.js` ,
281
+ {
282
+ path : `/` ,
283
+ } ,
284
+ reporter
285
+ ) . derivedPath
286
+ ) . toEqual ( `index.js` )
287
+ expect (
288
+ derivePath (
289
+ `foo/{Page.path}` ,
290
+ {
291
+ path : `/` ,
292
+ } ,
293
+ reporter
294
+ ) . derivedPath
295
+ ) . toEqual ( `foo` )
296
+ expect (
297
+ derivePath (
298
+ `foo/{Page.path}/bar` ,
299
+ {
300
+ path : `/` ,
301
+ } ,
302
+ reporter
303
+ ) . derivedPath
304
+ ) . toEqual ( `foo/bar` )
305
+ expect (
306
+ derivePath (
307
+ `foo/{Page.pathOne}/{Page.pathTwo}` ,
308
+ {
309
+ pathOne : `/` ,
310
+ pathTwo : `bar` ,
311
+ } ,
312
+ reporter
313
+ ) . derivedPath
314
+ ) . toEqual ( `foo/bar` )
315
+ expect (
316
+ derivePath (
317
+ `foo/{Page.pathOne}/{Page.pathTwo}` ,
318
+ {
319
+ pathOne : `/` ,
320
+ pathTwo : `/bar` ,
321
+ } ,
322
+ reporter
323
+ ) . derivedPath
324
+ ) . toEqual ( `foo/bar` )
325
+ expect (
326
+ derivePath (
327
+ `foo/{Page.path}/[...name]` ,
328
+ {
329
+ path : `/` ,
330
+ } ,
331
+ reporter
332
+ ) . derivedPath
333
+ ) . toEqual ( `foo/[...name]` )
334
+ } )
335
+
268
336
it ( `handles special chars` , ( ) => {
269
337
expect (
270
338
derivePath (
Original file line number Diff line number Diff line change @@ -6,9 +6,12 @@ import {
6
6
extractAllCollectionSegments ,
7
7
switchToPeriodDelimiters ,
8
8
stripTrailingSlash ,
9
+ removeFileExtension ,
9
10
} from "./path-utils"
10
11
11
12
const doubleForwardSlashes = / \/ \/ + / g
13
+ // Match 0 or 1 of "/"
14
+ const indexRoute = / ^ \/ ? $ /
12
15
13
16
// Generates the path for the page from the file path
14
17
// product/{Product.id} => /product/:id, pulls from nodes.id
@@ -64,6 +67,14 @@ export function derivePath(
64
67
// 4. Remove double forward slashes that could occur in the final URL
65
68
modifiedPath = modifiedPath . replace ( doubleForwardSlashes , `/` )
66
69
70
+ // 5. Remove trailing slashes that could occur in the final URL
71
+ modifiedPath = stripTrailingSlash ( modifiedPath )
72
+
73
+ // 6. If the final URL appears to be an index path, use the "index" file naming convention
74
+ if ( indexRoute . test ( removeFileExtension ( modifiedPath ) ) ) {
75
+ modifiedPath = `index${ modifiedPath } `
76
+ }
77
+
67
78
const derivedPath = modifiedPath
68
79
69
80
return {
You can’t perform that action at this time.
0 commit comments