Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sidebar 'auto' not working (#178) #224

Merged
merged 2 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions __tests__/client/theme-default/support/sideBar.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import {
isSideBarEmpty,
getSideBarConfig,
getFlatSideBarLinks
} from 'client/theme-default/support/sideBar'

describe('client/theme-default/support/sideBar', () => {
it('checks if the given sidebar is empty', () => {
expect(isSideBarEmpty(undefined)).toBe(true)
expect(isSideBarEmpty(false)).toBe(true)
expect(isSideBarEmpty([])).toBe(true)

expect(isSideBarEmpty('auto')).toBe(false)
expect(isSideBarEmpty([{ text: 'a', link: '/a' }])).toBe(false)
})

it('gets the correct sidebar items', () => {
expect(getSideBarConfig(false, '')).toEqual(false)
expect(getSideBarConfig('auto', '')).toEqual('auto')
Expand Down
15 changes: 8 additions & 7 deletions src/client/theme-default/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
usePageData,
useSiteDataByRoute
} from 'vitepress'
import { isSideBarEmpty, getSideBarConfig } from './support/sideBar'
import type { DefaultTheme } from './config'

// components
Expand Down Expand Up @@ -127,14 +128,14 @@ const openSideBar = ref(false)

const showSidebar = computed(() => {
const { frontmatter } = route.data

if (frontmatter.home || frontmatter.sidebar === false) {
return false
}

const { themeConfig } = siteRouteData.value
return (
!frontmatter.home &&
frontmatter.sidebar !== false &&
((typeof themeConfig.sidebar === 'object' &&
Object.keys(themeConfig.sidebar).length != 0) ||
(Array.isArray(themeConfig.sidebar) && themeConfig.sidebar.length != 0))
)

return !isSideBarEmpty(getSideBarConfig(themeConfig.sidebar, route.path))
})

const toggleSidebar = (to?: boolean) => {
Expand Down
4 changes: 4 additions & 0 deletions src/client/theme-default/support/sideBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export function isSideBarGroup(
return (item as DefaultTheme.SideBarGroup).children !== undefined
}

export function isSideBarEmpty(sidebar?: DefaultTheme.SideBarConfig): boolean {
return isArray(sidebar) ? sidebar.length === 0 : !sidebar
}

/**
* Get the `SideBarConfig` from sidebar option. This method will ensure to get
* correct sidebar config from `MultiSideBarConfig` with various path
Expand Down