From e2c761777cfb5787209bb71eb485dee4678ce26d Mon Sep 17 00:00:00 2001 From: Kia King Ishii Date: Sat, 6 Feb 2021 00:49:51 +0900 Subject: [PATCH 1/2] fix: sidebar 'auto' not working --- .../client/theme-default/support/sideBar.spec.ts | 10 ++++++++++ src/client/theme-default/Layout.vue | 15 ++++++++------- src/client/theme-default/support/sideBar.ts | 4 ++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/__tests__/client/theme-default/support/sideBar.spec.ts b/__tests__/client/theme-default/support/sideBar.spec.ts index 033daced9983..604c945d229a 100644 --- a/__tests__/client/theme-default/support/sideBar.spec.ts +++ b/__tests__/client/theme-default/support/sideBar.spec.ts @@ -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') diff --git a/src/client/theme-default/Layout.vue b/src/client/theme-default/Layout.vue index ed68e60b8560..17579d38d546 100644 --- a/src/client/theme-default/Layout.vue +++ b/src/client/theme-default/Layout.vue @@ -74,6 +74,7 @@ import { usePageData, useSiteDataByRoute } from 'vitepress' +import { isSideBarEmpty, getSideBarConfig } from './support/sideBar' import type { DefaultTheme } from './config' // components @@ -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)) }) const toggleSidebar = (to?: boolean) => { diff --git a/src/client/theme-default/support/sideBar.ts b/src/client/theme-default/support/sideBar.ts index 7af837215a90..63ccce1732bb 100644 --- a/src/client/theme-default/support/sideBar.ts +++ b/src/client/theme-default/support/sideBar.ts @@ -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 From cada4e75bb5cd600e755de5a40d1ebbc06e741f2 Mon Sep 17 00:00:00 2001 From: Kia King Ishii Date: Sat, 6 Feb 2021 01:11:24 +0900 Subject: [PATCH 2/2] fix: add missing argument --- src/client/theme-default/Layout.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/theme-default/Layout.vue b/src/client/theme-default/Layout.vue index 17579d38d546..43a0b89fa778 100644 --- a/src/client/theme-default/Layout.vue +++ b/src/client/theme-default/Layout.vue @@ -135,7 +135,7 @@ const showSidebar = computed(() => { const { themeConfig } = siteRouteData.value - return !isSideBarEmpty(getSideBarConfig(themeConfig.sidebar)) + return !isSideBarEmpty(getSideBarConfig(themeConfig.sidebar, route.path)) }) const toggleSidebar = (to?: boolean) => {