From 86f63ca0e8bd5c0c9caf2cc2e03d2d6a325995d7 Mon Sep 17 00:00:00 2001 From: sanyuan <494130947@qq.com> Date: Fri, 23 Sep 2022 21:57:38 +0800 Subject: [PATCH] fix: sidebar data undefined in production --- docs/.island/config.ts | 4 ++-- docs/guide/configure-site.md | 2 +- src/node/build.ts | 13 +++++++++++-- src/theme-default/components/Siderbar/index.tsx | 1 - src/theme-default/layout/APILayout/index.tsx | 3 ++- src/theme-default/logic/index.ts | 9 ++++++++- src/theme-default/logic/useSidebarData.ts | 8 ++++++-- 7 files changed, 30 insertions(+), 10 deletions(-) diff --git a/docs/.island/config.ts b/docs/.island/config.ts index 066e4aae..f3d2cd19 100644 --- a/docs/.island/config.ts +++ b/docs/.island/config.ts @@ -29,14 +29,14 @@ export default defineConfig({ }, { text: 'API', - link: '/api', + link: '/api/', activeMatch: '^/api' } ], sidebar: { '/': getTutorialSidebar(), - '/api': getApiSidebar() + '/api/': getApiSidebar() }, footer: { diff --git a/docs/guide/configure-site.md b/docs/guide/configure-site.md index 27e897a2..48d75672 100644 --- a/docs/guide/configure-site.md +++ b/docs/guide/configure-site.md @@ -125,5 +125,5 @@ type SidebarItem = { The `object` config is a map for `SidebarGroup`, which has following type: ```ts -Record; +Record; ``` diff --git a/src/node/build.ts b/src/node/build.ts index 5bb6819d..1424ddb3 100644 --- a/src/node/build.ts +++ b/src/node/build.ts @@ -293,8 +293,17 @@ class SSGBuilder { } `.trim(); - const fileName = - routePath === '/' ? 'index.html' : `${routePath.slice(1)}.html`; + + const normalizeHtmlFilePath = (path: string) => { + if (path === '/') { + return 'index.html'; + } + if (path.endsWith('/')) { + return `${path}index.html`; + } + return `${path}.html`; + }; + const fileName = normalizeHtmlFilePath(routePath); await fs.ensureDir(join(this.#root, DIST_PATH, dirname(fileName))); await fs.writeFile(join(this.#root, DIST_PATH, fileName), html); } diff --git a/src/theme-default/components/Siderbar/index.tsx b/src/theme-default/components/Siderbar/index.tsx index 2d5f6764..4b1167ec 100644 --- a/src/theme-default/components/Siderbar/index.tsx +++ b/src/theme-default/components/Siderbar/index.tsx @@ -12,7 +12,6 @@ export function SideBar() { const sidebar = siteData?.themeConfig?.sidebar || []; const sidebarData = useSidebarData(sidebar, location.pathname); - console.log(sidebarData); const renderGroupItem = (item: DefaultTheme.SidebarItem, depth = 0) => { const marginLeft = `${depth * 20}px`; diff --git a/src/theme-default/layout/APILayout/index.tsx b/src/theme-default/layout/APILayout/index.tsx index 331ecc5d..5f8fbbac 100644 --- a/src/theme-default/layout/APILayout/index.tsx +++ b/src/theme-default/layout/APILayout/index.tsx @@ -1,6 +1,7 @@ import { usePageData } from 'island/client'; import { useEffect, useState } from 'react'; import { Header } from 'shared/types'; +import { normalizeHref } from '../../logic'; import { Link } from '../../components/Link/index'; import styles from './index.module.scss'; @@ -59,7 +60,7 @@ export function APILayout() { styles[`level${header.depth}`] }`} > - + {header.text} diff --git a/src/theme-default/logic/index.ts b/src/theme-default/logic/index.ts index 13a2b401..b0e89c76 100644 --- a/src/theme-default/logic/index.ts +++ b/src/theme-default/logic/index.ts @@ -11,7 +11,14 @@ export function normalizeHref(url?: string) { if (!isProduction() || url.startsWith('http')) { return url; } - const suffix = import.meta.env.ENABLE_SPA ? '' : '.html'; + + let suffix = ''; + if (!import.meta.env.ENABLE_SPA) { + suffix += '.html'; + if (url.endsWith('/')) { + suffix = 'index' + suffix; + } + } return addLeadingSlash(`${url}${suffix}`); } diff --git a/src/theme-default/logic/useSidebarData.ts b/src/theme-default/logic/useSidebarData.ts index ac64ba01..90f5478a 100644 --- a/src/theme-default/logic/useSidebarData.ts +++ b/src/theme-default/logic/useSidebarData.ts @@ -6,12 +6,16 @@ export function useSidebarData( ): DefaultTheme.SidebarGroup | DefaultTheme.SidebarGroup[] | undefined { if (Array.isArray(siderbar)) { return siderbar.find((group) => - group.items.some((item) => normalizeHref(item.link) === currentPathname) + group.items.some( + (item) => normalizeHref(item.link) === normalizeHref(currentPathname) + ) ); } else { for (const name of Object.keys(siderbar)) { const result = siderbar[name].find((group) => - group.items.some((item) => normalizeHref(item.link) === currentPathname) + group.items.some( + (item) => normalizeHref(item.link) === normalizeHref(currentPathname) + ) ); if (result) { return siderbar[name];