Skip to content

Commit 8e5a75f

Browse files
feat: allow activeBaseRegex for DocNavbarItem
1 parent a0ab2b0 commit 8e5a75f

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

packages/docusaurus-theme-classic/src/theme/NavbarItem/DocNavbarItem.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@
77

88
import React from 'react';
99
import {useActiveDocContext} from '@docusaurus/plugin-content-docs/client';
10-
import {useLayoutDoc} from '@docusaurus/theme-common/internal';
10+
import useBaseUrl from '@docusaurus/useBaseUrl';
11+
import {
12+
isRegexpStringMatch,
13+
useLayoutDoc,
14+
} from '@docusaurus/theme-common/internal';
1115
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
1216
import type {Props} from '@theme/NavbarItem/DocNavbarItem';
1317

1418
export default function DocNavbarItem({
1519
docId,
1620
label: staticLabel,
1721
docsPluginId,
22+
activeBasePath,
23+
activeBaseRegex,
1824
...props
1925
}: Props): JSX.Element | null {
2026
const {activeDoc} = useActiveDocContext(docsPluginId);
2127
const doc = useLayoutDoc(docId, docsPluginId);
28+
const activeBaseUrl = useBaseUrl(activeBasePath);
2229

2330
// Draft items are not displayed in the navbar.
2431
if (doc === null) {
@@ -29,10 +36,25 @@ export default function DocNavbarItem({
2936
<DefaultNavbarItem
3037
exact
3138
{...props}
32-
isActive={() =>
33-
activeDoc?.path === doc.path ||
34-
(!!activeDoc?.sidebar && activeDoc.sidebar === doc.sidebar)
35-
}
39+
isActive={(_match, location) => {
40+
if (activeBaseRegex) {
41+
console.log('using activeBaseRegex', activeBaseRegex);
42+
console.log('location', location.pathname);
43+
const matched = isRegexpStringMatch(
44+
activeBaseRegex,
45+
location.pathname,
46+
);
47+
console.log('matched', matched);
48+
return matched;
49+
}
50+
if (activeBasePath) {
51+
return location.pathname.startsWith(activeBaseUrl);
52+
}
53+
return (
54+
activeDoc?.path === doc.path ||
55+
(!!activeDoc?.sidebar && activeDoc.sidebar === doc.sidebar)
56+
);
57+
}}
3658
label={staticLabel ?? doc.id}
3759
to={doc.path}
3860
/>

website/docusaurus.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,13 @@ const config = {
430430
docId: 'introduction',
431431
label: 'Docs',
432432
},
433+
{
434+
type: 'doc',
435+
position: 'left',
436+
docId: 'playground',
437+
label: 'Playground',
438+
activeBaseRegex: `/docs/playground`,
439+
},
433440
{
434441
type: 'docSidebar',
435442
position: 'left',

0 commit comments

Comments
 (0)