From b96cc6640bcdaf099b3537b52b2e278ef3de66fc Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Thu, 14 Nov 2024 15:14:24 +0800 Subject: [PATCH] =?UTF-8?q?Gitbook=20=E4=B8=BB=E9=A2=98=E4=BC=AA=E9=9D=99?= =?UTF-8?q?=E6=80=81=E8=87=AA=E5=8A=A8=E5=B1=95=E5=BC=80=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/gitbook/components/NavPostList.js | 18 ++++++++---------- themes/gitbook/components/PageNavDrawer.js | 1 + 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/themes/gitbook/components/NavPostList.js b/themes/gitbook/components/NavPostList.js index c7c4adf9..bf905526 100644 --- a/themes/gitbook/components/NavPostList.js +++ b/themes/gitbook/components/NavPostList.js @@ -14,7 +14,7 @@ import NavPostItem from './NavPostItem' * @constructor */ const NavPostList = props => { - const { filteredNavPages, post } = props + const { filteredNavPages } = props const { locale, currentSearch } = useGlobal() const router = useRouter() @@ -31,16 +31,17 @@ const NavPostList = props => { CONFIG ) - // 展开文件夹 useEffect(() => { + // 展开文件夹 setTimeout(() => { - // 默认展开一个 + const currentPath = decodeURIComponent(router.asPath.split('?')[0]) const defaultOpenIndex = getDefaultOpenIndexByPath( categoryFolders, - decodeURIComponent(router.asPath.split('?')[0]) + currentPath ) setExpandedGroups([defaultOpenIndex]) }, 500) + // eslint-disable-next-line react-hooks/exhaustive-deps }, [router, filteredNavPages]) // 折叠项切换,当折叠或展开数组时会调用 @@ -149,19 +150,16 @@ function groupArticles(filteredNavPages) { * @returns {number} 返回需要展开的菜单索引 */ function getDefaultOpenIndexByPath(categoryFolders, path) { - // 默认展开第一个索引 - let defaultIndex = 0 - // 查找满足条件的第一个索引 const index = categoryFolders.findIndex(group => { - return group.items.some(post => path === '/' + post.slug) + return group.items.some(post => path === post.href) }) // 如果找到满足条件的索引,则设置为该索引 if (index !== -1) { - defaultIndex = index + return index } - return defaultIndex + return 0 } export default NavPostList diff --git a/themes/gitbook/components/PageNavDrawer.js b/themes/gitbook/components/PageNavDrawer.js index df50be6c..c1bf835d 100644 --- a/themes/gitbook/components/PageNavDrawer.js +++ b/themes/gitbook/components/PageNavDrawer.js @@ -22,6 +22,7 @@ const PageNavDrawer = props => { useEffect(() => { changePageNavVisible(false) + // eslint-disable-next-line react-hooks/exhaustive-deps }, [router]) return (