Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sidebar): set next/prev properly on deep nested tree #201

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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