diff --git a/themes/gitbook/components/NavPostList.js b/themes/gitbook/components/NavPostList.js index 48222a46..ceef2105 100644 --- a/themes/gitbook/components/NavPostList.js +++ b/themes/gitbook/components/NavPostList.js @@ -17,19 +17,19 @@ const NavPostList = (props) => { const groupedArray = filteredNavPages?.reduce((groups, item) => { const categoryName = item?.category ? item?.category : '' // 将category转换为字符串 + let existingGroup = null // 开启自动分组排序 if (JSON.parse(CONFIG.AUTO_SORT)) { - const existingGroup = groups.find(group => group.category === categoryName) - if (existingGroup) { - existingGroup.items.push(item) - } else { - groups.push({ category: categoryName, items: [item] }) - } + existingGroup = groups.find(group => group.category === categoryName) // 搜索同名的最后一个分组 } else { - const lastGroup = groups[groups.length - 1] // 获取最后一个分组 - if (!lastGroup || lastGroup?.category !== categoryName) { // 如果当前元素的category与上一个元素不同,则创建新分组 - groups.push({ category: categoryName, items: [] }) - } + existingGroup = groups[groups.length - 1] // 获取最后一个分组 + } + + // 添加数据 + if (existingGroup && existingGroup.category === categoryName) { + existingGroup.items.push(item) + } else { + groups.push({ category: categoryName, items: [item] }) } return groups }, [])