mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-06 04:42:40 +02:00
fix(v2): show doc sidebar on pages with case-sensitive paths (#2235)
This commit is contained in:
parent
155539421b
commit
42061c6d83
2 changed files with 20 additions and 16 deletions
|
@ -18,29 +18,33 @@ import {matchPath} from '@docusaurus/router';
|
||||||
|
|
||||||
import styles from './styles.module.css';
|
import styles from './styles.module.css';
|
||||||
|
|
||||||
function matchingRouteExist(routes, pathname) {
|
|
||||||
return routes.some(route => matchPath(pathname, route));
|
|
||||||
}
|
|
||||||
|
|
||||||
function DocPage(props) {
|
function DocPage(props) {
|
||||||
const {route, docsMetadata, location} = props;
|
const {route: baseRoute, docsMetadata, location} = props;
|
||||||
|
// case-sensitive route such as it is defined in the sidebar
|
||||||
|
const currentRoute = baseRoute.routes.find(route =>
|
||||||
|
matchPath(location.pathname, route),
|
||||||
|
);
|
||||||
|
|
||||||
const {permalinkToSidebar, docsSidebars, version} = docsMetadata;
|
const {permalinkToSidebar, docsSidebars, version} = docsMetadata;
|
||||||
const sidebar = permalinkToSidebar[location.pathname.replace(/\/$/, '')];
|
const sidebar = permalinkToSidebar[currentRoute.path];
|
||||||
const {siteConfig: {themeConfig = {}} = {}} = useDocusaurusContext();
|
const {
|
||||||
|
siteConfig: {themeConfig = {}} = {},
|
||||||
|
isClient,
|
||||||
|
} = useDocusaurusContext();
|
||||||
const {sidebarCollapsible = true} = themeConfig;
|
const {sidebarCollapsible = true} = themeConfig;
|
||||||
|
|
||||||
if (!matchingRouteExist(route.routes, location.pathname)) {
|
if (!currentRoute) {
|
||||||
return <NotFound {...props} />;
|
return <NotFound {...props} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout version={version}>
|
<Layout version={version} key={isClient}>
|
||||||
<div className={styles.docPage}>
|
<div className={styles.docPage}>
|
||||||
{sidebar && (
|
{sidebar && (
|
||||||
<div className={styles.docSidebarContainer}>
|
<div className={styles.docSidebarContainer}>
|
||||||
<DocSidebar
|
<DocSidebar
|
||||||
docsSidebars={docsSidebars}
|
docsSidebars={docsSidebars}
|
||||||
location={location}
|
path={currentRoute.path}
|
||||||
sidebar={sidebar}
|
sidebar={sidebar}
|
||||||
sidebarCollapsible={sidebarCollapsible}
|
sidebarCollapsible={sidebarCollapsible}
|
||||||
/>
|
/>
|
||||||
|
@ -48,7 +52,7 @@ function DocPage(props) {
|
||||||
)}
|
)}
|
||||||
<main className={styles.docMainContainer}>
|
<main className={styles.docMainContainer}>
|
||||||
<MDXProvider components={MDXComponents}>
|
<MDXProvider components={MDXComponents}>
|
||||||
{renderRoutes(route.routes)}
|
{renderRoutes(baseRoute.routes)}
|
||||||
</MDXProvider>
|
</MDXProvider>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -93,13 +93,13 @@ function DocSidebarItem({item, onItemClick, collapsible}) {
|
||||||
|
|
||||||
// Calculate the category collapsing state when a page navigation occurs.
|
// Calculate the category collapsing state when a page navigation occurs.
|
||||||
// We want to automatically expand the categories which contains the current page.
|
// We want to automatically expand the categories which contains the current page.
|
||||||
function mutateSidebarCollapsingState(item, location) {
|
function mutateSidebarCollapsingState(item, path) {
|
||||||
const {items, href, type} = item;
|
const {items, href, type} = item;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'category': {
|
case 'category': {
|
||||||
const anyChildItemsActive =
|
const anyChildItemsActive =
|
||||||
items
|
items
|
||||||
.map(childItem => mutateSidebarCollapsingState(childItem, location))
|
.map(childItem => mutateSidebarCollapsingState(childItem, path))
|
||||||
.filter(val => val).length > 0;
|
.filter(val => val).length > 0;
|
||||||
// eslint-disable-next-line no-param-reassign
|
// eslint-disable-next-line no-param-reassign
|
||||||
item.collapsed = !anyChildItemsActive;
|
item.collapsed = !anyChildItemsActive;
|
||||||
|
@ -108,7 +108,7 @@ function mutateSidebarCollapsingState(item, location) {
|
||||||
|
|
||||||
case 'link':
|
case 'link':
|
||||||
default:
|
default:
|
||||||
return href === location.pathname.replace(/\/$/, '');
|
return href === path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ function DocSidebar(props) {
|
||||||
|
|
||||||
const {
|
const {
|
||||||
docsSidebars,
|
docsSidebars,
|
||||||
location,
|
path,
|
||||||
sidebar: currentSidebar,
|
sidebar: currentSidebar,
|
||||||
sidebarCollapsible,
|
sidebarCollapsible,
|
||||||
} = props;
|
} = props;
|
||||||
|
@ -142,7 +142,7 @@ function DocSidebar(props) {
|
||||||
|
|
||||||
if (sidebarCollapsible) {
|
if (sidebarCollapsible) {
|
||||||
sidebarData.forEach(sidebarItem =>
|
sidebarData.forEach(sidebarItem =>
|
||||||
mutateSidebarCollapsingState(sidebarItem, location),
|
mutateSidebarCollapsingState(sidebarItem, path),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue