Skip to content

Commit

Permalink
Core & UI: Support trailing slash
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Jan 11, 2025
1 parent 1e6ece0 commit 041f230
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 59 deletions.
6 changes: 6 additions & 0 deletions .changeset/green-items-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'fumadocs-core': patch
'fumadocs-ui': patch
---

Support trailing slash
3 changes: 0 additions & 3 deletions apps/docs/next.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ const config: NextConfig = {
ignoreDuringBuilds: true,
},
serverExternalPackages: ['ts-morph', 'typescript', 'oxc-transform'],
experimental: {
reactCompiler: true,
},
images: {
unoptimized: true,
remotePatterns: [
Expand Down
1 change: 0 additions & 1 deletion apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@radix-ui/react-tooltip": "^1.1.6",
"@shikijs/rehype": "^1.26.1",
"@theguild/remark-mermaid": "^0.2.0",
"babel-plugin-react-compiler": "19.0.0-beta-df7b47d-20241124",
"class-variance-authority": "^0.7.1",
"fumadocs-core": "workspace:*",
"fumadocs-docgen": "workspace:^",
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ export function getBreadcrumbItemsFromPath(
export function searchPath(
nodes: PageTree.Node[],
url: string,
): (PageTree.Folder | PageTree.Item | PageTree.Separator)[] | null {
): PageTree.Node[] | null {
if (url.endsWith('/')) url = url.slice(0, -1);

let separator: PageTree.Separator | undefined;

for (const node of nodes) {
Expand Down
6 changes: 4 additions & 2 deletions packages/ui/src/components/layout/root-toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface Option {
/**
* Detect from a list of urls
*/
urls?: string[];
urls?: Set<string>;

props?: HTMLAttributes<HTMLElement>;
}
Expand All @@ -41,7 +41,9 @@ export function RootToggle({
const selected = useMemo(() => {
return options.findLast((item) =>
item.urls
? item.urls.includes(pathname)
? item.urls.has(
pathname.endsWith('/') ? pathname.slice(0, -1) : pathname,
)
: isActive(item.url, pathname, true),
);
}, [options, pathname]);
Expand Down
16 changes: 9 additions & 7 deletions packages/ui/src/layouts/docs/shared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function getSidebarTabs(
icon: node.icon,
description: node.description,

urls: getFolderUrls(node),
urls: getFolderUrls(node, new Set()),
};

const mapped = transform ? transform(option, node) : option;
Expand All @@ -175,14 +175,16 @@ function getSidebarTabs(
return findOptions(pageTree as PageTree.Folder);
}

function getFolderUrls(folder: PageTree.Folder): string[] {
const results: string[] = [];
if (folder.index) results.push(folder.index.url);
function getFolderUrls(
folder: PageTree.Folder,
output: Set<string>,
): Set<string> {
if (folder.index) output.add(folder.index.url);

for (const child of folder.children) {
if (child.type === 'page') results.push(child.url);
if (child.type === 'folder') results.push(...getFolderUrls(child));
if (child.type === 'page') output.add(child.url);
if (child.type === 'folder') getFolderUrls(child, output);
}

return results;
return output;
}
3 changes: 2 additions & 1 deletion packages/ui/src/page.client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getBreadcrumbItemsFromPath,
} from 'fumadocs-core/breadcrumb';
import { usePageStyles } from '@/contexts/layout';
import { isActive } from '@/utils/is-active';

export function TocNav(props: HTMLAttributes<HTMLDivElement>) {
const { open } = useSidebar();
Expand Down Expand Up @@ -152,7 +153,7 @@ export function Footer({ items }: FooterProps) {
const list = cached ?? scanNavigationList(root.children);
listCache.set(root, list);

const idx = list.findIndex((item) => item.url === pathname);
const idx = list.findIndex((item) => isActive(item.url, pathname, false));

if (idx === -1) return {};
return {
Expand Down
3 changes: 3 additions & 0 deletions packages/ui/src/utils/is-active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ export function isActive(
pathname: string,
nested = true,
): boolean {
if (url.endsWith('/')) url = url.slice(0, -1);
if (pathname.endsWith('/')) pathname = pathname.slice(0, -1);

return url === pathname || (nested && pathname.startsWith(`${url}/`));
}
63 changes: 19 additions & 44 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 041f230

Please sign in to comment.