docs: fix current sidebar not found (#11155)

This commit is contained in:
Shahed Nasser
2025-01-25 18:51:05 +02:00
committed by GitHub
parent f803157943
commit 1d7eeb53ae
@@ -107,7 +107,11 @@ export const isSidebarItemLink = (
) )
} }
const areItemsEqual = (itemA: SidebarItem, itemB: SidebarItem): boolean => { const areItemsEqual = (
itemA: SidebarItem,
itemB: SidebarItem,
compareTitles = true
): boolean => {
if ( if (
itemA.type === "separator" || itemA.type === "separator" ||
itemB.type === "separator" || itemB.type === "separator" ||
@@ -115,7 +119,7 @@ const areItemsEqual = (itemA: SidebarItem, itemB: SidebarItem): boolean => {
) { ) {
return false return false
} }
const hasSameTitle = itemA.title === itemB.title const hasSameTitle = !compareTitles || itemA.title === itemB.title
const hasSamePath = const hasSamePath =
!isSidebarItemLink(itemA) || !isSidebarItemLink(itemA) ||
!isSidebarItemLink(itemB) || !isSidebarItemLink(itemB) ||
@@ -127,17 +131,18 @@ const areItemsEqual = (itemA: SidebarItem, itemB: SidebarItem): boolean => {
const findItem = ( const findItem = (
section: SidebarItem[], section: SidebarItem[],
item: Partial<SidebarItem>, item: Partial<SidebarItem>,
checkChildren = true checkChildren = true,
compareTitles = true
): SidebarItemLinkWithParent | undefined => { ): SidebarItemLinkWithParent | undefined => {
let foundItem: SidebarItemLinkWithParent | undefined let foundItem: SidebarItemLinkWithParent | undefined
section.some((i) => { section.some((i) => {
if (i.type === "separator") { if (i.type === "separator") {
return false return false
} }
if (areItemsEqual(item as SidebarItem, i)) { if (areItemsEqual(item as SidebarItem, i, compareTitles)) {
foundItem = i as SidebarItemLink foundItem = i as SidebarItemLink
} else if (checkChildren && i.children) { } else if (checkChildren && i.children) {
foundItem = findItem(i.children, item) foundItem = findItem(i.children, item, checkChildren, compareTitles)
if (foundItem && !foundItem.parentItem) { if (foundItem && !foundItem.parentItem) {
foundItem.parentItem = i foundItem.parentItem = i
} }
@@ -428,10 +433,15 @@ export const SidebarProvider = ({
const childSidebar = const childSidebar =
getCurrentSidebar(item.children) || getCurrentSidebar(item.children) ||
(activePath (activePath
? findItem(item.children, { ? findItem(
path: activePath, item.children,
type: "link", {
}) path: activePath,
type: "link",
},
true,
false
)
: undefined) : undefined)
currentSidebar = childSidebar currentSidebar = childSidebar