docs: generate sidebar for main docs (#11139)
This commit is contained in:
1 parent
8038a917ee
commit
f803157943
6 files changed
+1588
-604
No files matched your search
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* @param {import("@/types").SidebarItem[]} sidebarItems - The items to add numbers to their titles
|
||||
* @param {number[]} numbering - The current numbering level
|
||||
* @returns {import("@/types").SidebarItem[]} The modified sidebar items
|
||||
*/
|
||||
export default function numberSidebarItems(sidebarItems, numbering = [1]) {
|
||||
if (!numbering.length) {
|
||||
numbering.push(1)
|
||||
}
|
||||
const isTopItems = numbering.length === 1
|
||||
/** @type {import("@/types").SidebarItem[]} */
|
||||
const numberedItems = []
|
||||
/** @type {import("@/types").SidebarItem | undefined} */
|
||||
let parentItem
|
||||
sidebarItems.forEach((item, index) => {
|
||||
if (item.type === "separator") {
|
||||
;(parentItem?.children || numberedItems).push(item)
|
||||
return
|
||||
}
|
||||
|
||||
// append current number to the item's title
|
||||
item.number = `${numbering.join(".")}.`
|
||||
item.chapterTitle = `${item.number} ${
|
||||
item.chapterTitle?.trim() || item.title?.trim()
|
||||
}`
|
||||
item.title = item.title.trim()
|
||||
|
||||
if (isTopItems) {
|
||||
// Add chapter category
|
||||
numberedItems.push(
|
||||
item.type === "category"
|
||||
? item
|
||||
: {
|
||||
type: "category",
|
||||
title: item.chapterTitle,
|
||||
children: [],
|
||||
loaded: true,
|
||||
initialOpen: false,
|
||||
}
|
||||
)
|
||||
|
||||
parentItem = numberedItems[numberedItems.length - 1]
|
||||
}
|
||||
|
||||
if (item.children) {
|
||||
item.children = numberSidebarItems(item.children, [...numbering, 1])
|
||||
}
|
||||
|
||||
if (item.type !== "category" || !isTopItems) {
|
||||
;(parentItem?.children || numberedItems).push(item)
|
||||
}
|
||||
|
||||
numbering[numbering.length - 1]++
|
||||
})
|
||||
|
||||
return numberedItems
|
||||
}
|
||||
Reference in new issue
Block a user