Skip to content

Commit

Permalink
Merge pull request #80 from sbardian/develop
Browse files Browse the repository at this point in the history
Add trailingSlashes plugin option
  • Loading branch information
sbardian authored May 22, 2020
2 parents 988bfd9 + 6fd5d91 commit f6d53e2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 9 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,21 @@ gatsby-config.js
autoGenHomeLabel: `Root`,
// exlude: optional, include to overwrite these default excluded pages
exclude: [
`/dev-404-page`,
`/404`,
`/dev-404-page/`,
`/404/`,
`/404.html`,
`/offline-plugin-app-shell-fallback`,
`/offline-plugin-app-shell-fallback/`,
],
// crumbLabelUpdates: optional, update specific crumbLabels in the path
crumbLabelUpdates: [
{
pathname: '/book',
crumbLabel: 'Books'
}
]
],
// trailingSlashes: optional, will add trailing slashes to the end
// of crumb pathnames. default is false
trailingSlashes: true,
// usePathPrefix: optional, if you are using pathPrefix above
usePathPrefix: '/blog',
},
Expand Down
23 changes: 18 additions & 5 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ exports.onCreatePage = ({ page, pathPrefix, actions }, pluginOptions) => {
const { createPage, deletePage } = actions

const defaultOptions = {
trailingSlashes: false,
exclude: [
`/dev-404-page`,
`/404`,
`/dev-404-page/`,
`/404/`,
`/404.html`,
`/offline-plugin-app-shell-fallback`,
`/offline-plugin-app-shell-fallback/`,
],
}

const optionsActual = { ...defaultOptions, ...pluginOptions }
const { crumbLabelUpdates = [] } = optionsActual
const { crumbLabelUpdates = [], trailingSlashes } = optionsActual

// for pages not excludecd, create crumbs out of each section of the page path
if (!optionsActual.exclude.includes(page.path)) {
Expand All @@ -34,12 +35,13 @@ exports.onCreatePage = ({ page, pathPrefix, actions }, pluginOptions) => {
]
} else if (index !== 0 && split !== '') {
// remaining sections of path

acc += `/${split}`
const n = acc.lastIndexOf('/')

// update crumbLabel for any crumbLabelUpdates otherwise use path
let crumbLabel = acc.slice(n + 1).replace(/%20/g, ' ')
crumbLabelUpdates.forEach(labelUpdate => {
crumbLabelUpdates.forEach((labelUpdate) => {
if (labelUpdate.pathname === acc) {
crumbLabel = labelUpdate.crumbLabel
}
Expand All @@ -57,6 +59,17 @@ exports.onCreatePage = ({ page, pathPrefix, actions }, pluginOptions) => {
crumbs = [...crumbs]
}
})

// if trailingSlashes add a trailing slash to the end of
// each crumb. Excluding root (/) and crumbs including a "." (ex: 404.html)
if (trailingSlashes) {
crumbs.forEach((crumb, index) => {
if (index !== 0 && crumb.pathname.indexOf('.') === -1) {
crumbs[index].pathname = `${crumbs[index].pathname}/`
}
})
}

const breadcrumb = {
location: page.path,
crumbs,
Expand Down
75 changes: 75 additions & 0 deletions src/gatsby-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,52 @@ const calledWithLongLabelUpdates = {
},
}

const calledWithTrailingSlashes = {
path: '/long/test',
context: {
breadcrumb: {
crumbs: [
{
crumbLabel: 'Home',
pathname: '/',
},
{
crumbLabel: 'long',
pathname: '/long/',
},
{
crumbLabel: 'test',
pathname: '/long/test/',
},
],
location: '/long/test',
},
},
}

const calledWithTrailingSlashesLabelUpdates = {
path: '/long/test',
context: {
breadcrumb: {
crumbs: [
{
crumbLabel: 'Home',
pathname: '/',
},
{
crumbLabel: 'LONG',
pathname: '/long/',
},
{
crumbLabel: 'test',
pathname: '/long/test/',
},
],
location: '/long/test',
},
},
}

afterEach(() => {
actions.createPage.mockClear()
actions.deletePage.mockClear()
Expand Down Expand Up @@ -202,4 +248,33 @@ describe('AutoGen crumbs: ', () => {
expect(actions.deletePage).not.toHaveBeenCalled()
expect(actions.createPage).not.toHaveBeenCalled()
})
it('should generate crumbs with trailingSlashes', () => {
onCreatePage(
{ page: mockPageLongPath, actions },
{
useAutoGen: true,
trailingSlashes: true,
},
)
expect(actions.deletePage).toHaveBeenCalledTimes(1)
expect(actions.deletePage).toHaveBeenCalledWith(mockPageLongPath)
expect(actions.createPage).toHaveBeenCalledTimes(1)
expect(actions.createPage).toHaveBeenCalledWith(calledWithTrailingSlashes)
})
it('should generate crumbs with trailingSlashes and label updates', () => {
onCreatePage(
{ page: mockPageLongPath, actions },
{
useAutoGen: true,
trailingSlashes: true,
crumbLabelUpdates: [{ pathname: '/long', crumbLabel: 'LONG' }],
},
)
expect(actions.deletePage).toHaveBeenCalledTimes(1)
expect(actions.deletePage).toHaveBeenCalledWith(mockPageLongPath)
expect(actions.createPage).toHaveBeenCalledTimes(1)
expect(actions.createPage).toHaveBeenCalledWith(
calledWithTrailingSlashesLabelUpdates,
)
})
})

0 comments on commit f6d53e2

Please sign in to comment.