Skip to content

Commit

Permalink
Merge pull request #48 from x-govuk/dont-link-self-in-breadcrumb
Browse files Browse the repository at this point in the history
If the current page is in the navigation, do not link to it
  • Loading branch information
fofr authored May 25, 2022
2 parents 7144ccb + f637e6d commit 22551de
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 21 deletions.
35 changes: 22 additions & 13 deletions lib/filters/items-from-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@ module.exports = (eleventyNavigation, pageUrl = false, options = {}) => {
const currentUrl = pageUrl ? url(pageUrl, pathPrefix) : false
const items = []

eleventyNavigation.map(item => items.push({
current: pageUrl ? url(item.url, pathPrefix) === currentUrl : false,
parent: pageUrl ? pageUrl.startsWith(item.url, pathPrefix) : false,
href: url(item.url, pathPrefix),
text: item.title,
children: item.children
? item.children.map(child => ({
current: pageUrl ? url(child.url, pathPrefix) === currentUrl : false,
href: url(child.url, pathPrefix),
text: child.title
}))
: false
}))
eleventyNavigation.forEach(item => {
const isCurrentPage = pageUrl && url(item.url, pathPrefix) === currentUrl
const navItem = {
current: isCurrentPage,
parent: pageUrl ? pageUrl.startsWith(item.url, pathPrefix) : false,
text: item.title,
children: item.children
? item.children.map(child => ({
current: pageUrl ? url(child.url, pathPrefix) === currentUrl : false,
href: url(child.url, pathPrefix),
text: child.title
}))
: false
}

// If the current page is being shown in the navigation, do not link to it
if (!isCurrentPage) {
navItem.href = url(item.url, pathPrefix)
}

items.push(navItem)
})

if (options?.parentSite) {
items.unshift({
Expand Down
52 changes: 44 additions & 8 deletions tests/lib/filters/items-from-navigation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ const eleventyNavigationBreadcrumb = [{
title: 'Child page',
_isBreadcrumb: true
}]
}, {
key: 'child',
parent: 'parent',
excerpt: false,
url: '/parent/child',
pluginType: 'eleventy-navigation',
parentKey: 'parent',
title: 'Child page',
_isBreadcrumb: true,
children: false
}]

test('Converts navigation data to items array', t => {
const result = itemsFromNavigation(eleventyNavigationBreadcrumb, '/parent/')
const result = itemsFromNavigation(eleventyNavigationBreadcrumb, '/parent/child')

t.deepEqual(result, [{
href: '/',
Expand All @@ -42,13 +52,18 @@ test('Converts navigation data to items array', t => {
}, {
href: '/parent/',
text: 'Parent page',
current: true,
current: false,
parent: true,
children: [{
href: '/parent/child/',
text: 'Child page',
current: false
}]
}, {
text: 'Child page',
current: true,
parent: true,
children: false
}])
})

Expand All @@ -71,14 +86,20 @@ test('Converts navigation data to items array without page URL', t => {
text: 'Child page',
current: false
}]
}, {
href: '/parent/child',
text: 'Child page',
current: false,
parent: false,
children: false
}])
})

test('Converts navigation data to items array using path prefix', t => {
const config = {
pathPrefix: '/prefix/'
}
const result = itemsFromNavigation(eleventyNavigationBreadcrumb, '/parent/', config)
const result = itemsFromNavigation(eleventyNavigationBreadcrumb, '/parent/child', config)

t.deepEqual(result, [{
href: '/prefix/',
Expand All @@ -89,13 +110,18 @@ test('Converts navigation data to items array using path prefix', t => {
}, {
href: '/prefix/parent/',
text: 'Parent page',
current: true,
current: false,
parent: true,
children: [{
href: '/prefix/parent/child/',
text: 'Child page',
current: false
}]
}, {
text: 'Child page',
current: true,
parent: true,
children: false
}])
})

Expand All @@ -106,7 +132,7 @@ test('Converts navigation data to items array adding parent site', t => {
name: 'Example'
}
}
const result = itemsFromNavigation(eleventyNavigationBreadcrumb, '/parent/', config)
const result = itemsFromNavigation(eleventyNavigationBreadcrumb, '/parent/child', config)

t.deepEqual(result, [{
href: 'https://example.org',
Expand All @@ -120,13 +146,18 @@ test('Converts navigation data to items array adding parent site', t => {
}, {
href: '/parent/',
text: 'Parent page',
current: true,
current: false,
parent: true,
children: [{
href: '/parent/child/',
text: 'Child page',
current: false
}]
}, {
text: 'Child page',
current: true,
parent: true,
children: false
}])
})

Expand All @@ -138,7 +169,7 @@ test('Converts navigation data to items array adding parent site and using path
},
pathPrefix: '/prefix/'
}
const result = itemsFromNavigation(eleventyNavigationBreadcrumb, '/parent/', config)
const result = itemsFromNavigation(eleventyNavigationBreadcrumb, '/parent/child', config)

t.deepEqual(result, [{
href: 'https://example.org',
Expand All @@ -152,12 +183,17 @@ test('Converts navigation data to items array adding parent site and using path
}, {
href: '/prefix/parent/',
text: 'Parent page',
current: true,
current: false,
parent: true,
children: [{
href: '/prefix/parent/child/',
text: 'Child page',
current: false
}]
}, {
text: 'Child page',
current: true,
parent: true,
children: false
}])
})

0 comments on commit 22551de

Please sign in to comment.