Skip to content

Commit

Permalink
Gitbook 主题伪静态自动展开异常
Browse files Browse the repository at this point in the history
  • Loading branch information
tangly1024 committed Nov 14, 2024
1 parent e977092 commit b96cc66
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
18 changes: 8 additions & 10 deletions themes/gitbook/components/NavPostList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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])

// 折叠项切换,当折叠或展开数组时会调用
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions themes/gitbook/components/PageNavDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const PageNavDrawer = props => {

useEffect(() => {
changePageNavVisible(false)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [router])

return (
Expand Down

0 comments on commit b96cc66

Please sign in to comment.