@viteplus/versions 2.3.0 → 3.0.0
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/README.md +44 -9
- package/components/fallback-component.spec.ts +137 -0
- package/components/fallback.component.ts +221 -0
- package/components/version-switcher.component.ts +116 -424
- package/components/version-switcher.component.vue +3 -8
- package/dist/index.d.ts +126 -11
- package/dist/index.js +2 -2
- package/dist/index.js.map +5 -5
- package/package.json +13 -8
|
@@ -15,15 +15,16 @@ import { useData, useRouter } from 'vitepress';
|
|
|
15
15
|
* The versioning state that the plugin hands to the theme.
|
|
16
16
|
*
|
|
17
17
|
* @remarks
|
|
18
|
-
* The plugin builds this object at configuration time and
|
|
19
|
-
*
|
|
18
|
+
* The plugin builds this object at configuration time and publishes it once as `themeConfig.versioning`,
|
|
19
|
+
* which VitePress merges into the theme of every locale, so the switcher reads it through `useData().theme`.
|
|
20
20
|
*
|
|
21
21
|
* @example
|
|
22
22
|
* ```ts
|
|
23
23
|
* const versioningPlugin: VersioningPluginInterface = {
|
|
24
24
|
* routes: { 'v1.0.x': [ 'v1.0.x/guide/index.md' ] },
|
|
25
|
-
*
|
|
26
|
-
*
|
|
25
|
+
* labels: { 'v1.0.x': 'v1.0.x (LTS)' },
|
|
26
|
+
* versions: [ 'next', 'v2.0.x', 'v1.0.x' ],
|
|
27
|
+
* latestVersion: 'v2.0.x'
|
|
27
28
|
* };
|
|
28
29
|
* ```
|
|
29
30
|
*
|
|
@@ -32,12 +33,11 @@ import { useData, useRouter } from 'vitepress';
|
|
|
32
33
|
|
|
33
34
|
export interface VersioningPluginInterface {
|
|
34
35
|
/**
|
|
35
|
-
* The routes that every version publishes, keyed by version
|
|
36
|
+
* The routes that every version publishes, keyed by version name.
|
|
36
37
|
*
|
|
37
38
|
* @remarks
|
|
38
|
-
* A route is written exactly as VitePress serves it,
|
|
39
|
-
*
|
|
40
|
-
* An older plugin ships no such record, and the switcher then trusts every path it builds.
|
|
39
|
+
* A route is written exactly as VitePress serves it, with its version prefix and the `.md` extension.
|
|
40
|
+
* Without this record, the switcher trusts every path it builds.
|
|
41
41
|
*
|
|
42
42
|
* @example
|
|
43
43
|
* ```ts
|
|
@@ -51,40 +51,67 @@ export interface VersioningPluginInterface {
|
|
|
51
51
|
routes?: Record<string, Array<string>>;
|
|
52
52
|
|
|
53
53
|
/**
|
|
54
|
-
* The
|
|
54
|
+
* The text to show for each version, keyed by version name.
|
|
55
55
|
*
|
|
56
56
|
* @remarks
|
|
57
|
-
*
|
|
58
|
-
*
|
|
57
|
+
* A version missing from the record shows its name.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const { labels } = versioningPlugin;
|
|
62
|
+
* labels; // { 'v1.0.x': 'v1.0.x (LTS)' }
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @since 3.0.0
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
labels?: Record<string, string>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The names of every published version, in menu order.
|
|
59
72
|
*
|
|
60
73
|
* @example
|
|
61
74
|
* ```ts
|
|
62
75
|
* const { versions } = versioningPlugin;
|
|
63
|
-
* versions; //
|
|
76
|
+
* versions; // [ 'next', 'v2.0.x', 'v1.0.x' ]
|
|
64
77
|
* ```
|
|
65
78
|
*
|
|
66
79
|
* @since 2.1.0
|
|
67
80
|
*/
|
|
68
81
|
|
|
69
|
-
versions:
|
|
82
|
+
versions: Array<string>;
|
|
70
83
|
|
|
71
84
|
/**
|
|
72
|
-
* The
|
|
85
|
+
* The name of the stable version.
|
|
73
86
|
*
|
|
74
87
|
* @remarks
|
|
75
|
-
*
|
|
76
|
-
*
|
|
88
|
+
* The switcher tags it with {@link PropsInterface.latestLabel},
|
|
89
|
+
* and a page with no version segment, such as the home page at the root, belongs to it.
|
|
77
90
|
*
|
|
78
91
|
* @example
|
|
79
92
|
* ```ts
|
|
80
|
-
* const {
|
|
81
|
-
*
|
|
93
|
+
* const { latestVersion } = versioningPlugin;
|
|
94
|
+
* latestVersion; // 'v2.0.x'
|
|
82
95
|
* ```
|
|
83
96
|
*
|
|
84
|
-
* @since
|
|
97
|
+
* @since 3.0.0
|
|
85
98
|
*/
|
|
86
99
|
|
|
87
|
-
|
|
100
|
+
latestVersion: string;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* The URL prefixes of every locale other than the root one.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* const { locales } = versioningPlugin;
|
|
108
|
+
* locales; // [ 'de' ]
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @since 3.0.0
|
|
112
|
+
*/
|
|
113
|
+
|
|
114
|
+
locales?: Array<string>;
|
|
88
115
|
}
|
|
89
116
|
|
|
90
117
|
/**
|
|
@@ -103,19 +130,22 @@ export interface VersioningPluginInterface {
|
|
|
103
130
|
|
|
104
131
|
export interface PropsInterface {
|
|
105
132
|
/**
|
|
106
|
-
*
|
|
133
|
+
* Versioning state that replaces the `themeConfig.versioning` the plugin publishes.
|
|
134
|
+
*
|
|
135
|
+
* @remarks
|
|
136
|
+
* Leave it out to read the state of the site, which is what every `VersionSwitcher` nav item does.
|
|
107
137
|
*
|
|
108
138
|
* @example
|
|
109
139
|
* ```ts
|
|
110
140
|
* const { versioningPlugin } = props;
|
|
111
|
-
* versioningPlugin; // { versions:
|
|
141
|
+
* versioningPlugin; // { versions: [ 'v2.0.x', 'v1.0.x' ], latestVersion: 'v2.0.x' }
|
|
112
142
|
* ```
|
|
113
143
|
*
|
|
114
144
|
* @see VersioningPluginInterface
|
|
115
145
|
* @since 2.1.0
|
|
116
146
|
*/
|
|
117
147
|
|
|
118
|
-
versioningPlugin
|
|
148
|
+
versioningPlugin?: VersioningPluginInterface;
|
|
119
149
|
|
|
120
150
|
/**
|
|
121
151
|
* Whether the switcher renders as the mobile dropdown.
|
|
@@ -136,21 +166,17 @@ export interface PropsInterface {
|
|
|
136
166
|
screenMenu?: boolean;
|
|
137
167
|
|
|
138
168
|
/**
|
|
139
|
-
* The tag that marks the
|
|
169
|
+
* The tag that marks the stable version as the latest one.
|
|
140
170
|
*
|
|
141
171
|
* @remarks
|
|
142
|
-
* The switcher appends the tag in parentheses to the
|
|
143
|
-
* so the reader can tell the latest version apart from the archived ones.
|
|
172
|
+
* The switcher appends the tag in parentheses to the label of the stable version.
|
|
144
173
|
* When omitted, the tag reads `latest`, and an empty string turns it off.
|
|
145
|
-
* A
|
|
146
|
-
*
|
|
147
|
-
* The plugin passes the `props` of the nav item through,
|
|
148
|
-
* so the tag is set where the component is placed in the navigation.
|
|
174
|
+
* A label that already equals the tag is left as it is.
|
|
149
175
|
*
|
|
150
176
|
* @example
|
|
151
177
|
* ```ts
|
|
152
178
|
* const nav = [{ component: 'VersionSwitcher', props: { latestLabel: 'neueste' } }];
|
|
153
|
-
* // the
|
|
179
|
+
* // the stable version reads 'v2.0.x (neueste)'
|
|
154
180
|
* ```
|
|
155
181
|
*
|
|
156
182
|
* @see https://github.com/viteplus/versions/issues/44
|
|
@@ -167,9 +193,6 @@ export interface PropsInterface {
|
|
|
167
193
|
* and the mobile dropdown shows it as the text of its button.
|
|
168
194
|
* When omitted, it reads `Switch Version`.
|
|
169
195
|
*
|
|
170
|
-
* The plugin passes the `props` of the nav item through,
|
|
171
|
-
* so the label is set where the component is placed in the navigation.
|
|
172
|
-
*
|
|
173
196
|
* @example
|
|
174
197
|
* ```ts
|
|
175
198
|
* const nav = [{ component: 'VersionSwitcher', props: { label: 'Version wechseln' } }];
|
|
@@ -187,8 +210,7 @@ export interface PropsInterface {
|
|
|
187
210
|
* The shape of a single entry of the version menu.
|
|
188
211
|
*
|
|
189
212
|
* @remarks
|
|
190
|
-
* `VPMenuLink` of the default theme consumes this shape as it is
|
|
191
|
-
* so an entry needs no further mapping before it renders.
|
|
213
|
+
* `VPMenuLink` of the default theme consumes this shape as it is.
|
|
192
214
|
*
|
|
193
215
|
* @example
|
|
194
216
|
* ```ts
|
|
@@ -202,14 +224,10 @@ export interface VersionMenuItemInterface {
|
|
|
202
224
|
/**
|
|
203
225
|
* The label that the entry shows.
|
|
204
226
|
*
|
|
205
|
-
* @remarks
|
|
206
|
-
* The version label is used verbatim, so the menu reads the same as the archive folders.
|
|
207
|
-
* The current version alone carries the tag of {@link PropsInterface.latestLabel}.
|
|
208
|
-
*
|
|
209
227
|
* @example
|
|
210
228
|
* ```ts
|
|
211
229
|
* const { text } = item;
|
|
212
|
-
* text; // 'v1.0.x'
|
|
230
|
+
* text; // 'v1.0.x (LTS)'
|
|
213
231
|
* ```
|
|
214
232
|
*
|
|
215
233
|
* @since 2.1.0
|
|
@@ -235,10 +253,6 @@ export interface VersionMenuItemInterface {
|
|
|
235
253
|
/**
|
|
236
254
|
* The bindings that {@link useVersionSwitcher} exposes to the template.
|
|
237
255
|
*
|
|
238
|
-
* @remarks
|
|
239
|
-
* Only what the template renders is exposed.
|
|
240
|
-
* Every path helper stays inside the composable, since each one reads the props and the route.
|
|
241
|
-
*
|
|
242
256
|
* @example
|
|
243
257
|
* ```ts
|
|
244
258
|
* const { hasVersions, toggle } = useVersionSwitcher(props);
|
|
@@ -251,9 +265,6 @@ export interface VersionSwitcherInterface {
|
|
|
251
265
|
/**
|
|
252
266
|
* Whether the mobile dropdown is open.
|
|
253
267
|
*
|
|
254
|
-
* @remarks
|
|
255
|
-
* The desktop flyout keeps its own open state, so this ref drives the mobile layout alone.
|
|
256
|
-
*
|
|
257
268
|
* @example
|
|
258
269
|
* ```ts
|
|
259
270
|
* isOpen.value; // false while the dropdown is collapsed
|
|
@@ -265,11 +276,23 @@ export interface VersionSwitcherInterface {
|
|
|
265
276
|
isOpen: Ref<boolean>;
|
|
266
277
|
|
|
267
278
|
/**
|
|
268
|
-
*
|
|
279
|
+
* The names of every published version, in menu order.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* ```ts
|
|
283
|
+
* versions.value; // [ 'next', 'v2.0.x', 'v1.0.x' ]
|
|
284
|
+
* ```
|
|
285
|
+
*
|
|
286
|
+
* @since 3.0.0
|
|
287
|
+
*/
|
|
288
|
+
|
|
289
|
+
versions: ComputedRef<Array<string>>;
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Whether the site publishes more than one version.
|
|
269
293
|
*
|
|
270
294
|
* @remarks
|
|
271
|
-
* The component renders nothing
|
|
272
|
-
* so a site with no archive keeps its navigation bar unchanged.
|
|
295
|
+
* The component renders nothing otherwise, so a site with a single version keeps its navigation bar unchanged.
|
|
273
296
|
*
|
|
274
297
|
* @example
|
|
275
298
|
* ```ts
|
|
@@ -282,11 +305,11 @@ export interface VersionSwitcherInterface {
|
|
|
282
305
|
hasVersions: ComputedRef<boolean>;
|
|
283
306
|
|
|
284
307
|
/**
|
|
285
|
-
* The version
|
|
308
|
+
* The version that the current page belongs to.
|
|
286
309
|
*
|
|
287
310
|
* @remarks
|
|
288
|
-
* The version sits in the first segment, or in the second one when a locale leads
|
|
289
|
-
* A page with no version segment belongs to the
|
|
311
|
+
* The version sits in the first segment of the page path, or in the second one when a locale leads it.
|
|
312
|
+
* A page with no version segment, such as the home page at the root, belongs to the stable version.
|
|
290
313
|
*
|
|
291
314
|
* @example
|
|
292
315
|
* ```ts
|
|
@@ -300,16 +323,11 @@ export interface VersionSwitcherInterface {
|
|
|
300
323
|
activeVersion: ComputedRef<string>;
|
|
301
324
|
|
|
302
325
|
/**
|
|
303
|
-
* The label of the active version as the switcher shows it.
|
|
304
|
-
*
|
|
305
|
-
* @remarks
|
|
306
|
-
* This is {@link VersionSwitcherInterface.activeVersion} with the tag of {@link PropsInterface.latestLabel}
|
|
307
|
-
* appended when the active version is the current one.
|
|
308
|
-
* The flyout button renders it, while every path helper keeps working with the bare label.
|
|
326
|
+
* The label of the active version as the switcher button shows it.
|
|
309
327
|
*
|
|
310
328
|
* @example
|
|
311
329
|
* ```ts
|
|
312
|
-
* // on /de/guide/index.md
|
|
330
|
+
* // on /de/v2.0.x/guide/index.md
|
|
313
331
|
* activeVersionText.value; // 'v2.0.x (latest)'
|
|
314
332
|
* ```
|
|
315
333
|
*
|
|
@@ -319,15 +337,12 @@ export interface VersionSwitcherInterface {
|
|
|
319
337
|
activeVersionText: ComputedRef<string>;
|
|
320
338
|
|
|
321
339
|
/**
|
|
322
|
-
* The
|
|
323
|
-
*
|
|
324
|
-
* @remarks
|
|
325
|
-
* The active version is left out, so the menu never links to the page the reader already reads.
|
|
340
|
+
* The versions that the reader can switch to, without the active one.
|
|
326
341
|
*
|
|
327
342
|
* @example
|
|
328
343
|
* ```ts
|
|
329
344
|
* // on /de/v1.0.x/guide/index.md
|
|
330
|
-
* availableVersions.value; // [ '
|
|
345
|
+
* availableVersions.value; // [ 'next', 'v2.0.x' ]
|
|
331
346
|
* ```
|
|
332
347
|
*
|
|
333
348
|
* @since 2.1.0
|
|
@@ -335,38 +350,21 @@ export interface VersionSwitcherInterface {
|
|
|
335
350
|
|
|
336
351
|
availableVersions: ComputedRef<Array<string>>;
|
|
337
352
|
|
|
338
|
-
/**
|
|
339
|
-
* Whether the desktop menu needs an entry for the current version.
|
|
340
|
-
*
|
|
341
|
-
* @remarks
|
|
342
|
-
* The entry appears only while the reader is on an archived page,
|
|
343
|
-
* since the current version is the active one everywhere else.
|
|
344
|
-
*
|
|
345
|
-
* @example
|
|
346
|
-
* ```ts
|
|
347
|
-
* shouldShowCurrentVersion.value; // true on an archived page
|
|
348
|
-
* ```
|
|
349
|
-
*
|
|
350
|
-
* @since 2.1.0
|
|
351
|
-
*/
|
|
352
|
-
|
|
353
|
-
shouldShowCurrentVersion: ComputedRef<boolean>;
|
|
354
|
-
|
|
355
353
|
/**
|
|
356
354
|
* Builds the menu entry that links to a version.
|
|
357
355
|
*
|
|
358
|
-
* @param version - The version
|
|
359
|
-
* @returns A {@link VersionMenuItemInterface}
|
|
356
|
+
* @param version - The name of the version that the entry points at
|
|
357
|
+
* @returns A {@link VersionMenuItemInterface} with the label of the version and the path of the current page in it
|
|
360
358
|
*
|
|
361
359
|
* @remarks
|
|
362
|
-
* The
|
|
363
|
-
*
|
|
360
|
+
* The link keeps the locale and the page of the reader.
|
|
361
|
+
* When the version never published that page, the link falls back to the home page of the version in the locale,
|
|
362
|
+
* and when the locale has no pages in that version either, to the home page of the version itself.
|
|
364
363
|
*
|
|
365
364
|
* @example
|
|
366
365
|
* ```ts
|
|
367
|
-
* // on /de/guide/index.md
|
|
368
|
-
* createVersionMenuItem('v1.0.x');
|
|
369
|
-
* // { text: 'v1.0.x', link: '/de/v1.0.x/guide/index.md' }
|
|
366
|
+
* // on /de/v2.0.x/guide/index.md
|
|
367
|
+
* createVersionMenuItem('v1.0.x'); // { text: 'v1.0.x (LTS)', link: '/de/v1.0.x/guide/index.md' }
|
|
370
368
|
* ```
|
|
371
369
|
*
|
|
372
370
|
* @since 2.1.0
|
|
@@ -377,12 +375,8 @@ export interface VersionSwitcherInterface {
|
|
|
377
375
|
/**
|
|
378
376
|
* Opens or closes the mobile dropdown.
|
|
379
377
|
*
|
|
380
|
-
* @remarks
|
|
381
|
-
* Only the mobile layout calls this, since the desktop flyout keeps its own open state.
|
|
382
|
-
*
|
|
383
378
|
* @example
|
|
384
379
|
* ```ts
|
|
385
|
-
* isOpen.value; // false
|
|
386
380
|
* toggle();
|
|
387
381
|
* isOpen.value; // true
|
|
388
382
|
* ```
|
|
@@ -400,10 +394,8 @@ export interface VersionSwitcherInterface {
|
|
|
400
394
|
* @returns The bindings that the template renders {@link VersionSwitcherInterface}
|
|
401
395
|
*
|
|
402
396
|
* @remarks
|
|
403
|
-
* The composable reads the router and the site data of the
|
|
404
|
-
*
|
|
405
|
-
* Every menu entry is rebuilt on render, which keeps the links pointed at the page the reader is on
|
|
406
|
-
* as the reader moves through the site.
|
|
397
|
+
* The composable reads the router and the site data, so only the setup function of the component may call it.
|
|
398
|
+
* The versions come from `props.versioningPlugin`, or else from `themeConfig.versioning` of the site.
|
|
407
399
|
*
|
|
408
400
|
* @example
|
|
409
401
|
* ```ts
|
|
@@ -417,353 +409,53 @@ export interface VersionSwitcherInterface {
|
|
|
417
409
|
|
|
418
410
|
export function useVersionSwitcher(props: PropsInterface): VersionSwitcherInterface {
|
|
419
411
|
const router = useRouter();
|
|
420
|
-
const { site } = useData();
|
|
412
|
+
const { site, theme } = useData();
|
|
421
413
|
const isOpen = ref(false);
|
|
422
414
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
* @remarks
|
|
427
|
-
* Every segment of every path the switcher handles is tested against this set
|
|
428
|
-
* to tell a version apart from an ordinary segment.
|
|
429
|
-
*
|
|
430
|
-
* @example
|
|
431
|
-
* ```ts
|
|
432
|
-
* const versions = versionSet.value;
|
|
433
|
-
* versions.has('v1.0.x'); // true
|
|
434
|
-
* versions.has('guide'); // false
|
|
435
|
-
* ```
|
|
436
|
-
*
|
|
437
|
-
* @since 2.1.0
|
|
438
|
-
*/
|
|
439
|
-
|
|
440
|
-
const versionSet = computed(() => new Set([ ...props.versioningPlugin.versions ]));
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* Whether the site publishes any archived version.
|
|
444
|
-
*
|
|
445
|
-
* @remarks
|
|
446
|
-
* The component renders nothing while the set is empty.
|
|
447
|
-
*
|
|
448
|
-
* @since 2.1.0
|
|
449
|
-
*/
|
|
450
|
-
|
|
451
|
-
const hasVersions = computed(() => versionSet.value.size > 0);
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
* The prefix of the active locale, without its slashes.
|
|
455
|
-
*
|
|
456
|
-
* @remarks
|
|
457
|
-
* The root locale has no prefix of its own, so this value is an empty string there
|
|
458
|
-
* and no path the switcher builds gains a locale segment.
|
|
459
|
-
*
|
|
460
|
-
* @example
|
|
461
|
-
* ```ts
|
|
462
|
-
* currentLocale.value; // 'de'
|
|
463
|
-
* ```
|
|
464
|
-
*
|
|
465
|
-
* @since 2.1.0
|
|
466
|
-
*/
|
|
467
|
-
|
|
468
|
-
const currentLocale = computed(() => {
|
|
469
|
-
const { locales, localeIndex } = site.value;
|
|
470
|
-
|
|
471
|
-
return locales[localeIndex ?? '']?.link?.replace(/\//g, '') || '';
|
|
472
|
-
});
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* The relative path of the current page, split on the slash.
|
|
476
|
-
*
|
|
477
|
-
* @remarks
|
|
478
|
-
* VitePress reports the source path here, so the last segment still carries its `.md` extension,
|
|
479
|
-
* and a route of {@link VersioningPluginInterface.routes} matches it as written.
|
|
480
|
-
*
|
|
481
|
-
* @example
|
|
482
|
-
* ```ts
|
|
483
|
-
* pathSegments.value; // [ 'de', 'v1.0.x', 'guide', 'index.md' ]
|
|
484
|
-
* ```
|
|
485
|
-
*
|
|
486
|
-
* @since 2.1.0
|
|
487
|
-
*/
|
|
488
|
-
|
|
489
|
-
const pathSegments = computed(() => router.route.data.relativePath.split('/'));
|
|
415
|
+
const plugin = computed<VersioningPluginInterface>(
|
|
416
|
+
() => props.versioningPlugin ?? theme.value.versioning ?? { versions: [], latestVersion: '' }
|
|
417
|
+
);
|
|
490
418
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
* The version sits in the first segment, or in the second one when a locale leads the path.
|
|
496
|
-
* A page with no version segment belongs to the current version.
|
|
497
|
-
*
|
|
498
|
-
* @since 2.1.0
|
|
499
|
-
*/
|
|
419
|
+
const versionSet = computed(() => new Set(plugin.value.versions));
|
|
420
|
+
const locale = computed(() => site.value.locales[site.value.localeIndex ?? '']?.link?.replace(/\//g, '') || '');
|
|
421
|
+
const segments = computed(() => router.route.data.relativePath.split('/'));
|
|
422
|
+
const versionIndex = computed(() => (segments.value[0] === locale.value ? 1 : 0));
|
|
500
423
|
|
|
501
424
|
const activeVersion = computed(() => {
|
|
502
|
-
const
|
|
503
|
-
const segments = pathSegments.value;
|
|
504
|
-
const locale = currentLocale.value;
|
|
505
|
-
|
|
506
|
-
// Determine which segment might be the version
|
|
507
|
-
const versionCandidate = segments[0] === locale ? segments[1] : segments[0];
|
|
425
|
+
const candidate = segments.value[versionIndex.value];
|
|
508
426
|
|
|
509
|
-
return versionSet.value.has(
|
|
427
|
+
return versionSet.value.has(candidate) ? candidate : plugin.value.latestVersion;
|
|
510
428
|
});
|
|
511
429
|
|
|
512
|
-
/**
|
|
513
|
-
* Builds the label that the switcher shows for a version.
|
|
514
|
-
*
|
|
515
|
-
* @param version - The version label to display
|
|
516
|
-
* @returns The label, tagged as the latest one when it names the current version
|
|
517
|
-
*
|
|
518
|
-
* @remarks
|
|
519
|
-
* The tag comes from {@link PropsInterface.latestLabel} and defaults to `latest`.
|
|
520
|
-
* An empty tag, or a current version that already reads as the tag, leaves the label as it is,
|
|
521
|
-
* so the default `current: 'latest'` never shows as `latest (latest)`.
|
|
522
|
-
*
|
|
523
|
-
* @example
|
|
524
|
-
* ```ts
|
|
525
|
-
* versionText('v2.0.x'); // 'v2.0.x (latest)'
|
|
526
|
-
* versionText('v1.0.x'); // 'v1.0.x'
|
|
527
|
-
* ```
|
|
528
|
-
*
|
|
529
|
-
* @since 2.2.0
|
|
530
|
-
*/
|
|
531
|
-
|
|
532
430
|
function versionText(version: string): string {
|
|
533
431
|
const tag = props.latestLabel ?? 'latest';
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
return `${ version } (${ tag })`;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
/**
|
|
541
|
-
* The label of the active version as the switcher shows it.
|
|
542
|
-
*
|
|
543
|
-
* @remarks
|
|
544
|
-
* The flyout button renders it, while every path helper keeps working with the bare label.
|
|
545
|
-
*
|
|
546
|
-
* @since 2.2.0
|
|
547
|
-
*/
|
|
548
|
-
|
|
549
|
-
const activeVersionText = computed(() => versionText(activeVersion.value));
|
|
550
|
-
|
|
551
|
-
/**
|
|
552
|
-
* The archived versions that the reader can switch to from the current page.
|
|
553
|
-
*
|
|
554
|
-
* @remarks
|
|
555
|
-
* The active version is left out, so the menu never links to the page the reader already reads.
|
|
556
|
-
*
|
|
557
|
-
* @since 2.1.0
|
|
558
|
-
*/
|
|
559
|
-
|
|
560
|
-
const availableVersions = computed(() =>
|
|
561
|
-
Array.from(props.versioningPlugin.versions).filter(v => v !== activeVersion.value)
|
|
562
|
-
);
|
|
563
|
-
|
|
564
|
-
/**
|
|
565
|
-
* Whether the desktop menu needs an entry for the current version.
|
|
566
|
-
*
|
|
567
|
-
* @remarks
|
|
568
|
-
* The entry appears only while the reader is on an archived page.
|
|
569
|
-
*
|
|
570
|
-
* @since 2.1.0
|
|
571
|
-
*/
|
|
572
|
-
|
|
573
|
-
const shouldShowCurrentVersion = computed(() =>
|
|
574
|
-
activeVersion.value !== props.versioningPlugin.currentVersion
|
|
575
|
-
);
|
|
576
|
-
|
|
577
|
-
/**
|
|
578
|
-
* Tells whether the leading segment of a path is the active locale.
|
|
579
|
-
*
|
|
580
|
-
* @param segments - The path segments to inspect
|
|
581
|
-
* @returns `true` when the first segment matches the active locale
|
|
582
|
-
*
|
|
583
|
-
* @remarks
|
|
584
|
-
* The root locale resolves to an empty prefix, which matches no segment,
|
|
585
|
-
* so a path under the root locale always reports `false`.
|
|
586
|
-
*
|
|
587
|
-
* @example
|
|
588
|
-
* ```ts
|
|
589
|
-
* isLocaleFirst([ 'de', 'guide', 'index.md' ]); // true
|
|
590
|
-
* isLocaleFirst([ 'guide', 'index.md' ]); // false
|
|
591
|
-
* ```
|
|
592
|
-
*
|
|
593
|
-
* @since 2.1.0
|
|
594
|
-
*/
|
|
595
|
-
|
|
596
|
-
function isLocaleFirst(segments: Array<string>): boolean {
|
|
597
|
-
return segments[0] === currentLocale.value;
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
/**
|
|
601
|
-
* Strips every version segment out of a path.
|
|
602
|
-
*
|
|
603
|
-
* @param segments - The path segments to filter
|
|
604
|
-
* @returns The segments that name no version
|
|
605
|
-
*
|
|
606
|
-
* @remarks
|
|
607
|
-
* The result is the version-free form of the path,
|
|
608
|
-
* which the switcher then rebuilds for the version the reader picked.
|
|
609
|
-
*
|
|
610
|
-
* @example
|
|
611
|
-
* ```ts
|
|
612
|
-
* removeVersionSegments([ 'de', 'v1.0.x', 'guide', 'index.md' ]);
|
|
613
|
-
* // [ 'de', 'guide', 'index.md' ]
|
|
614
|
-
* ```
|
|
615
|
-
*
|
|
616
|
-
* @since 2.1.0
|
|
617
|
-
*/
|
|
618
|
-
|
|
619
|
-
function removeVersionSegments(segments: Array<string>): Array<string> {
|
|
620
|
-
return segments.filter(seg => !versionSet.value.has(seg));
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
/**
|
|
624
|
-
* Tells whether a version publishes the given path.
|
|
625
|
-
*
|
|
626
|
-
* @param version - The version label to look the path up in
|
|
627
|
-
* @param path - The path to check, with its leading slash
|
|
628
|
-
* @returns `true` when the version publishes the path, or when the plugin lists no routes
|
|
629
|
-
*
|
|
630
|
-
* @remarks
|
|
631
|
-
* The plugin lists the routes of every version, so the switcher can check a link before it renders.
|
|
632
|
-
* An older plugin, or the component used on its own, ships no such list,
|
|
633
|
-
* and the check then passes for every path.
|
|
634
|
-
*
|
|
635
|
-
* @example
|
|
636
|
-
* ```ts
|
|
637
|
-
* routeExists('v1.0.x', '/v1.0.x/guide/index.md'); // true
|
|
638
|
-
* routeExists('v1.0.x', '/v1.0.x/api/index.md'); // false
|
|
639
|
-
* ```
|
|
640
|
-
*
|
|
641
|
-
* @since 2.1.0
|
|
642
|
-
*/
|
|
432
|
+
const label = plugin.value.labels?.[version] ?? version;
|
|
433
|
+
const tagged = tag && version === plugin.value.latestVersion && label.toLowerCase() !== tag.toLowerCase();
|
|
643
434
|
|
|
644
|
-
|
|
645
|
-
const routes = props.versioningPlugin.routes?.[version];
|
|
646
|
-
if (!routes) return true;
|
|
647
|
-
|
|
648
|
-
return routes.includes(path.replace(/^\//, ''));
|
|
435
|
+
return tagged ? `${ label } (${ tag })` : label;
|
|
649
436
|
}
|
|
650
437
|
|
|
651
|
-
/**
|
|
652
|
-
* Builds the home path of a version.
|
|
653
|
-
*
|
|
654
|
-
* @param version - The version label to build the home path for
|
|
655
|
-
* @returns The path of that version's home, with a trailing slash
|
|
656
|
-
*
|
|
657
|
-
* @remarks
|
|
658
|
-
* The path keeps the locale of the current page and drops every other segment.
|
|
659
|
-
* The current version takes no version segment, so its home is the locale root.
|
|
660
|
-
*
|
|
661
|
-
* @example
|
|
662
|
-
* ```ts
|
|
663
|
-
* // on /de/v1.0.x/guide/index.md
|
|
664
|
-
* versionHome('v0.9.x'); // '/de/v0.9.x/'
|
|
665
|
-
* versionHome('v2.0.x'); // '/de/'
|
|
666
|
-
* ```
|
|
667
|
-
*
|
|
668
|
-
* @since 2.1.0
|
|
669
|
-
*/
|
|
670
|
-
|
|
671
|
-
function versionHome(version: string): string {
|
|
672
|
-
const { currentVersion } = props.versioningPlugin;
|
|
673
|
-
const baseSegments = removeVersionSegments(pathSegments.value);
|
|
674
|
-
const locale = isLocaleFirst(baseSegments) ? baseSegments[0] : '';
|
|
675
|
-
const segments = [ locale, version === currentVersion ? '' : version ].filter(Boolean);
|
|
676
|
-
|
|
677
|
-
return segments.length ? `/${ segments.join('/') }/` : '/';
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
/**
|
|
681
|
-
* Builds the path of the current page in another version.
|
|
682
|
-
*
|
|
683
|
-
* @param version - The version label to move the current page to
|
|
684
|
-
* @returns The path of the page in that version, or that version's home when the page is missing there
|
|
685
|
-
*
|
|
686
|
-
* @remarks
|
|
687
|
-
* The switcher drops every version segment first,
|
|
688
|
-
* then puts the picked version behind the locale where the path carries one, and in front otherwise.
|
|
689
|
-
* The current version takes no segment at all, since the site serves it at its root.
|
|
690
|
-
*
|
|
691
|
-
* A reader who leaves a page that the other version never published would land on a dead link,
|
|
692
|
-
* so {@link routeExists} checks the path and {@link versionHome} takes over when the check fails.
|
|
693
|
-
*
|
|
694
|
-
* @example
|
|
695
|
-
* ```ts
|
|
696
|
-
* // on /de/v1.0.x/guide/index.md
|
|
697
|
-
* buildVersionPath('v2.0.x'); // '/de/guide/index.md'
|
|
698
|
-
* buildVersionPath('v0.9.x'); // '/de/v0.9.x/' when v0.9.x has no guide page
|
|
699
|
-
* ```
|
|
700
|
-
*
|
|
701
|
-
* @see routeExists
|
|
702
|
-
* @see versionHome
|
|
703
|
-
*
|
|
704
|
-
* @since 2.1.0
|
|
705
|
-
*/
|
|
706
|
-
|
|
707
|
-
function buildVersionPath(version: string): string {
|
|
708
|
-
const { currentVersion } = props.versioningPlugin;
|
|
709
|
-
const baseSegments = removeVersionSegments(pathSegments.value);
|
|
710
|
-
const hasLocale = isLocaleFirst(baseSegments);
|
|
711
|
-
|
|
712
|
-
let newSegments: Array<string>;
|
|
713
|
-
|
|
714
|
-
if (version === currentVersion) {
|
|
715
|
-
newSegments = baseSegments;
|
|
716
|
-
} else if (hasLocale) {
|
|
717
|
-
newSegments = [ baseSegments[0], version, ...baseSegments.slice(1) ];
|
|
718
|
-
} else {
|
|
719
|
-
newSegments = [ version, ...baseSegments ];
|
|
720
|
-
}
|
|
721
|
-
|
|
722
|
-
const path = `/${ newSegments.join('/') }`;
|
|
723
|
-
|
|
724
|
-
return routeExists(version, path) ? path : versionHome(version);
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
/**
|
|
728
|
-
* Builds the menu entry that links to a version.
|
|
729
|
-
*
|
|
730
|
-
* @param version - The version label that the entry points at
|
|
731
|
-
* @returns A {@link VersionMenuItemInterface} carrying the label and the path of that version
|
|
732
|
-
*
|
|
733
|
-
* @remarks
|
|
734
|
-
* The template calls this for every entry it renders.
|
|
735
|
-
*
|
|
736
|
-
* @since 2.1.0
|
|
737
|
-
*/
|
|
738
|
-
|
|
739
438
|
function createVersionMenuItem(version: string): VersionMenuItemInterface {
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
};
|
|
744
|
-
}
|
|
439
|
+
const index = versionIndex.value;
|
|
440
|
+
const page = [ ...segments.value ];
|
|
441
|
+
page.splice(index, versionSet.value.has(page[index]) ? 1 : 0, version);
|
|
745
442
|
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
* Only the mobile layout calls this.
|
|
751
|
-
*
|
|
752
|
-
* @since 2.1.0
|
|
753
|
-
*/
|
|
443
|
+
const path = page.join('/');
|
|
444
|
+
const home = [ ...page.slice(0, index + 1), 'index.md' ].join('/');
|
|
445
|
+
const routes = plugin.value.routes?.[version];
|
|
446
|
+
if (!routes || routes.includes(path)) return { text: versionText(version), link: `/${ path }` };
|
|
754
447
|
|
|
755
|
-
|
|
756
|
-
isOpen.value = !isOpen.value;
|
|
448
|
+
return { text: versionText(version), link: routes.includes(home) ? `/${ home }` : `/${ version }/` };
|
|
757
449
|
}
|
|
758
450
|
|
|
759
451
|
return {
|
|
760
452
|
isOpen,
|
|
761
|
-
|
|
762
|
-
hasVersions,
|
|
453
|
+
versions: computed(() => plugin.value.versions),
|
|
454
|
+
hasVersions: computed(() => versionSet.value.size > 1),
|
|
763
455
|
activeVersion,
|
|
764
|
-
activeVersionText,
|
|
765
|
-
availableVersions,
|
|
456
|
+
activeVersionText: computed(() => versionText(activeVersion.value)),
|
|
457
|
+
availableVersions: computed(() => plugin.value.versions.filter((version: string) => version !== activeVersion.value)),
|
|
766
458
|
createVersionMenuItem,
|
|
767
|
-
|
|
459
|
+
toggle: () => (isOpen.value = !isOpen.value)
|
|
768
460
|
};
|
|
769
461
|
}
|