docs: support multiple sidebars in a project (#11768)

* changed to new sidebar across projects except resources

* finalize multi sidebar support

* clean up

* remove redundant property

* small changes

* fixes

* generate

* fix error

* fix initial open
This commit is contained in:
Shahed Nasser
2025-03-07 15:47:38 +02:00
committed by GitHub
parent 2a0bd86204
commit 5deb8eaf50
108 changed files with 37634 additions and 36749 deletions
@@ -21,12 +21,16 @@ export const getLocalSearch = <T extends BaseSearchRecord = BaseSearchRecord>({
docs,
searchableFields,
options,
}: GetLocalSearchInput<T>): LocalSearch<T> => {
const miniSearch = new MiniSearch({
fields: searchableFields,
...options,
})
miniSearch.addAll(docs)
}: GetLocalSearchInput<T>): LocalSearch<T> | undefined => {
try {
const miniSearch = new MiniSearch({
fields: searchableFields,
...options,
})
miniSearch.addAll(docs)
return miniSearch as LocalSearch<T>
return miniSearch as LocalSearch<T>
} catch (e) {
console.warn(e)
}
}
+1 -1
View File
@@ -12,6 +12,6 @@ export * from "./is-elm-window"
export * from "./is-in-view"
export * from "./learning-paths"
export * from "./set-obj-value"
export * from "./sidebar-attach-href-common-options"
export * from "./sidebar-utils"
export * from "./swr-fetcher"
export * from "./workflow-diagram-utils"
@@ -1,22 +0,0 @@
import { RawSidebarItem } from "types"
const commonOptions: Partial<RawSidebarItem> = {
loaded: true,
isPathHref: true,
}
export function sidebarAttachHrefCommonOptions(
sidebar: RawSidebarItem[]
): RawSidebarItem[] {
return sidebar.map((item) => {
if (item.type === "separator") {
return item
}
return {
...commonOptions,
...item,
children: sidebarAttachHrefCommonOptions(item.children || []),
}
})
}
@@ -0,0 +1,129 @@
import { Sidebar } from "types"
export const isSidebarItemLink = (
item: Sidebar.SidebarItem | undefined,
options?: {
checkRef?: boolean
checkExternal?: boolean
}
): item is Sidebar.SidebarItemLink => {
const { checkRef = true, checkExternal = true } = options || {}
return (
item !== undefined &&
(item.type === "link" ||
(checkRef && item.type === "ref") ||
(checkExternal && item.type === "external"))
)
}
export const areSidebarItemsEqual = ({
itemA,
itemB,
compareTitles = true,
}: {
itemA: Sidebar.SidebarItem
itemB: Sidebar.SidebarItem
compareTitles?: boolean
}): boolean => {
if (itemA.type !== itemB.type) {
return false
}
// after this, we know that itemA and itemB have the same type
switch (itemA.type) {
case "separator":
return true
case "sidebar":
return (
itemA.sidebar_id === (itemB as Sidebar.SidebarItemSidebar).sidebar_id
)
case "category":
case "sub-category":
return compareTitles
? itemA.title === (itemB as Sidebar.SidebarItemCategory).title
: false
case "link":
case "ref":
case "external": {
const hasSameTitle =
!compareTitles ||
itemA.title === (itemB as Sidebar.SidebarItemLink).title
const hasSamePath = itemA.path === (itemB as Sidebar.SidebarItemLink).path
return hasSameTitle && hasSamePath
}
}
}
export const findSidebarItem = ({
sidebarItems,
item,
checkChildren = true,
compareTitles = true,
}: {
sidebarItems: Sidebar.SidebarItem[]
item: Sidebar.SidebarItem
checkChildren?: boolean
compareTitles?: boolean
}): Sidebar.SidebarItem | undefined => {
let foundItem: Sidebar.SidebarItem | undefined
sidebarItems.some((i) => {
if (areSidebarItemsEqual({ itemA: i, itemB: item })) {
foundItem = i
} else if (checkChildren && "children" in i && i.children) {
foundItem = findSidebarItem({
sidebarItems: i.children,
item,
checkChildren,
compareTitles,
})
}
return foundItem !== undefined
})
return foundItem
}
export const getSidebarItemWithHistory = ({
sidebarItems,
item,
sidebarHistory = [],
checkChildren = true,
compareTitles = true,
}: {
sidebarItems: Sidebar.SidebarItem[]
item: Sidebar.SidebarItem
sidebarHistory?: string[]
checkChildren?: boolean
compareTitles?: boolean
}): {
item: Sidebar.SidebarItem | undefined
sidebarHistory: string[]
} => {
let foundItem: Sidebar.SidebarItem | undefined
sidebarItems.some((i) => {
if (areSidebarItemsEqual({ itemA: i, itemB: item, compareTitles })) {
foundItem = i
} else if (checkChildren && "children" in i && i.children) {
const result = getSidebarItemWithHistory({
sidebarItems: i.children,
item,
checkChildren,
compareTitles,
})
if (result.item) {
foundItem = result.item
if (i.type === "sidebar") {
sidebarHistory.push(i.sidebar_id)
}
sidebarHistory.push(...result.sidebarHistory)
}
}
return foundItem !== undefined
})
return { item: foundItem, sidebarHistory }
}