From 98bde91ec90bc706237495897d46c03a14a160e9 Mon Sep 17 00:00:00 2001 From: Ivan Shcheklein Date: Thu, 4 May 2023 19:17:12 -0700 Subject: [PATCH] fix(sidebar): set next/prev properly on deep nested tree --- .../src/utils/shared/sidebar.js | 8 ++- .../src/utils/shared/sidebar.test.js | 62 +++++++++++++++---- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/packages/gatsby-theme-iterative/src/utils/shared/sidebar.js b/packages/gatsby-theme-iterative/src/utils/shared/sidebar.js index ca45c545..cbe1bc8c 100644 --- a/packages/gatsby-theme-iterative/src/utils/shared/sidebar.js +++ b/packages/gatsby-theme-iterative/src/utils/shared/sidebar.js @@ -156,6 +156,8 @@ function normalizeSidebar({ prevRef.next = normalizedItem.path } + prevRef = normalizedItem + if (rawItem.children) { normalizedItem.children = normalizeSidebar({ data: rawItem.children, @@ -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) diff --git a/packages/gatsby-theme-iterative/src/utils/shared/sidebar.test.js b/packages/gatsby-theme-iterative/src/utils/shared/sidebar.test.js index 06db04d2..3c6c3a61 100644 --- a/packages/gatsby-theme-iterative/src/utils/shared/sidebar.test.js +++ b/packages/gatsby-theme-iterative/src/utils/shared/sidebar.test.js @@ -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 } ]