Skip to content

Commit

Permalink
fix: sidebar data undefined in production
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyuan0704 committed Sep 24, 2022
1 parent db5b88e commit 86f63ca
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/.island/config.ts
Expand Up @@ -29,14 +29,14 @@ export default defineConfig({
},
{
text: 'API',
link: '/api',
link: '/api/',
activeMatch: '^/api'
}
],

sidebar: {
'/': getTutorialSidebar(),
'/api': getApiSidebar()
'/api/': getApiSidebar()
},

footer: {
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/configure-site.md
Expand Up @@ -125,5 +125,5 @@ type SidebarItem = {
The `object` config is a map for `SidebarGroup`, which has following type:

```ts
Record<string, SidebarGroup>;
Record<string, SidebarGroup[]>;
```
13 changes: 11 additions & 2 deletions src/node/build.ts
Expand Up @@ -293,8 +293,17 @@ class SSGBuilder {
}
</body>
</html>`.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);
}
Expand Down
1 change: 0 additions & 1 deletion src/theme-default/components/Siderbar/index.tsx
Expand Up @@ -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`;
Expand Down
3 changes: 2 additions & 1 deletion 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';

Expand Down Expand Up @@ -59,7 +60,7 @@ export function APILayout() {
styles[`level${header.depth}`]
}`}
>
<Link href={`${item.link}#${header.id}`}>
<Link href={`${normalizeHref(item.link)}#${header.id}`}>
{header.text}
</Link>
</li>
Expand Down
9 changes: 8 additions & 1 deletion src/theme-default/logic/index.ts
Expand Up @@ -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}`);
}

Expand Down
8 changes: 6 additions & 2 deletions src/theme-default/logic/useSidebarData.ts
Expand Up @@ -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];
Expand Down

0 comments on commit 86f63ca

Please sign in to comment.