@viteplus/versions 2.2.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 +131 -9
- package/components/fallback-component.spec.ts +137 -0
- package/components/fallback.component.ts +221 -0
- package/components/version-switcher.component.ts +136 -421
- package/components/version-switcher.component.vue +5 -10
- 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
|
|
98
|
+
*/
|
|
99
|
+
|
|
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
|
|
85
112
|
*/
|
|
86
113
|
|
|
87
|
-
|
|
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
|
|
@@ -158,14 +184,33 @@ export interface PropsInterface {
|
|
|
158
184
|
*/
|
|
159
185
|
|
|
160
186
|
latestLabel?: string;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* The label of the switcher button.
|
|
190
|
+
*
|
|
191
|
+
* @remarks
|
|
192
|
+
* The desktop flyout shows it as the accessible label of its button,
|
|
193
|
+
* and the mobile dropdown shows it as the text of its button.
|
|
194
|
+
* When omitted, it reads `Switch Version`.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* const nav = [{ component: 'VersionSwitcher', props: { label: 'Version wechseln' } }];
|
|
199
|
+
* // the button reads 'Version wechseln'
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* @see https://github.com/viteplus/versions/issues/51
|
|
203
|
+
* @since 2.3.0
|
|
204
|
+
*/
|
|
205
|
+
|
|
206
|
+
label?: string;
|
|
161
207
|
}
|
|
162
208
|
|
|
163
209
|
/**
|
|
164
210
|
* The shape of a single entry of the version menu.
|
|
165
211
|
*
|
|
166
212
|
* @remarks
|
|
167
|
-
* `VPMenuLink` of the default theme consumes this shape as it is
|
|
168
|
-
* so an entry needs no further mapping before it renders.
|
|
213
|
+
* `VPMenuLink` of the default theme consumes this shape as it is.
|
|
169
214
|
*
|
|
170
215
|
* @example
|
|
171
216
|
* ```ts
|
|
@@ -179,14 +224,10 @@ export interface VersionMenuItemInterface {
|
|
|
179
224
|
/**
|
|
180
225
|
* The label that the entry shows.
|
|
181
226
|
*
|
|
182
|
-
* @remarks
|
|
183
|
-
* The version label is used verbatim, so the menu reads the same as the archive folders.
|
|
184
|
-
* The current version alone carries the tag of {@link PropsInterface.latestLabel}.
|
|
185
|
-
*
|
|
186
227
|
* @example
|
|
187
228
|
* ```ts
|
|
188
229
|
* const { text } = item;
|
|
189
|
-
* text; // 'v1.0.x'
|
|
230
|
+
* text; // 'v1.0.x (LTS)'
|
|
190
231
|
* ```
|
|
191
232
|
*
|
|
192
233
|
* @since 2.1.0
|
|
@@ -212,10 +253,6 @@ export interface VersionMenuItemInterface {
|
|
|
212
253
|
/**
|
|
213
254
|
* The bindings that {@link useVersionSwitcher} exposes to the template.
|
|
214
255
|
*
|
|
215
|
-
* @remarks
|
|
216
|
-
* Only what the template renders is exposed.
|
|
217
|
-
* Every path helper stays inside the composable, since each one reads the props and the route.
|
|
218
|
-
*
|
|
219
256
|
* @example
|
|
220
257
|
* ```ts
|
|
221
258
|
* const { hasVersions, toggle } = useVersionSwitcher(props);
|
|
@@ -228,9 +265,6 @@ export interface VersionSwitcherInterface {
|
|
|
228
265
|
/**
|
|
229
266
|
* Whether the mobile dropdown is open.
|
|
230
267
|
*
|
|
231
|
-
* @remarks
|
|
232
|
-
* The desktop flyout keeps its own open state, so this ref drives the mobile layout alone.
|
|
233
|
-
*
|
|
234
268
|
* @example
|
|
235
269
|
* ```ts
|
|
236
270
|
* isOpen.value; // false while the dropdown is collapsed
|
|
@@ -242,11 +276,23 @@ export interface VersionSwitcherInterface {
|
|
|
242
276
|
isOpen: Ref<boolean>;
|
|
243
277
|
|
|
244
278
|
/**
|
|
245
|
-
*
|
|
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.
|
|
246
293
|
*
|
|
247
294
|
* @remarks
|
|
248
|
-
* The component renders nothing
|
|
249
|
-
* 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.
|
|
250
296
|
*
|
|
251
297
|
* @example
|
|
252
298
|
* ```ts
|
|
@@ -259,11 +305,11 @@ export interface VersionSwitcherInterface {
|
|
|
259
305
|
hasVersions: ComputedRef<boolean>;
|
|
260
306
|
|
|
261
307
|
/**
|
|
262
|
-
* The version
|
|
308
|
+
* The version that the current page belongs to.
|
|
263
309
|
*
|
|
264
310
|
* @remarks
|
|
265
|
-
* The version sits in the first segment, or in the second one when a locale leads
|
|
266
|
-
* 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.
|
|
267
313
|
*
|
|
268
314
|
* @example
|
|
269
315
|
* ```ts
|
|
@@ -277,16 +323,11 @@ export interface VersionSwitcherInterface {
|
|
|
277
323
|
activeVersion: ComputedRef<string>;
|
|
278
324
|
|
|
279
325
|
/**
|
|
280
|
-
* The label of the active version as the switcher shows it.
|
|
281
|
-
*
|
|
282
|
-
* @remarks
|
|
283
|
-
* This is {@link VersionSwitcherInterface.activeVersion} with the tag of {@link PropsInterface.latestLabel}
|
|
284
|
-
* appended when the active version is the current one.
|
|
285
|
-
* 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.
|
|
286
327
|
*
|
|
287
328
|
* @example
|
|
288
329
|
* ```ts
|
|
289
|
-
* // on /de/guide/index.md
|
|
330
|
+
* // on /de/v2.0.x/guide/index.md
|
|
290
331
|
* activeVersionText.value; // 'v2.0.x (latest)'
|
|
291
332
|
* ```
|
|
292
333
|
*
|
|
@@ -296,15 +337,12 @@ export interface VersionSwitcherInterface {
|
|
|
296
337
|
activeVersionText: ComputedRef<string>;
|
|
297
338
|
|
|
298
339
|
/**
|
|
299
|
-
* The
|
|
300
|
-
*
|
|
301
|
-
* @remarks
|
|
302
|
-
* 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.
|
|
303
341
|
*
|
|
304
342
|
* @example
|
|
305
343
|
* ```ts
|
|
306
344
|
* // on /de/v1.0.x/guide/index.md
|
|
307
|
-
* availableVersions.value; // [ '
|
|
345
|
+
* availableVersions.value; // [ 'next', 'v2.0.x' ]
|
|
308
346
|
* ```
|
|
309
347
|
*
|
|
310
348
|
* @since 2.1.0
|
|
@@ -312,38 +350,21 @@ export interface VersionSwitcherInterface {
|
|
|
312
350
|
|
|
313
351
|
availableVersions: ComputedRef<Array<string>>;
|
|
314
352
|
|
|
315
|
-
/**
|
|
316
|
-
* Whether the desktop menu needs an entry for the current version.
|
|
317
|
-
*
|
|
318
|
-
* @remarks
|
|
319
|
-
* The entry appears only while the reader is on an archived page,
|
|
320
|
-
* since the current version is the active one everywhere else.
|
|
321
|
-
*
|
|
322
|
-
* @example
|
|
323
|
-
* ```ts
|
|
324
|
-
* shouldShowCurrentVersion.value; // true on an archived page
|
|
325
|
-
* ```
|
|
326
|
-
*
|
|
327
|
-
* @since 2.1.0
|
|
328
|
-
*/
|
|
329
|
-
|
|
330
|
-
shouldShowCurrentVersion: ComputedRef<boolean>;
|
|
331
|
-
|
|
332
353
|
/**
|
|
333
354
|
* Builds the menu entry that links to a version.
|
|
334
355
|
*
|
|
335
|
-
* @param version - The version
|
|
336
|
-
* @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
|
|
337
358
|
*
|
|
338
359
|
* @remarks
|
|
339
|
-
* The
|
|
340
|
-
*
|
|
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.
|
|
341
363
|
*
|
|
342
364
|
* @example
|
|
343
365
|
* ```ts
|
|
344
|
-
* // on /de/guide/index.md
|
|
345
|
-
* createVersionMenuItem('v1.0.x');
|
|
346
|
-
* // { 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' }
|
|
347
368
|
* ```
|
|
348
369
|
*
|
|
349
370
|
* @since 2.1.0
|
|
@@ -354,12 +375,8 @@ export interface VersionSwitcherInterface {
|
|
|
354
375
|
/**
|
|
355
376
|
* Opens or closes the mobile dropdown.
|
|
356
377
|
*
|
|
357
|
-
* @remarks
|
|
358
|
-
* Only the mobile layout calls this, since the desktop flyout keeps its own open state.
|
|
359
|
-
*
|
|
360
378
|
* @example
|
|
361
379
|
* ```ts
|
|
362
|
-
* isOpen.value; // false
|
|
363
380
|
* toggle();
|
|
364
381
|
* isOpen.value; // true
|
|
365
382
|
* ```
|
|
@@ -377,10 +394,8 @@ export interface VersionSwitcherInterface {
|
|
|
377
394
|
* @returns The bindings that the template renders {@link VersionSwitcherInterface}
|
|
378
395
|
*
|
|
379
396
|
* @remarks
|
|
380
|
-
* The composable reads the router and the site data of the
|
|
381
|
-
*
|
|
382
|
-
* Every menu entry is rebuilt on render, which keeps the links pointed at the page the reader is on
|
|
383
|
-
* 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.
|
|
384
399
|
*
|
|
385
400
|
* @example
|
|
386
401
|
* ```ts
|
|
@@ -394,353 +409,53 @@ export interface VersionSwitcherInterface {
|
|
|
394
409
|
|
|
395
410
|
export function useVersionSwitcher(props: PropsInterface): VersionSwitcherInterface {
|
|
396
411
|
const router = useRouter();
|
|
397
|
-
const { site } = useData();
|
|
412
|
+
const { site, theme } = useData();
|
|
398
413
|
const isOpen = ref(false);
|
|
399
414
|
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
* @remarks
|
|
404
|
-
* Every segment of every path the switcher handles is tested against this set
|
|
405
|
-
* to tell a version apart from an ordinary segment.
|
|
406
|
-
*
|
|
407
|
-
* @example
|
|
408
|
-
* ```ts
|
|
409
|
-
* const versions = versionSet.value;
|
|
410
|
-
* versions.has('v1.0.x'); // true
|
|
411
|
-
* versions.has('guide'); // false
|
|
412
|
-
* ```
|
|
413
|
-
*
|
|
414
|
-
* @since 2.1.0
|
|
415
|
-
*/
|
|
416
|
-
|
|
417
|
-
const versionSet = computed(() => new Set([ ...props.versioningPlugin.versions ]));
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* Whether the site publishes any archived version.
|
|
421
|
-
*
|
|
422
|
-
* @remarks
|
|
423
|
-
* The component renders nothing while the set is empty.
|
|
424
|
-
*
|
|
425
|
-
* @since 2.1.0
|
|
426
|
-
*/
|
|
427
|
-
|
|
428
|
-
const hasVersions = computed(() => versionSet.value.size > 0);
|
|
429
|
-
|
|
430
|
-
/**
|
|
431
|
-
* The prefix of the active locale, without its slashes.
|
|
432
|
-
*
|
|
433
|
-
* @remarks
|
|
434
|
-
* The root locale has no prefix of its own, so this value is an empty string there
|
|
435
|
-
* and no path the switcher builds gains a locale segment.
|
|
436
|
-
*
|
|
437
|
-
* @example
|
|
438
|
-
* ```ts
|
|
439
|
-
* currentLocale.value; // 'de'
|
|
440
|
-
* ```
|
|
441
|
-
*
|
|
442
|
-
* @since 2.1.0
|
|
443
|
-
*/
|
|
444
|
-
|
|
445
|
-
const currentLocale = computed(() => {
|
|
446
|
-
const { locales, localeIndex } = site.value;
|
|
447
|
-
|
|
448
|
-
return locales[localeIndex ?? '']?.link?.replace(/\//g, '') || '';
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* The relative path of the current page, split on the slash.
|
|
453
|
-
*
|
|
454
|
-
* @remarks
|
|
455
|
-
* VitePress reports the source path here, so the last segment still carries its `.md` extension,
|
|
456
|
-
* and a route of {@link VersioningPluginInterface.routes} matches it as written.
|
|
457
|
-
*
|
|
458
|
-
* @example
|
|
459
|
-
* ```ts
|
|
460
|
-
* pathSegments.value; // [ 'de', 'v1.0.x', 'guide', 'index.md' ]
|
|
461
|
-
* ```
|
|
462
|
-
*
|
|
463
|
-
* @since 2.1.0
|
|
464
|
-
*/
|
|
415
|
+
const plugin = computed<VersioningPluginInterface>(
|
|
416
|
+
() => props.versioningPlugin ?? theme.value.versioning ?? { versions: [], latestVersion: '' }
|
|
417
|
+
);
|
|
465
418
|
|
|
466
|
-
const
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
*
|
|
471
|
-
* @remarks
|
|
472
|
-
* The version sits in the first segment, or in the second one when a locale leads the path.
|
|
473
|
-
* A page with no version segment belongs to the current version.
|
|
474
|
-
*
|
|
475
|
-
* @since 2.1.0
|
|
476
|
-
*/
|
|
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));
|
|
477
423
|
|
|
478
424
|
const activeVersion = computed(() => {
|
|
479
|
-
const
|
|
480
|
-
const segments = pathSegments.value;
|
|
481
|
-
const locale = currentLocale.value;
|
|
425
|
+
const candidate = segments.value[versionIndex.value];
|
|
482
426
|
|
|
483
|
-
|
|
484
|
-
const versionCandidate = segments[0] === locale ? segments[1] : segments[0];
|
|
485
|
-
|
|
486
|
-
return versionSet.value.has(versionCandidate) ? versionCandidate : currentVersion;
|
|
427
|
+
return versionSet.value.has(candidate) ? candidate : plugin.value.latestVersion;
|
|
487
428
|
});
|
|
488
429
|
|
|
489
|
-
/**
|
|
490
|
-
* Builds the label that the switcher shows for a version.
|
|
491
|
-
*
|
|
492
|
-
* @param version - The version label to display
|
|
493
|
-
* @returns The label, tagged as the latest one when it names the current version
|
|
494
|
-
*
|
|
495
|
-
* @remarks
|
|
496
|
-
* The tag comes from {@link PropsInterface.latestLabel} and defaults to `latest`.
|
|
497
|
-
* An empty tag, or a current version that already reads as the tag, leaves the label as it is,
|
|
498
|
-
* so the default `current: 'latest'` never shows as `latest (latest)`.
|
|
499
|
-
*
|
|
500
|
-
* @example
|
|
501
|
-
* ```ts
|
|
502
|
-
* versionText('v2.0.x'); // 'v2.0.x (latest)'
|
|
503
|
-
* versionText('v1.0.x'); // 'v1.0.x'
|
|
504
|
-
* ```
|
|
505
|
-
*
|
|
506
|
-
* @since 2.2.0
|
|
507
|
-
*/
|
|
508
|
-
|
|
509
430
|
function versionText(version: string): string {
|
|
510
431
|
const tag = props.latestLabel ?? 'latest';
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
return `${ version } (${ tag })`;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* The label of the active version as the switcher shows it.
|
|
519
|
-
*
|
|
520
|
-
* @remarks
|
|
521
|
-
* The flyout button renders it, while every path helper keeps working with the bare label.
|
|
522
|
-
*
|
|
523
|
-
* @since 2.2.0
|
|
524
|
-
*/
|
|
525
|
-
|
|
526
|
-
const activeVersionText = computed(() => versionText(activeVersion.value));
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* The archived versions that the reader can switch to from the current page.
|
|
530
|
-
*
|
|
531
|
-
* @remarks
|
|
532
|
-
* The active version is left out, so the menu never links to the page the reader already reads.
|
|
533
|
-
*
|
|
534
|
-
* @since 2.1.0
|
|
535
|
-
*/
|
|
536
|
-
|
|
537
|
-
const availableVersions = computed(() =>
|
|
538
|
-
Array.from(props.versioningPlugin.versions).filter(v => v !== activeVersion.value)
|
|
539
|
-
);
|
|
540
|
-
|
|
541
|
-
/**
|
|
542
|
-
* Whether the desktop menu needs an entry for the current version.
|
|
543
|
-
*
|
|
544
|
-
* @remarks
|
|
545
|
-
* The entry appears only while the reader is on an archived page.
|
|
546
|
-
*
|
|
547
|
-
* @since 2.1.0
|
|
548
|
-
*/
|
|
549
|
-
|
|
550
|
-
const shouldShowCurrentVersion = computed(() =>
|
|
551
|
-
activeVersion.value !== props.versioningPlugin.currentVersion
|
|
552
|
-
);
|
|
432
|
+
const label = plugin.value.labels?.[version] ?? version;
|
|
433
|
+
const tagged = tag && version === plugin.value.latestVersion && label.toLowerCase() !== tag.toLowerCase();
|
|
553
434
|
|
|
554
|
-
|
|
555
|
-
* Tells whether the leading segment of a path is the active locale.
|
|
556
|
-
*
|
|
557
|
-
* @param segments - The path segments to inspect
|
|
558
|
-
* @returns `true` when the first segment matches the active locale
|
|
559
|
-
*
|
|
560
|
-
* @remarks
|
|
561
|
-
* The root locale resolves to an empty prefix, which matches no segment,
|
|
562
|
-
* so a path under the root locale always reports `false`.
|
|
563
|
-
*
|
|
564
|
-
* @example
|
|
565
|
-
* ```ts
|
|
566
|
-
* isLocaleFirst([ 'de', 'guide', 'index.md' ]); // true
|
|
567
|
-
* isLocaleFirst([ 'guide', 'index.md' ]); // false
|
|
568
|
-
* ```
|
|
569
|
-
*
|
|
570
|
-
* @since 2.1.0
|
|
571
|
-
*/
|
|
572
|
-
|
|
573
|
-
function isLocaleFirst(segments: Array<string>): boolean {
|
|
574
|
-
return segments[0] === currentLocale.value;
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
/**
|
|
578
|
-
* Strips every version segment out of a path.
|
|
579
|
-
*
|
|
580
|
-
* @param segments - The path segments to filter
|
|
581
|
-
* @returns The segments that name no version
|
|
582
|
-
*
|
|
583
|
-
* @remarks
|
|
584
|
-
* The result is the version-free form of the path,
|
|
585
|
-
* which the switcher then rebuilds for the version the reader picked.
|
|
586
|
-
*
|
|
587
|
-
* @example
|
|
588
|
-
* ```ts
|
|
589
|
-
* removeVersionSegments([ 'de', 'v1.0.x', 'guide', 'index.md' ]);
|
|
590
|
-
* // [ 'de', 'guide', 'index.md' ]
|
|
591
|
-
* ```
|
|
592
|
-
*
|
|
593
|
-
* @since 2.1.0
|
|
594
|
-
*/
|
|
595
|
-
|
|
596
|
-
function removeVersionSegments(segments: Array<string>): Array<string> {
|
|
597
|
-
return segments.filter(seg => !versionSet.value.has(seg));
|
|
435
|
+
return tagged ? `${ label } (${ tag })` : label;
|
|
598
436
|
}
|
|
599
437
|
|
|
600
|
-
/**
|
|
601
|
-
* Tells whether a version publishes the given path.
|
|
602
|
-
*
|
|
603
|
-
* @param version - The version label to look the path up in
|
|
604
|
-
* @param path - The path to check, with its leading slash
|
|
605
|
-
* @returns `true` when the version publishes the path, or when the plugin lists no routes
|
|
606
|
-
*
|
|
607
|
-
* @remarks
|
|
608
|
-
* The plugin lists the routes of every version, so the switcher can check a link before it renders.
|
|
609
|
-
* An older plugin, or the component used on its own, ships no such list,
|
|
610
|
-
* and the check then passes for every path.
|
|
611
|
-
*
|
|
612
|
-
* @example
|
|
613
|
-
* ```ts
|
|
614
|
-
* routeExists('v1.0.x', '/v1.0.x/guide/index.md'); // true
|
|
615
|
-
* routeExists('v1.0.x', '/v1.0.x/api/index.md'); // false
|
|
616
|
-
* ```
|
|
617
|
-
*
|
|
618
|
-
* @since 2.1.0
|
|
619
|
-
*/
|
|
620
|
-
|
|
621
|
-
function routeExists(version: string, path: string): boolean {
|
|
622
|
-
const routes = props.versioningPlugin.routes?.[version];
|
|
623
|
-
if (!routes) return true;
|
|
624
|
-
|
|
625
|
-
return routes.includes(path.replace(/^\//, ''));
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
/**
|
|
629
|
-
* Builds the home path of a version.
|
|
630
|
-
*
|
|
631
|
-
* @param version - The version label to build the home path for
|
|
632
|
-
* @returns The path of that version's home, with a trailing slash
|
|
633
|
-
*
|
|
634
|
-
* @remarks
|
|
635
|
-
* The path keeps the locale of the current page and drops every other segment.
|
|
636
|
-
* The current version takes no version segment, so its home is the locale root.
|
|
637
|
-
*
|
|
638
|
-
* @example
|
|
639
|
-
* ```ts
|
|
640
|
-
* // on /de/v1.0.x/guide/index.md
|
|
641
|
-
* versionHome('v0.9.x'); // '/de/v0.9.x/'
|
|
642
|
-
* versionHome('v2.0.x'); // '/de/'
|
|
643
|
-
* ```
|
|
644
|
-
*
|
|
645
|
-
* @since 2.1.0
|
|
646
|
-
*/
|
|
647
|
-
|
|
648
|
-
function versionHome(version: string): string {
|
|
649
|
-
const { currentVersion } = props.versioningPlugin;
|
|
650
|
-
const baseSegments = removeVersionSegments(pathSegments.value);
|
|
651
|
-
const locale = isLocaleFirst(baseSegments) ? baseSegments[0] : '';
|
|
652
|
-
const segments = [ locale, version === currentVersion ? '' : version ].filter(Boolean);
|
|
653
|
-
|
|
654
|
-
return segments.length ? `/${ segments.join('/') }/` : '/';
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
/**
|
|
658
|
-
* Builds the path of the current page in another version.
|
|
659
|
-
*
|
|
660
|
-
* @param version - The version label to move the current page to
|
|
661
|
-
* @returns The path of the page in that version, or that version's home when the page is missing there
|
|
662
|
-
*
|
|
663
|
-
* @remarks
|
|
664
|
-
* The switcher drops every version segment first,
|
|
665
|
-
* then puts the picked version behind the locale where the path carries one, and in front otherwise.
|
|
666
|
-
* The current version takes no segment at all, since the site serves it at its root.
|
|
667
|
-
*
|
|
668
|
-
* A reader who leaves a page that the other version never published would land on a dead link,
|
|
669
|
-
* so {@link routeExists} checks the path and {@link versionHome} takes over when the check fails.
|
|
670
|
-
*
|
|
671
|
-
* @example
|
|
672
|
-
* ```ts
|
|
673
|
-
* // on /de/v1.0.x/guide/index.md
|
|
674
|
-
* buildVersionPath('v2.0.x'); // '/de/guide/index.md'
|
|
675
|
-
* buildVersionPath('v0.9.x'); // '/de/v0.9.x/' when v0.9.x has no guide page
|
|
676
|
-
* ```
|
|
677
|
-
*
|
|
678
|
-
* @see routeExists
|
|
679
|
-
* @see versionHome
|
|
680
|
-
*
|
|
681
|
-
* @since 2.1.0
|
|
682
|
-
*/
|
|
683
|
-
|
|
684
|
-
function buildVersionPath(version: string): string {
|
|
685
|
-
const { currentVersion } = props.versioningPlugin;
|
|
686
|
-
const baseSegments = removeVersionSegments(pathSegments.value);
|
|
687
|
-
const hasLocale = isLocaleFirst(baseSegments);
|
|
688
|
-
|
|
689
|
-
let newSegments: Array<string>;
|
|
690
|
-
|
|
691
|
-
if (version === currentVersion) {
|
|
692
|
-
newSegments = baseSegments;
|
|
693
|
-
} else if (hasLocale) {
|
|
694
|
-
newSegments = [ baseSegments[0], version, ...baseSegments.slice(1) ];
|
|
695
|
-
} else {
|
|
696
|
-
newSegments = [ version, ...baseSegments ];
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
const path = `/${ newSegments.join('/') }`;
|
|
700
|
-
|
|
701
|
-
return routeExists(version, path) ? path : versionHome(version);
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
/**
|
|
705
|
-
* Builds the menu entry that links to a version.
|
|
706
|
-
*
|
|
707
|
-
* @param version - The version label that the entry points at
|
|
708
|
-
* @returns A {@link VersionMenuItemInterface} carrying the label and the path of that version
|
|
709
|
-
*
|
|
710
|
-
* @remarks
|
|
711
|
-
* The template calls this for every entry it renders.
|
|
712
|
-
*
|
|
713
|
-
* @since 2.1.0
|
|
714
|
-
*/
|
|
715
|
-
|
|
716
438
|
function createVersionMenuItem(version: string): VersionMenuItemInterface {
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
};
|
|
721
|
-
}
|
|
439
|
+
const index = versionIndex.value;
|
|
440
|
+
const page = [ ...segments.value ];
|
|
441
|
+
page.splice(index, versionSet.value.has(page[index]) ? 1 : 0, version);
|
|
722
442
|
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
* Only the mobile layout calls this.
|
|
728
|
-
*
|
|
729
|
-
* @since 2.1.0
|
|
730
|
-
*/
|
|
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 }` };
|
|
731
447
|
|
|
732
|
-
|
|
733
|
-
isOpen.value = !isOpen.value;
|
|
448
|
+
return { text: versionText(version), link: routes.includes(home) ? `/${ home }` : `/${ version }/` };
|
|
734
449
|
}
|
|
735
450
|
|
|
736
451
|
return {
|
|
737
452
|
isOpen,
|
|
738
|
-
|
|
739
|
-
hasVersions,
|
|
453
|
+
versions: computed(() => plugin.value.versions),
|
|
454
|
+
hasVersions: computed(() => versionSet.value.size > 1),
|
|
740
455
|
activeVersion,
|
|
741
|
-
activeVersionText,
|
|
742
|
-
availableVersions,
|
|
456
|
+
activeVersionText: computed(() => versionText(activeVersion.value)),
|
|
457
|
+
availableVersions: computed(() => plugin.value.versions.filter((version: string) => version !== activeVersion.value)),
|
|
743
458
|
createVersionMenuItem,
|
|
744
|
-
|
|
459
|
+
toggle: () => (isOpen.value = !isOpen.value)
|
|
745
460
|
};
|
|
746
461
|
}
|