Skip to content

Commit

Permalink
No longer need to use url helper in itemsFromNavigation filter
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Dec 13, 2023
1 parent fa10a14 commit 03d8192
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 78 deletions.
15 changes: 6 additions & 9 deletions lib/filters/items-from-navigation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const url = require('@11ty/eleventy/src/Filters/Url.js')
const smart = require('./smart.js')

/**
Expand All @@ -10,29 +9,27 @@ const smart = require('./smart.js')
* @returns {Array} `items` array
*/
module.exports = (eleventyNavigation, pageUrl = false, options = {}) => {
const pathPrefix = options?.pathPrefix || '/'
const currentUrl = pageUrl ? url(pageUrl, pathPrefix) : false
const items = []

eleventyNavigation.forEach(item => {
const isCurrentPage = pageUrl && url(item.url, pathPrefix) === currentUrl
const isCurrentPage = pageUrl && item.url === pageUrl
const navItem = {
current: isCurrentPage,
parent: pageUrl ? pageUrl.startsWith(item.url, pathPrefix) : false,
href: url(item.url, pathPrefix),
parent: pageUrl && pageUrl.startsWith(item.url),
href: item.url,
text: smart(item.title),
children: item.children
? item.children.map(child => ({
current: pageUrl ? url(child.url, pathPrefix) === currentUrl : false,
href: url(child.url, pathPrefix),
current: pageUrl && child.url === pageUrl,
href: child.url,
text: smart(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)
navItem.href = item.url
}

items.push(navItem)
Expand Down
69 changes: 0 additions & 69 deletions test/lib/filters/items-from-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,37 +98,6 @@ describe('itemsFromNavigation filter', () => {
}])
})

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

assert.deepEqual(result, [{
href: '/prefix/',
text: 'Home',
current: false,
parent: true,
children: false
}, {
href: '/prefix/parent/',
text: 'Parent page',
current: false,
parent: true,
children: [{
href: '/prefix/parent/child/',
text: 'Child page',
current: false
}]
}, {
href: '/prefix/parent/child',
text: 'Child page',
current: true,
parent: true,
children: false
}])
})

it('Converts navigation data to items array adding parent site', () => {
const config = {
parentSite: {
Expand Down Expand Up @@ -165,42 +134,4 @@ describe('itemsFromNavigation filter', () => {
children: false
}])
})

it('Converts navigation data to items array adding parent site and using path prefix', () => {
const config = {
parentSite: {
url: 'https://example.org',
name: 'Example'
},
pathPrefix: '/prefix/'
}
const result = itemsFromNavigation(eleventyNavigationBreadcrumb, '/parent/child', config)

assert.deepEqual(result, [{
href: 'https://example.org',
text: 'Example'
}, {
href: '/prefix/',
text: 'Home',
current: false,
parent: true,
children: false
}, {
href: '/prefix/parent/',
text: 'Parent page',
current: false,
parent: true,
children: [{
href: '/prefix/parent/child/',
text: 'Child page',
current: false
}]
}, {
href: '/prefix/parent/child',
text: 'Child page',
current: true,
parent: true,
children: false
}])
})
})

0 comments on commit 03d8192

Please sign in to comment.