Skip to content

Commit

Permalink
fix(sidebar): set next/prev properly on deep nested tree
Browse files Browse the repository at this point in the history
  • Loading branch information
shcheklein committed May 5, 2023
1 parent f6e9716 commit 98bde91
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
8 changes: 5 additions & 3 deletions packages/gatsby-theme-iterative/src/utils/shared/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ function normalizeSidebar({
prevRef.next = normalizedItem.path
}

prevRef = normalizedItem

if (rawItem.children) {
normalizedItem.children = normalizeSidebar({
data: rawItem.children,
Expand All @@ -164,9 +166,9 @@ function normalizeSidebar({
startingPrevRef: normalizedItem
})

prevRef = normalizedItem.children[normalizedItem.children.length - 1]
} else {
prevRef = normalizedItem
while (prevRef.children) {
prevRef = prevRef.children[prevRef.children.length - 1]
}
}

currentResult.push(normalizedItem)
Expand Down
62 changes: 51 additions & 11 deletions packages/gatsby-theme-iterative/src/utils/shared/sidebar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,37 +154,77 @@ describe('normalizeSidebar', () => {
expect(sidebarData).toEqual(result)
})

it('Adds correct prev/next links in nested list', () => {
it('Adds correct prev/next links in nested lists', () => {
const rawData = [
{ slug: 'first-item', children: ['nested-item'] },
{
slug: 'first-item',
children: [
'nested-item-first',
{
slug: 'nested-item-second',
source: 'nested-item-second/index.md',
children: ['nested-nested-item']
}
]
},
'second-item'
]

const result = [
{
label: 'First Item',
path: '/doc/first-item',
source: '/docs/first-item.md',
label: 'First Item',
tutorials: {},
prev: undefined,
next: '/doc/first-item/nested-item',
next: '/doc/first-item/nested-item-first',
style: undefined,
icon: undefined,
children: [
{
label: 'Nested Item',
path: '/doc/first-item/nested-item',
source: '/docs/first-item/nested-item.md',
path: '/doc/first-item/nested-item-first',
source: '/docs/first-item/nested-item-first.md',
label: 'Nested Item First',
tutorials: {},
prev: '/doc/first-item',
next: '/doc/second-item'
next: '/doc/first-item/nested-item-second',
style: undefined,
icon: undefined
},
{
path: '/doc/first-item/nested-item-second',
source: '/docs/first-item/nested-item-second/index.md',
label: 'Nested Item Second',
tutorials: {},
prev: '/doc/first-item/nested-item-first',
next: '/doc/first-item/nested-item-second/nested-nested-item',
style: undefined,
icon: undefined,
children: [
{
path: '/doc/first-item/nested-item-second/nested-nested-item',
source:
'/docs/first-item/nested-item-second/nested-nested-item.md',
label: 'Nested Nested Item',
tutorials: {},
prev: '/doc/first-item/nested-item-second',
next: '/doc/second-item',
style: undefined,
icon: undefined
}
]
}
]
},
{
label: 'Second Item',
path: '/doc/second-item',
source: '/docs/second-item.md',
label: 'Second Item',
tutorials: {},
prev: '/doc/first-item/nested-item',
next: undefined
prev: '/doc/first-item/nested-item-second/nested-nested-item',
next: undefined,
style: undefined,
icon: undefined
}
]

Expand Down

0 comments on commit 98bde91

Please sign in to comment.