Skip to content

Commit 5deaf6a

Browse files
authored
fix: sidebar 'auto' not working (#178) (#224)
BREAKING CHANGE: If sidebat is `undefined`, it will be treated as `auto` where previsouly it's treated as `false`. It was always treated as `auto`, but due to this bug, the sidebar was hidden, therefore it looked like it was treated as `false`.
1 parent 338e845 commit 5deaf6a

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

__tests__/client/theme-default/support/sideBar.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
import {
2+
isSideBarEmpty,
23
getSideBarConfig,
34
getFlatSideBarLinks
45
} from 'client/theme-default/support/sideBar'
56

67
describe('client/theme-default/support/sideBar', () => {
8+
it('checks if the given sidebar is empty', () => {
9+
expect(isSideBarEmpty(undefined)).toBe(true)
10+
expect(isSideBarEmpty(false)).toBe(true)
11+
expect(isSideBarEmpty([])).toBe(true)
12+
13+
expect(isSideBarEmpty('auto')).toBe(false)
14+
expect(isSideBarEmpty([{ text: 'a', link: '/a' }])).toBe(false)
15+
})
16+
717
it('gets the correct sidebar items', () => {
818
expect(getSideBarConfig(false, '')).toEqual(false)
919
expect(getSideBarConfig('auto', '')).toEqual('auto')

src/client/theme-default/Layout.vue

+8-7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import {
7474
usePageData,
7575
useSiteDataByRoute
7676
} from 'vitepress'
77+
import { isSideBarEmpty, getSideBarConfig } from './support/sideBar'
7778
import type { DefaultTheme } from './config'
7879
7980
// components
@@ -127,14 +128,14 @@ const openSideBar = ref(false)
127128
128129
const showSidebar = computed(() => {
129130
const { frontmatter } = route.data
131+
132+
if (frontmatter.home || frontmatter.sidebar === false) {
133+
return false
134+
}
135+
130136
const { themeConfig } = siteRouteData.value
131-
return (
132-
!frontmatter.home &&
133-
frontmatter.sidebar !== false &&
134-
((typeof themeConfig.sidebar === 'object' &&
135-
Object.keys(themeConfig.sidebar).length != 0) ||
136-
(Array.isArray(themeConfig.sidebar) && themeConfig.sidebar.length != 0))
137-
)
137+
138+
return !isSideBarEmpty(getSideBarConfig(themeConfig.sidebar, route.path))
138139
})
139140
140141
const toggleSidebar = (to?: boolean) => {

src/client/theme-default/support/sideBar.ts

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export function isSideBarGroup(
1313
return (item as DefaultTheme.SideBarGroup).children !== undefined
1414
}
1515

16+
export function isSideBarEmpty(sidebar?: DefaultTheme.SideBarConfig): boolean {
17+
return isArray(sidebar) ? sidebar.length === 0 : !sidebar
18+
}
19+
1620
/**
1721
* Get the `SideBarConfig` from sidebar option. This method will ensure to get
1822
* correct sidebar config from `MultiSideBarConfig` with various path

0 commit comments

Comments
 (0)