Skip to content

Commit

Permalink
feat: add trailingSlashes plugin option
Browse files Browse the repository at this point in the history
  • Loading branch information
sbardian committed May 22, 2020
1 parent e4f2d1d commit 7f89eb1
Showing 1 changed file with 18 additions and 5 deletions.
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

0 comments on commit 7f89eb1

Please sign in to comment.