Skip to content

Commit

Permalink
gitbook 侧边栏支持自动分组自动排序
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Aug 15, 2023
1 parent 09e9f88 commit cee3b27
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 14 additions & 3 deletions themes/gitbook/components/NavPostList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import NavPostListEmpty from './NavPostListEmpty'
import { useRouter } from 'next/router'
import NavPostItem from './NavPostItem'
import CONFIG from '../config'

/**
* 博客列表滚动分页
Expand All @@ -15,10 +16,20 @@ const NavPostList = (props) => {
let selectedSth = false
const groupedArray = filteredNavPages?.reduce((groups, item) => {
const categoryName = item?.category ? item?.category : '' // 将category转换为字符串
const lastGroup = groups[groups.length - 1] // 获取最后一个分组

if (!lastGroup || lastGroup?.category !== categoryName) { // 如果当前元素的category与上一个元素不同,则创建新分组
groups.push({ category: categoryName, items: [] })
// 开启自动分组排序
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] })
}
} else {
const lastGroup = groups[groups.length - 1] // 获取最后一个分组
if (!lastGroup || lastGroup?.category !== categoryName) { // 如果当前元素的category与上一个元素不同,则创建新分组
groups.push({ category: categoryName, items: [] })
}
}

groups[groups.length - 1].items.push(item) // 将元素加入对应的分组
Expand Down
2 changes: 2 additions & 0 deletions themes/gitbook/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const CONFIG = {

INDEX_PAGE: 'about', // 文档首页显示的文章,请确此路径包含在您的notion数据库中

AUTO_SORT: process.env.NEXT_PUBLIC_GITBOOK_AUTO_SORT || true, // 是否自动按分类名 归组排序文章;自动归组可能会打乱您Notion中的文章顺序

// 菜单
MENU_CATEGORY: true, // 显示分类
MENU_TAG: true, // 显示标签
Expand Down

0 comments on commit cee3b27

Please sign in to comment.