Make changes to code to improve readability
This commit is contained in:
committed by
Benedikt Rötsch
parent
a110d848c0
commit
cdb0a07ff7
@@ -3,24 +3,25 @@ const url = require('url')
|
||||
module.exports = (modifier) => {
|
||||
return (request, response, next) => {
|
||||
const baseUrl = url.format({ protocol: request.protocol, host: request.get('host') })
|
||||
const parts = url.parse(request.url).pathname.split('/').filter(Boolean)
|
||||
let items = []
|
||||
const urlComponents = url.parse(request.url).pathname.split('/').filter(Boolean)
|
||||
let breadcrumbs = []
|
||||
|
||||
items.push({ label: 'Home', url: baseUrl })
|
||||
items = items.concat(parts.map((part, i, array) => {
|
||||
breadcrumbs.push({ label: 'Home', url: baseUrl })
|
||||
// Map components of the path to breadcrumbs with resolvable URLs
|
||||
let mappedComponents = urlComponents.map((component, i, array) => {
|
||||
const path = array.slice(0, i + 1).join('/')
|
||||
return {
|
||||
label: part.replace(/-/g, ' '),
|
||||
label: component.replace(/-/g, ' '),
|
||||
url: url.resolve(baseUrl, path),
|
||||
path: path
|
||||
}
|
||||
})
|
||||
)
|
||||
breadcrumbs = breadcrumbs.concat(mappedComponents)
|
||||
if (modifier) {
|
||||
items = items.map(modifier)
|
||||
breadcrumbs = breadcrumbs.map(modifier)
|
||||
}
|
||||
// Make it global
|
||||
request.app.locals.breadcrumb = items
|
||||
request.app.locals.breadcrumb = breadcrumbs
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user