77
88import React from 'react' ;
99import { 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' ;
1115import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem' ;
1216import type { Props } from '@theme/NavbarItem/DocNavbarItem' ;
1317
1418export 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 />
0 commit comments