Skip to content

Commit f6d53e2

Browse files
authored
Merge pull request #80 from sbardian/develop
Add trailingSlashes plugin option
2 parents 988bfd9 + 6fd5d91 commit f6d53e2

File tree

3 files changed

+100
-9
lines changed

3 files changed

+100
-9
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,21 @@ gatsby-config.js
345345
autoGenHomeLabel: `Root`,
346346
// exlude: optional, include to overwrite these default excluded pages
347347
exclude: [
348-
`/dev-404-page`,
349-
`/404`,
348+
`/dev-404-page/`,
349+
`/404/`,
350350
`/404.html`,
351-
`/offline-plugin-app-shell-fallback`,
351+
`/offline-plugin-app-shell-fallback/`,
352352
],
353353
// crumbLabelUpdates: optional, update specific crumbLabels in the path
354354
crumbLabelUpdates: [
355355
{
356356
pathname: '/book',
357357
crumbLabel: 'Books'
358358
}
359-
]
359+
],
360+
// trailingSlashes: optional, will add trailing slashes to the end
361+
// of crumb pathnames. default is false
362+
trailingSlashes: true,
360363
// usePathPrefix: optional, if you are using pathPrefix above
361364
usePathPrefix: '/blog',
362365
},

src/gatsby-node.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ exports.onCreatePage = ({ page, pathPrefix, actions }, pluginOptions) => {
33
const { createPage, deletePage } = actions
44

55
const defaultOptions = {
6+
trailingSlashes: false,
67
exclude: [
7-
`/dev-404-page`,
8-
`/404`,
8+
`/dev-404-page/`,
9+
`/404/`,
910
`/404.html`,
10-
`/offline-plugin-app-shell-fallback`,
11+
`/offline-plugin-app-shell-fallback/`,
1112
],
1213
}
1314

1415
const optionsActual = { ...defaultOptions, ...pluginOptions }
15-
const { crumbLabelUpdates = [] } = optionsActual
16+
const { crumbLabelUpdates = [], trailingSlashes } = optionsActual
1617

1718
// for pages not excludecd, create crumbs out of each section of the page path
1819
if (!optionsActual.exclude.includes(page.path)) {
@@ -34,12 +35,13 @@ exports.onCreatePage = ({ page, pathPrefix, actions }, pluginOptions) => {
3435
]
3536
} else if (index !== 0 && split !== '') {
3637
// remaining sections of path
38+
3739
acc += `/${split}`
3840
const n = acc.lastIndexOf('/')
3941

4042
// update crumbLabel for any crumbLabelUpdates otherwise use path
4143
let crumbLabel = acc.slice(n + 1).replace(/%20/g, ' ')
42-
crumbLabelUpdates.forEach(labelUpdate => {
44+
crumbLabelUpdates.forEach((labelUpdate) => {
4345
if (labelUpdate.pathname === acc) {
4446
crumbLabel = labelUpdate.crumbLabel
4547
}
@@ -57,6 +59,17 @@ exports.onCreatePage = ({ page, pathPrefix, actions }, pluginOptions) => {
5759
crumbs = [...crumbs]
5860
}
5961
})
62+
63+
// if trailingSlashes add a trailing slash to the end of
64+
// each crumb. Excluding root (/) and crumbs including a "." (ex: 404.html)
65+
if (trailingSlashes) {
66+
crumbs.forEach((crumb, index) => {
67+
if (index !== 0 && crumb.pathname.indexOf('.') === -1) {
68+
crumbs[index].pathname = `${crumbs[index].pathname}/`
69+
}
70+
})
71+
}
72+
6073
const breadcrumb = {
6174
location: page.path,
6275
crumbs,

src/gatsby-node.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,52 @@ const calledWithLongLabelUpdates = {
106106
},
107107
}
108108

109+
const calledWithTrailingSlashes = {
110+
path: '/long/test',
111+
context: {
112+
breadcrumb: {
113+
crumbs: [
114+
{
115+
crumbLabel: 'Home',
116+
pathname: '/',
117+
},
118+
{
119+
crumbLabel: 'long',
120+
pathname: '/long/',
121+
},
122+
{
123+
crumbLabel: 'test',
124+
pathname: '/long/test/',
125+
},
126+
],
127+
location: '/long/test',
128+
},
129+
},
130+
}
131+
132+
const calledWithTrailingSlashesLabelUpdates = {
133+
path: '/long/test',
134+
context: {
135+
breadcrumb: {
136+
crumbs: [
137+
{
138+
crumbLabel: 'Home',
139+
pathname: '/',
140+
},
141+
{
142+
crumbLabel: 'LONG',
143+
pathname: '/long/',
144+
},
145+
{
146+
crumbLabel: 'test',
147+
pathname: '/long/test/',
148+
},
149+
],
150+
location: '/long/test',
151+
},
152+
},
153+
}
154+
109155
afterEach(() => {
110156
actions.createPage.mockClear()
111157
actions.deletePage.mockClear()
@@ -202,4 +248,33 @@ describe('AutoGen crumbs: ', () => {
202248
expect(actions.deletePage).not.toHaveBeenCalled()
203249
expect(actions.createPage).not.toHaveBeenCalled()
204250
})
251+
it('should generate crumbs with trailingSlashes', () => {
252+
onCreatePage(
253+
{ page: mockPageLongPath, actions },
254+
{
255+
useAutoGen: true,
256+
trailingSlashes: true,
257+
},
258+
)
259+
expect(actions.deletePage).toHaveBeenCalledTimes(1)
260+
expect(actions.deletePage).toHaveBeenCalledWith(mockPageLongPath)
261+
expect(actions.createPage).toHaveBeenCalledTimes(1)
262+
expect(actions.createPage).toHaveBeenCalledWith(calledWithTrailingSlashes)
263+
})
264+
it('should generate crumbs with trailingSlashes and label updates', () => {
265+
onCreatePage(
266+
{ page: mockPageLongPath, actions },
267+
{
268+
useAutoGen: true,
269+
trailingSlashes: true,
270+
crumbLabelUpdates: [{ pathname: '/long', crumbLabel: 'LONG' }],
271+
},
272+
)
273+
expect(actions.deletePage).toHaveBeenCalledTimes(1)
274+
expect(actions.deletePage).toHaveBeenCalledWith(mockPageLongPath)
275+
expect(actions.createPage).toHaveBeenCalledTimes(1)
276+
expect(actions.createPage).toHaveBeenCalledWith(
277+
calledWithTrailingSlashesLabelUpdates,
278+
)
279+
})
205280
})

0 commit comments

Comments
 (0)