@docusaurus/theme-classic 3.7.0-canary-6206 → 3.7.0-canary-6209

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/options.js CHANGED
@@ -182,6 +182,11 @@ const DocsVersionDropdownNavbarItemSchema = NavbarItemBaseSchema.append({
182
182
  dropdownActiveClassDisabled: utils_validation_1.Joi.boolean(),
183
183
  dropdownItemsBefore: utils_validation_1.Joi.array().items(DropdownSubitemSchema).default([]),
184
184
  dropdownItemsAfter: utils_validation_1.Joi.array().items(DropdownSubitemSchema).default([]),
185
+ versions: utils_validation_1.Joi.alternatives().try(utils_validation_1.Joi.array().items(utils_validation_1.Joi.string().min(1)).min(1), utils_validation_1.Joi.object()
186
+ .pattern(utils_validation_1.Joi.string().min(1), utils_validation_1.Joi.object({
187
+ label: utils_validation_1.Joi.string().min(1),
188
+ }))
189
+ .min(1)),
185
190
  });
186
191
  const LocaleDropdownNavbarItemSchema = NavbarItemBaseSchema.append({
187
192
  type: utils_validation_1.Joi.string().equal('localeDropdown').required(),
@@ -6,4 +6,4 @@
6
6
  */
7
7
  import { type ReactNode } from 'react';
8
8
  import type { Props } from '@theme/NavbarItem/DocsVersionDropdownNavbarItem';
9
- export default function DocsVersionDropdownNavbarItem({ mobile, docsPluginId, dropdownActiveClassDisabled, dropdownItemsBefore, dropdownItemsAfter, ...props }: Props): ReactNode;
9
+ export default function DocsVersionDropdownNavbarItem({ mobile, docsPluginId, dropdownActiveClassDisabled, dropdownItemsBefore, dropdownItemsAfter, versions: configs, ...props }: Props): ReactNode;
@@ -15,6 +15,35 @@ import {translate} from '@docusaurus/Translate';
15
15
  import {useLocation} from '@docusaurus/router';
16
16
  import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
17
17
  import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem';
18
+ function getVersionItems(versions, configs) {
19
+ if (configs) {
20
+ // Collect all the versions we have
21
+ const versionMap = new Map(
22
+ versions.map((version) => [version.name, version]),
23
+ );
24
+ const toVersionItem = (name, config) => {
25
+ const version = versionMap.get(name);
26
+ if (!version) {
27
+ throw new Error(`No docs version exist for name '${name}', please verify your 'docsVersionDropdown' navbar item versions config.
28
+ Available version names:\n- ${versions.map((v) => `${v.name}`).join('\n- ')}`);
29
+ }
30
+ return {version, label: config?.label ?? version.label};
31
+ };
32
+ if (Array.isArray(configs)) {
33
+ return configs.map((name) => toVersionItem(name, undefined));
34
+ } else {
35
+ return Object.entries(configs).map(([name, config]) =>
36
+ toVersionItem(name, config),
37
+ );
38
+ }
39
+ } else {
40
+ return versions.map((version) => ({version, label: version.label}));
41
+ }
42
+ }
43
+ function useVersionItems({docsPluginId, configs}) {
44
+ const versions = useVersions(docsPluginId);
45
+ return getVersionItems(versions, configs);
46
+ }
18
47
  function getVersionMainDoc(version) {
19
48
  return version.docs.find((doc) => doc.id === version.mainDocId);
20
49
  }
@@ -26,22 +55,36 @@ function getVersionTargetDoc(version, activeDocContext) {
26
55
  getVersionMainDoc(version)
27
56
  );
28
57
  }
58
+ // The version item to use for the "dropdown button"
59
+ function useDisplayedVersionItem({docsPluginId, versionItems}) {
60
+ // The order of the candidates matters!
61
+ const candidates = useDocsVersionCandidates(docsPluginId);
62
+ const candidateItems = candidates
63
+ .map((candidate) => versionItems.find((vi) => vi.version === candidate))
64
+ .filter((vi) => vi !== undefined);
65
+ return candidateItems[0] ?? versionItems[0];
66
+ }
29
67
  export default function DocsVersionDropdownNavbarItem({
30
68
  mobile,
31
69
  docsPluginId,
32
70
  dropdownActiveClassDisabled,
33
71
  dropdownItemsBefore,
34
72
  dropdownItemsAfter,
73
+ versions: configs,
35
74
  ...props
36
75
  }) {
37
76
  const {search, hash} = useLocation();
38
77
  const activeDocContext = useActiveDocContext(docsPluginId);
39
- const versions = useVersions(docsPluginId);
40
78
  const {savePreferredVersionName} = useDocsPreferredVersion(docsPluginId);
41
- function versionToLink(version) {
79
+ const versionItems = useVersionItems({docsPluginId, configs});
80
+ const displayedVersionItem = useDisplayedVersionItem({
81
+ docsPluginId,
82
+ versionItems,
83
+ });
84
+ function versionItemToLink({version, label}) {
42
85
  const targetDoc = getVersionTargetDoc(version, activeDocContext);
43
86
  return {
44
- label: version.label,
87
+ label,
45
88
  // preserve ?search#hash suffix on version switches
46
89
  to: `${targetDoc.path}${search}${hash}`,
47
90
  isActive: () => version === activeDocContext.activeVersion,
@@ -50,10 +93,9 @@ export default function DocsVersionDropdownNavbarItem({
50
93
  }
51
94
  const items = [
52
95
  ...dropdownItemsBefore,
53
- ...versions.map(versionToLink),
96
+ ...versionItems.map(versionItemToLink),
54
97
  ...dropdownItemsAfter,
55
98
  ];
56
- const dropdownVersion = useDocsVersionCandidates(docsPluginId)[0];
57
99
  // Mobile dropdown is handled a bit differently
58
100
  const dropdownLabel =
59
101
  mobile && items.length > 1
@@ -63,11 +105,12 @@ export default function DocsVersionDropdownNavbarItem({
63
105
  description:
64
106
  'The label for the navbar versions dropdown on mobile view',
65
107
  })
66
- : dropdownVersion.label;
108
+ : displayedVersionItem.label;
67
109
  const dropdownTo =
68
110
  mobile && items.length > 1
69
111
  ? undefined
70
- : getVersionTargetDoc(dropdownVersion, activeDocContext).path;
112
+ : getVersionTargetDoc(displayedVersionItem.version, activeDocContext)
113
+ .path;
71
114
  // We don't want to render a version dropdown with 0 or 1 item. If we build
72
115
  // the site with a single docs version (onlyIncludeVersions: ['1.0.0']),
73
116
  // We'd rather render a button instead of a dropdown
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/theme-classic",
3
- "version": "3.7.0-canary-6206",
3
+ "version": "3.7.0-canary-6209",
4
4
  "description": "Classic theme for Docusaurus",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/theme-classic.d.ts",
@@ -20,19 +20,19 @@
20
20
  "copy:watch": "node ../../admin/scripts/copyUntypedFiles.js --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@docusaurus/core": "3.7.0-canary-6206",
24
- "@docusaurus/logger": "3.7.0-canary-6206",
25
- "@docusaurus/mdx-loader": "3.7.0-canary-6206",
26
- "@docusaurus/module-type-aliases": "3.7.0-canary-6206",
27
- "@docusaurus/plugin-content-blog": "3.7.0-canary-6206",
28
- "@docusaurus/plugin-content-docs": "3.7.0-canary-6206",
29
- "@docusaurus/plugin-content-pages": "3.7.0-canary-6206",
30
- "@docusaurus/theme-common": "3.7.0-canary-6206",
31
- "@docusaurus/theme-translations": "3.7.0-canary-6206",
32
- "@docusaurus/types": "3.7.0-canary-6206",
33
- "@docusaurus/utils": "3.7.0-canary-6206",
34
- "@docusaurus/utils-common": "3.7.0-canary-6206",
35
- "@docusaurus/utils-validation": "3.7.0-canary-6206",
23
+ "@docusaurus/core": "3.7.0-canary-6209",
24
+ "@docusaurus/logger": "3.7.0-canary-6209",
25
+ "@docusaurus/mdx-loader": "3.7.0-canary-6209",
26
+ "@docusaurus/module-type-aliases": "3.7.0-canary-6209",
27
+ "@docusaurus/plugin-content-blog": "3.7.0-canary-6209",
28
+ "@docusaurus/plugin-content-docs": "3.7.0-canary-6209",
29
+ "@docusaurus/plugin-content-pages": "3.7.0-canary-6209",
30
+ "@docusaurus/theme-common": "3.7.0-canary-6209",
31
+ "@docusaurus/theme-translations": "3.7.0-canary-6209",
32
+ "@docusaurus/types": "3.7.0-canary-6209",
33
+ "@docusaurus/utils": "3.7.0-canary-6209",
34
+ "@docusaurus/utils-common": "3.7.0-canary-6209",
35
+ "@docusaurus/utils-validation": "3.7.0-canary-6209",
36
36
  "@mdx-js/react": "^3.0.0",
37
37
  "clsx": "^2.0.0",
38
38
  "copy-text-to-clipboard": "^3.2.0",
@@ -62,5 +62,5 @@
62
62
  "engines": {
63
63
  "node": ">=18.0"
64
64
  },
65
- "gitHead": "cac332ad32e91b5f3a11bd34a5a17e55d12db267"
65
+ "gitHead": "402fd1ff0512991b1f7793c11d0ec2013b077b9b"
66
66
  }
package/src/options.ts CHANGED
@@ -7,6 +7,10 @@
7
7
 
8
8
  import {themes} from 'prism-react-renderer';
9
9
  import {Joi, URISchema} from '@docusaurus/utils-validation';
10
+ import type {
11
+ PropVersionItem,
12
+ PropVersionItems,
13
+ } from '@theme/NavbarItem/DocsVersionDropdownNavbarItem';
10
14
  import type {Options, PluginOptions} from '@docusaurus/theme-classic';
11
15
  import type {ThemeConfig} from '@docusaurus/theme-common';
12
16
  import type {
@@ -210,6 +214,17 @@ const DocsVersionDropdownNavbarItemSchema = NavbarItemBaseSchema.append({
210
214
  dropdownActiveClassDisabled: Joi.boolean(),
211
215
  dropdownItemsBefore: Joi.array().items(DropdownSubitemSchema).default([]),
212
216
  dropdownItemsAfter: Joi.array().items(DropdownSubitemSchema).default([]),
217
+ versions: Joi.alternatives().try(
218
+ Joi.array().items(Joi.string().min(1)).min(1),
219
+ Joi.object<PropVersionItems>()
220
+ .pattern(
221
+ Joi.string().min(1),
222
+ Joi.object<PropVersionItem>({
223
+ label: Joi.string().min(1),
224
+ }),
225
+ )
226
+ .min(1),
227
+ ),
213
228
  });
214
229
 
215
230
  const LocaleDropdownNavbarItemSchema = NavbarItemBaseSchema.append({
@@ -16,7 +16,11 @@ import {translate} from '@docusaurus/Translate';
16
16
  import {useLocation} from '@docusaurus/router';
17
17
  import DefaultNavbarItem from '@theme/NavbarItem/DefaultNavbarItem';
18
18
  import DropdownNavbarItem from '@theme/NavbarItem/DropdownNavbarItem';
19
- import type {Props} from '@theme/NavbarItem/DocsVersionDropdownNavbarItem';
19
+ import type {
20
+ Props,
21
+ PropVersions,
22
+ PropVersionItem,
23
+ } from '@theme/NavbarItem/DocsVersionDropdownNavbarItem';
20
24
  import type {LinkLikeNavbarItemProps} from '@theme/NavbarItem';
21
25
  import type {
22
26
  GlobalVersion,
@@ -24,6 +28,56 @@ import type {
24
28
  ActiveDocContext,
25
29
  } from '@docusaurus/plugin-content-docs/client';
26
30
 
31
+ type VersionItem = {
32
+ version: GlobalVersion;
33
+ label: string;
34
+ };
35
+
36
+ function getVersionItems(
37
+ versions: GlobalVersion[],
38
+ configs?: PropVersions,
39
+ ): VersionItem[] {
40
+ if (configs) {
41
+ // Collect all the versions we have
42
+ const versionMap = new Map<string, GlobalVersion>(
43
+ versions.map((version) => [version.name, version]),
44
+ );
45
+
46
+ const toVersionItem = (
47
+ name: string,
48
+ config?: PropVersionItem,
49
+ ): VersionItem => {
50
+ const version = versionMap.get(name);
51
+ if (!version) {
52
+ throw new Error(`No docs version exist for name '${name}', please verify your 'docsVersionDropdown' navbar item versions config.
53
+ Available version names:\n- ${versions.map((v) => `${v.name}`).join('\n- ')}`);
54
+ }
55
+ return {version, label: config?.label ?? version.label};
56
+ };
57
+
58
+ if (Array.isArray(configs)) {
59
+ return configs.map((name) => toVersionItem(name, undefined));
60
+ } else {
61
+ return Object.entries(configs).map(([name, config]) =>
62
+ toVersionItem(name, config),
63
+ );
64
+ }
65
+ } else {
66
+ return versions.map((version) => ({version, label: version.label}));
67
+ }
68
+ }
69
+
70
+ function useVersionItems({
71
+ docsPluginId,
72
+ configs,
73
+ }: {
74
+ docsPluginId: Props['docsPluginId'];
75
+ configs: Props['versions'];
76
+ }): VersionItem[] {
77
+ const versions = useVersions(docsPluginId);
78
+ return getVersionItems(versions, configs);
79
+ }
80
+
27
81
  function getVersionMainDoc(version: GlobalVersion): GlobalDoc {
28
82
  return version.docs.find((doc) => doc.id === version.mainDocId)!;
29
83
  }
@@ -40,23 +94,47 @@ function getVersionTargetDoc(
40
94
  );
41
95
  }
42
96
 
97
+ // The version item to use for the "dropdown button"
98
+ function useDisplayedVersionItem({
99
+ docsPluginId,
100
+ versionItems,
101
+ }: {
102
+ docsPluginId: Props['docsPluginId'];
103
+ versionItems: VersionItem[];
104
+ }): VersionItem {
105
+ // The order of the candidates matters!
106
+ const candidates = useDocsVersionCandidates(docsPluginId);
107
+ const candidateItems = candidates
108
+ .map((candidate) => versionItems.find((vi) => vi.version === candidate))
109
+ .filter((vi) => vi !== undefined);
110
+ return candidateItems[0] ?? versionItems[0]!;
111
+ }
112
+
43
113
  export default function DocsVersionDropdownNavbarItem({
44
114
  mobile,
45
115
  docsPluginId,
46
116
  dropdownActiveClassDisabled,
47
117
  dropdownItemsBefore,
48
118
  dropdownItemsAfter,
119
+ versions: configs,
49
120
  ...props
50
121
  }: Props): ReactNode {
51
122
  const {search, hash} = useLocation();
52
123
  const activeDocContext = useActiveDocContext(docsPluginId);
53
- const versions = useVersions(docsPluginId);
54
124
  const {savePreferredVersionName} = useDocsPreferredVersion(docsPluginId);
125
+ const versionItems = useVersionItems({docsPluginId, configs});
126
+ const displayedVersionItem = useDisplayedVersionItem({
127
+ docsPluginId,
128
+ versionItems,
129
+ });
55
130
 
56
- function versionToLink(version: GlobalVersion): LinkLikeNavbarItemProps {
131
+ function versionItemToLink({
132
+ version,
133
+ label,
134
+ }: VersionItem): LinkLikeNavbarItemProps {
57
135
  const targetDoc = getVersionTargetDoc(version, activeDocContext);
58
136
  return {
59
- label: version.label,
137
+ label,
60
138
  // preserve ?search#hash suffix on version switches
61
139
  to: `${targetDoc.path}${search}${hash}`,
62
140
  isActive: () => version === activeDocContext.activeVersion,
@@ -66,12 +144,10 @@ export default function DocsVersionDropdownNavbarItem({
66
144
 
67
145
  const items: LinkLikeNavbarItemProps[] = [
68
146
  ...dropdownItemsBefore,
69
- ...versions.map(versionToLink),
147
+ ...versionItems.map(versionItemToLink),
70
148
  ...dropdownItemsAfter,
71
149
  ];
72
150
 
73
- const dropdownVersion = useDocsVersionCandidates(docsPluginId)[0];
74
-
75
151
  // Mobile dropdown is handled a bit differently
76
152
  const dropdownLabel =
77
153
  mobile && items.length > 1
@@ -81,11 +157,13 @@ export default function DocsVersionDropdownNavbarItem({
81
157
  description:
82
158
  'The label for the navbar versions dropdown on mobile view',
83
159
  })
84
- : dropdownVersion.label;
160
+ : displayedVersionItem.label;
161
+
85
162
  const dropdownTo =
86
163
  mobile && items.length > 1
87
164
  ? undefined
88
- : getVersionTargetDoc(dropdownVersion, activeDocContext).path;
165
+ : getVersionTargetDoc(displayedVersionItem.version, activeDocContext)
166
+ .path;
89
167
 
90
168
  // We don't want to render a version dropdown with 0 or 1 item. If we build
91
169
  // the site with a single docs version (onlyIncludeVersions: ['1.0.0']),
@@ -1257,11 +1257,22 @@ declare module '@theme/NavbarItem/DocsVersionDropdownNavbarItem' {
1257
1257
  import type {Props as DropdownNavbarItemProps} from '@theme/NavbarItem/DropdownNavbarItem';
1258
1258
  import type {LinkLikeNavbarItemProps} from '@theme/NavbarItem';
1259
1259
 
1260
+ type PropVersionItem = {
1261
+ readonly label?: string;
1262
+ };
1263
+
1264
+ type PropVersionItems = {
1265
+ readonly [version: string]: PropVersionItem;
1266
+ };
1267
+
1268
+ type PropVersions = string[] | PropVersionItems;
1269
+
1260
1270
  export interface Props extends DropdownNavbarItemProps {
1261
1271
  readonly docsPluginId?: string;
1262
1272
  readonly dropdownActiveClassDisabled?: boolean;
1263
1273
  readonly dropdownItemsBefore: LinkLikeNavbarItemProps[];
1264
1274
  readonly dropdownItemsAfter: LinkLikeNavbarItemProps[];
1275
+ readonly versions?: PropVersions;
1265
1276
  }
1266
1277
 
1267
1278
  export default function DocsVersionDropdownNavbarItem(