Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(v2): show doc sidebar on pages with case-sensitive paths #2235

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions packages/docusaurus-theme-classic/src/theme/DocPage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,41 @@ import {matchPath} from '@docusaurus/router';

import styles from './styles.module.css';

function matchingRouteExist(routes, pathname) {
return routes.some(route => matchPath(pathname, route));
}

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 sidebar = permalinkToSidebar[location.pathname.replace(/\/$/, '')];
const {siteConfig: {themeConfig = {}} = {}} = useDocusaurusContext();
const sidebar = permalinkToSidebar[currentRoute.path];
const {
siteConfig: {themeConfig = {}} = {},
isClient,
} = useDocusaurusContext();
const {sidebarCollapsible = true} = themeConfig;

if (!matchingRouteExist(route.routes, location.pathname)) {
if (!currentRoute) {
return <NotFound {...props} />;
}

return (
<Layout version={version}>
<Layout version={version} key={isClient}>
<div className={styles.docPage}>
{sidebar && (
<div className={styles.docSidebarContainer}>
<DocSidebar
docsSidebars={docsSidebars}
location={location}
path={currentRoute.path}
sidebar={sidebar}
sidebarCollapsible={sidebarCollapsible}
/>
</div>
)}
<main className={styles.docMainContainer}>
<MDXProvider components={MDXComponents}>
{renderRoutes(route.routes)}
{renderRoutes(baseRoute.routes)}
</MDXProvider>
</main>
</div>
Expand Down
10 changes: 5 additions & 5 deletions packages/docusaurus-theme-classic/src/theme/DocSidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ function DocSidebarItem({item, onItemClick, collapsible}) {

// Calculate the category collapsing state when a page navigation occurs.
// 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;
switch (type) {
case 'category': {
const anyChildItemsActive =
items
.map(childItem => mutateSidebarCollapsingState(childItem, location))
.map(childItem => mutateSidebarCollapsingState(childItem, path))
.filter(val => val).length > 0;
// eslint-disable-next-line no-param-reassign
item.collapsed = !anyChildItemsActive;
Expand All @@ -108,7 +108,7 @@ function mutateSidebarCollapsingState(item, location) {

case 'link':
default:
return href === location.pathname.replace(/\/$/, '');
return href === path;
}
}

Expand All @@ -121,7 +121,7 @@ function DocSidebar(props) {

const {
docsSidebars,
location,
path,
lex111 marked this conversation as resolved.
Show resolved Hide resolved
sidebar: currentSidebar,
sidebarCollapsible,
} = props;
Expand All @@ -142,7 +142,7 @@ function DocSidebar(props) {

if (sidebarCollapsible) {
sidebarData.forEach(sidebarItem =>
mutateSidebarCollapsingState(sidebarItem, location),
mutateSidebarCollapsingState(sidebarItem, path),
);
}

Expand Down