mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-06 12:52:31 +02:00
fix(theme-classic): fix breadcrumb home link bug with new useHomePageRoute() hook (#6749)
This commit is contained in:
parent
0d14470d54
commit
ece7399d2e
6 changed files with 123 additions and 4 deletions
|
@ -124,6 +124,8 @@ export {
|
|||
|
||||
export {isRegexpStringMatch} from './utils/regexpUtils';
|
||||
|
||||
export {useHomePageRoute} from './utils/routesUtils';
|
||||
|
||||
export {useColorMode, ColorModeProvider} from './utils/colorModeUtils';
|
||||
export {
|
||||
useTabGroupChoice,
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {type Route} from '@generated/routes';
|
||||
import {findHomePageRoute} from '../routesUtils';
|
||||
|
||||
describe('routesUtils findHomePageRoute', () => {
|
||||
const homePage: Route = {
|
||||
path: '/',
|
||||
exact: true,
|
||||
};
|
||||
|
||||
test('should return undefined for no routes', () => {
|
||||
expect(findHomePageRoute([])).toEqual(undefined);
|
||||
});
|
||||
|
||||
test('should return undefined for no homepage', () => {
|
||||
expect(
|
||||
findHomePageRoute([
|
||||
{path: '/a', exact: true},
|
||||
{path: '/b', exact: false},
|
||||
{path: '/c', exact: undefined},
|
||||
{
|
||||
path: '/d',
|
||||
exact: false,
|
||||
routes: [
|
||||
{path: '/d/1', exact: true},
|
||||
{path: '/d/2', exact: false},
|
||||
{path: '/d/3', exact: undefined},
|
||||
],
|
||||
},
|
||||
]),
|
||||
).toEqual(undefined);
|
||||
});
|
||||
|
||||
test('should find top-level homepage', () => {
|
||||
expect(
|
||||
findHomePageRoute([
|
||||
{path: '/a', exact: true},
|
||||
{path: '/b', exact: false},
|
||||
{path: '/c', exact: undefined},
|
||||
{...homePage, exact: false},
|
||||
homePage,
|
||||
{...homePage, exact: undefined},
|
||||
]),
|
||||
).toEqual(homePage);
|
||||
});
|
||||
|
||||
test('should find nested homepage', () => {
|
||||
expect(
|
||||
findHomePageRoute([
|
||||
{path: '/a', exact: true},
|
||||
{
|
||||
path: '/',
|
||||
exact: false,
|
||||
routes: [
|
||||
{path: '/b', exact: true},
|
||||
{
|
||||
path: '/',
|
||||
exact: false,
|
||||
routes: [{path: '/c', exact: true}, homePage],
|
||||
},
|
||||
],
|
||||
},
|
||||
{path: '/d', exact: true},
|
||||
]),
|
||||
).toEqual(homePage);
|
||||
});
|
||||
});
|
39
packages/docusaurus-theme-common/src/utils/routesUtils.ts
Normal file
39
packages/docusaurus-theme-common/src/utils/routesUtils.ts
Normal file
|
@ -0,0 +1,39 @@
|
|||
/**
|
||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import GeneratedRoutes, {type Route} from '@generated/routes';
|
||||
import {useMemo} from 'react';
|
||||
|
||||
function isHomePageRoute(route: Route): boolean {
|
||||
return route.path === '/' && route.exact === true;
|
||||
}
|
||||
|
||||
function isHomeParentRoute(route: Route): boolean {
|
||||
return route.path === '/' && route.exact === false;
|
||||
}
|
||||
|
||||
// Note that all sites don't always have a homepage in practice
|
||||
// See https://github.com/facebook/docusaurus/pull/6517#issuecomment-1048709116
|
||||
export function findHomePageRoute(
|
||||
routes: Route[] = GeneratedRoutes,
|
||||
): Route | undefined {
|
||||
if (routes.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
const homePage = routes.find(isHomePageRoute);
|
||||
if (homePage) {
|
||||
return homePage;
|
||||
}
|
||||
const indexSubRoutes = routes
|
||||
.filter(isHomeParentRoute)
|
||||
.flatMap((route) => route.routes ?? []);
|
||||
return findHomePageRoute(indexSubRoutes);
|
||||
}
|
||||
|
||||
export function useHomePageRoute(): Route | undefined {
|
||||
return useMemo(() => findHomePageRoute(), []);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue