gitbook 侧边栏支持自动分组自动排序

This commit is contained in:
tangly1024.com
2023-08-15 12:10:03 +08:00
parent 09e9f88317
commit cee3b27d7b
2 changed files with 16 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
import NavPostListEmpty from './NavPostListEmpty'
import { useRouter } from 'next/router'
import NavPostItem from './NavPostItem'
import CONFIG from '../config'
/**
* 博客列表滚动分页
@@ -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) // 将元素加入对应的分组

View File

@@ -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, // 显示标签