@@ -17,10 +17,28 @@ import {useLocation} from '@docusaurus/router';
17
17
import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem' ;
18
18
import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem' ;
19
19
import type { Props } from '@theme/NavbarItem/DocsVersionDropdownNavbarItem' ;
20
- import type { GlobalVersion } from '@docusaurus/plugin-content-docs/client' ;
20
+ import type { LinkLikeNavbarItemProps } from '@theme/NavbarItem' ;
21
+ import type {
22
+ GlobalVersion ,
23
+ GlobalDoc ,
24
+ ActiveDocContext ,
25
+ } from '@docusaurus/plugin-content-docs/client' ;
21
26
22
- const getVersionMainDoc = ( version : GlobalVersion ) =>
23
- version . docs . find ( ( doc ) => doc . id === version . mainDocId ) ! ;
27
+ function getVersionMainDoc ( version : GlobalVersion ) : GlobalDoc {
28
+ return version . docs . find ( ( doc ) => doc . id === version . mainDocId ) ! ;
29
+ }
30
+
31
+ function getVersionTargetDoc (
32
+ version : GlobalVersion ,
33
+ activeDocContext : ActiveDocContext ,
34
+ ) : GlobalDoc {
35
+ // We try to link to the same doc, in another version
36
+ // When not possible, fallback to the "main doc" of the version
37
+ return (
38
+ activeDocContext . alternateDocVersions [ version . name ] ??
39
+ getVersionMainDoc ( version )
40
+ ) ;
41
+ }
24
42
25
43
export default function DocsVersionDropdownNavbarItem ( {
26
44
mobile,
@@ -34,23 +52,21 @@ export default function DocsVersionDropdownNavbarItem({
34
52
const activeDocContext = useActiveDocContext ( docsPluginId ) ;
35
53
const versions = useVersions ( docsPluginId ) ;
36
54
const { savePreferredVersionName} = useDocsPreferredVersion ( docsPluginId ) ;
37
- const versionLinks = versions . map ( ( version ) => {
38
- // We try to link to the same doc, in another version
39
- // When not possible, fallback to the "main doc" of the version
40
- const versionDoc =
41
- activeDocContext . alternateDocVersions [ version . name ] ??
42
- getVersionMainDoc ( version ) ;
55
+
56
+ function versionToLink ( version : GlobalVersion ) : LinkLikeNavbarItemProps {
57
+ const targetDoc = getVersionTargetDoc ( version , activeDocContext ) ;
43
58
return {
44
59
label : version . label ,
45
60
// preserve ?search#hash suffix on version switches
46
- to : `${ versionDoc . path } ${ search } ${ hash } ` ,
61
+ to : `${ targetDoc . path } ${ search } ${ hash } ` ,
47
62
isActive : ( ) => version === activeDocContext . activeVersion ,
48
63
onClick : ( ) => savePreferredVersionName ( version . name ) ,
49
64
} ;
50
- } ) ;
51
- const items = [
65
+ }
66
+
67
+ const items : LinkLikeNavbarItemProps [ ] = [
52
68
...dropdownItemsBefore ,
53
- ...versionLinks ,
69
+ ...versions . map ( versionToLink ) ,
54
70
...dropdownItemsAfter ,
55
71
] ;
56
72
@@ -69,7 +85,7 @@ export default function DocsVersionDropdownNavbarItem({
69
85
const dropdownTo =
70
86
mobile && items . length > 1
71
87
? undefined
72
- : getVersionMainDoc ( dropdownVersion ) . path ;
88
+ : getVersionTargetDoc ( dropdownVersion , activeDocContext ) . path ;
73
89
74
90
// We don't want to render a version dropdown with 0 or 1 item. If we build
75
91
// the site with a single docs version (onlyIncludeVersions: ['1.0.0']),
0 commit comments