@viteplus/versions 2.0.9 → 2.1.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.
@@ -0,0 +1,662 @@
1
+ /**
2
+ * Type-only imports erased during TypeScript compilation.
3
+ */
4
+
5
+ import type { Ref, ComputedRef } from 'vue';
6
+
7
+ /**
8
+ * Imports
9
+ */
10
+
11
+ import { computed, ref } from 'vue';
12
+ import { useData, useRouter } from 'vitepress';
13
+
14
+ /**
15
+ * The versioning state that the plugin hands to the theme.
16
+ *
17
+ * @remarks
18
+ * The plugin builds this object at configuration time and injects it into the theme layout,
19
+ * so the switcher reads the published versions from its prop instead of the site configuration.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * const versioningPlugin: VersioningPluginInterface = {
24
+ * routes: { 'v1.0.x': [ 'v1.0.x/guide/index.md' ] },
25
+ * versions: new Set([ 'v1.0.x' ]),
26
+ * currentVersion: 'v2.0.x'
27
+ * };
28
+ * ```
29
+ *
30
+ * @since 2.1.0
31
+ */
32
+
33
+ export interface VersioningPluginInterface {
34
+ /**
35
+ * The routes that every version publishes, keyed by version label.
36
+ *
37
+ * @remarks
38
+ * A route is written exactly as VitePress serves it,
39
+ * so it keeps its version prefix and the `.md` extension of its source file.
40
+ * An older plugin ships no such record, and the switcher then trusts every path it builds.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const { routes } = versioningPlugin;
45
+ * routes; // { 'v1.0.x': [ 'v1.0.x/guide/index.md' ] }
46
+ * ```
47
+ *
48
+ * @since 2.1.0
49
+ */
50
+
51
+ routes?: Record<string, Array<string>>;
52
+
53
+ /**
54
+ * The archived versions that the site publishes.
55
+ *
56
+ * @remarks
57
+ * The current version is not a member of this set,
58
+ * so the switcher adds it to the menu as a separate entry.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const { versions } = versioningPlugin;
63
+ * versions; // Set { 'v1.0.x', 'v0.9.x' }
64
+ * ```
65
+ *
66
+ * @since 2.1.0
67
+ */
68
+
69
+ versions: Set<string>;
70
+
71
+ /**
72
+ * The label of the version that the site serves at its root.
73
+ *
74
+ * @remarks
75
+ * A path built for this version carries no version segment at all,
76
+ * which is why the switcher strips every version segment before it rebuilds a path.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * const { currentVersion } = versioningPlugin;
81
+ * currentVersion; // 'v2.0.x'
82
+ * ```
83
+ *
84
+ * @since 2.1.0
85
+ */
86
+
87
+ currentVersion: string;
88
+ }
89
+
90
+ /**
91
+ * The props that the theme layout passes to the switcher.
92
+ *
93
+ * @remarks
94
+ * One component serves both layouts, and `screenMenu` is what tells the two apart.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * const props: PropsInterface = { versioningPlugin, screenMenu: true };
99
+ * ```
100
+ *
101
+ * @since 2.1.0
102
+ */
103
+
104
+ export interface PropsInterface {
105
+ /**
106
+ * The versioning state that the plugin injected into the theme.
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * const { versioningPlugin } = props;
111
+ * versioningPlugin; // { versions: Set { 'v1.0.x' }, currentVersion: 'v2.0.x' }
112
+ * ```
113
+ *
114
+ * @see VersioningPluginInterface
115
+ * @since 2.1.0
116
+ */
117
+
118
+ versioningPlugin: VersioningPluginInterface;
119
+
120
+ /**
121
+ * Whether the switcher renders as the mobile dropdown.
122
+ *
123
+ * @remarks
124
+ * When `true`, the component renders a collapsible group for the mobile navigation screen.
125
+ * When `false` or omitted, it renders the flyout of the desktop navigation bar.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * const { screenMenu } = props;
130
+ * screenMenu; // true inside the mobile navigation screen
131
+ * ```
132
+ *
133
+ * @since 2.1.0
134
+ */
135
+
136
+ screenMenu?: boolean;
137
+ }
138
+
139
+ /**
140
+ * The shape of a single entry of the version menu.
141
+ *
142
+ * @remarks
143
+ * `VPMenuLink` of the default theme consumes this shape as it is,
144
+ * so an entry needs no further mapping before it renders.
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * const item: VersionMenuItemInterface = { text: 'v1.0.x', link: '/v1.0.x/guide/index.md' };
149
+ * ```
150
+ *
151
+ * @since 2.1.0
152
+ */
153
+
154
+ export interface VersionMenuItemInterface {
155
+ /**
156
+ * The label that the entry shows.
157
+ *
158
+ * @remarks
159
+ * The version label is used verbatim, so the menu reads the same as the archive folders.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * const { text } = item;
164
+ * text; // 'v1.0.x'
165
+ * ```
166
+ *
167
+ * @since 2.1.0
168
+ */
169
+
170
+ text: string;
171
+
172
+ /**
173
+ * The path that the entry points at.
174
+ *
175
+ * @example
176
+ * ```ts
177
+ * const { link } = item;
178
+ * link; // '/v1.0.x/guide/index.md'
179
+ * ```
180
+ *
181
+ * @since 2.1.0
182
+ */
183
+
184
+ link: string;
185
+ }
186
+
187
+ /**
188
+ * The bindings that {@link useVersionSwitcher} exposes to the template.
189
+ *
190
+ * @remarks
191
+ * Only what the template renders is exposed.
192
+ * Every path helper stays inside the composable, since each one reads the props and the route.
193
+ *
194
+ * @example
195
+ * ```ts
196
+ * const { hasVersions, toggle } = useVersionSwitcher(props);
197
+ * ```
198
+ *
199
+ * @since 2.1.0
200
+ */
201
+
202
+ export interface VersionSwitcherInterface {
203
+ /**
204
+ * Whether the mobile dropdown is open.
205
+ *
206
+ * @remarks
207
+ * The desktop flyout keeps its own open state, so this ref drives the mobile layout alone.
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * isOpen.value; // false while the dropdown is collapsed
212
+ * ```
213
+ *
214
+ * @since 2.1.0
215
+ */
216
+
217
+ isOpen: Ref<boolean>;
218
+
219
+ /**
220
+ * Whether the site publishes any archived version.
221
+ *
222
+ * @remarks
223
+ * The component renders nothing while the set is empty,
224
+ * so a site with no archive keeps its navigation bar unchanged.
225
+ *
226
+ * @example
227
+ * ```ts
228
+ * hasVersions.value; // false on a site with no archive
229
+ * ```
230
+ *
231
+ * @since 2.1.0
232
+ */
233
+
234
+ hasVersions: ComputedRef<boolean>;
235
+
236
+ /**
237
+ * The version label that the current page belongs to.
238
+ *
239
+ * @remarks
240
+ * The version sits in the first segment, or in the second one when a locale leads the path.
241
+ * A page with no version segment belongs to the current version.
242
+ *
243
+ * @example
244
+ * ```ts
245
+ * // on /de/v1.0.x/guide/index.md
246
+ * activeVersion.value; // 'v1.0.x'
247
+ * ```
248
+ *
249
+ * @since 2.1.0
250
+ */
251
+
252
+ activeVersion: ComputedRef<string>;
253
+
254
+ /**
255
+ * The archived versions that the reader can switch to from the current page.
256
+ *
257
+ * @remarks
258
+ * The active version is left out, so the menu never links to the page the reader already reads.
259
+ *
260
+ * @example
261
+ * ```ts
262
+ * // on /de/v1.0.x/guide/index.md
263
+ * availableVersions.value; // [ 'v0.9.x' ]
264
+ * ```
265
+ *
266
+ * @since 2.1.0
267
+ */
268
+
269
+ availableVersions: ComputedRef<Array<string>>;
270
+
271
+ /**
272
+ * Whether the desktop menu needs an entry for the current version.
273
+ *
274
+ * @remarks
275
+ * The entry appears only while the reader is on an archived page,
276
+ * since the current version is the active one everywhere else.
277
+ *
278
+ * @example
279
+ * ```ts
280
+ * shouldShowCurrentVersion.value; // true on an archived page
281
+ * ```
282
+ *
283
+ * @since 2.1.0
284
+ */
285
+
286
+ shouldShowCurrentVersion: ComputedRef<boolean>;
287
+
288
+ /**
289
+ * Builds the menu entry that links to a version.
290
+ *
291
+ * @param version - The version label that the entry points at
292
+ * @returns A {@link VersionMenuItemInterface} carrying the label and the path of that version
293
+ *
294
+ * @remarks
295
+ * The template calls this for every entry it renders,
296
+ * so an entry is built at render time and always points at the page the reader is on.
297
+ *
298
+ * @example
299
+ * ```ts
300
+ * // on /de/guide/index.md
301
+ * createVersionMenuItem('v1.0.x');
302
+ * // { text: 'v1.0.x', link: '/de/v1.0.x/guide/index.md' }
303
+ * ```
304
+ *
305
+ * @since 2.1.0
306
+ */
307
+
308
+ createVersionMenuItem(version: string): VersionMenuItemInterface;
309
+
310
+ /**
311
+ * Opens or closes the mobile dropdown.
312
+ *
313
+ * @remarks
314
+ * Only the mobile layout calls this, since the desktop flyout keeps its own open state.
315
+ *
316
+ * @example
317
+ * ```ts
318
+ * isOpen.value; // false
319
+ * toggle();
320
+ * isOpen.value; // true
321
+ * ```
322
+ *
323
+ * @since 2.1.0
324
+ */
325
+
326
+ toggle(): void;
327
+ }
328
+
329
+ /**
330
+ * Builds the reactive state and the handlers of the version switcher.
331
+ *
332
+ * @param props - The props that the theme layout passed to the component
333
+ * @returns The bindings that the template renders {@link VersionSwitcherInterface}
334
+ *
335
+ * @remarks
336
+ * The composable reads the router and the site data of the running site,
337
+ * so the setup function of the component is the only place that may call it.
338
+ * Every menu entry is rebuilt on render, which keeps the links pointed at the page the reader is on
339
+ * as the reader moves through the site.
340
+ *
341
+ * @example
342
+ * ```ts
343
+ * const props = defineProps<PropsInterface>();
344
+ * const { hasVersions, activeVersion, toggle } = useVersionSwitcher(props);
345
+ * ```
346
+ *
347
+ * @see VersionSwitcherInterface
348
+ * @since 2.1.0
349
+ */
350
+
351
+ export function useVersionSwitcher(props: PropsInterface): VersionSwitcherInterface {
352
+ const router = useRouter();
353
+ const { site } = useData();
354
+ const isOpen = ref(false);
355
+
356
+ /**
357
+ * The archived version labels as a set.
358
+ *
359
+ * @remarks
360
+ * Every segment of every path the switcher handles is tested against this set
361
+ * to tell a version apart from an ordinary segment.
362
+ *
363
+ * @example
364
+ * ```ts
365
+ * const versions = versionSet.value;
366
+ * versions.has('v1.0.x'); // true
367
+ * versions.has('guide'); // false
368
+ * ```
369
+ *
370
+ * @since 2.1.0
371
+ */
372
+
373
+ const versionSet = computed(() => new Set([ ...props.versioningPlugin.versions ]));
374
+
375
+ /**
376
+ * Whether the site publishes any archived version.
377
+ *
378
+ * @remarks
379
+ * The component renders nothing while the set is empty.
380
+ *
381
+ * @since 2.1.0
382
+ */
383
+
384
+ const hasVersions = computed(() => versionSet.value.size > 0);
385
+
386
+ /**
387
+ * The prefix of the active locale, without its slashes.
388
+ *
389
+ * @remarks
390
+ * The root locale has no prefix of its own, so this value is an empty string there
391
+ * and no path the switcher builds gains a locale segment.
392
+ *
393
+ * @example
394
+ * ```ts
395
+ * currentLocale.value; // 'de'
396
+ * ```
397
+ *
398
+ * @since 2.1.0
399
+ */
400
+
401
+ const currentLocale = computed(() => {
402
+ const { locales, localeIndex } = site.value;
403
+
404
+ return locales[localeIndex ?? '']?.link?.replace(/\//g, '') || '';
405
+ });
406
+
407
+ /**
408
+ * The relative path of the current page, split on the slash.
409
+ *
410
+ * @remarks
411
+ * VitePress reports the source path here, so the last segment still carries its `.md` extension,
412
+ * and a route of {@link VersioningPluginInterface.routes} matches it as written.
413
+ *
414
+ * @example
415
+ * ```ts
416
+ * pathSegments.value; // [ 'de', 'v1.0.x', 'guide', 'index.md' ]
417
+ * ```
418
+ *
419
+ * @since 2.1.0
420
+ */
421
+
422
+ const pathSegments = computed(() => router.route.data.relativePath.split('/'));
423
+
424
+ /**
425
+ * The version label that the current page belongs to.
426
+ *
427
+ * @remarks
428
+ * The version sits in the first segment, or in the second one when a locale leads the path.
429
+ * A page with no version segment belongs to the current version.
430
+ *
431
+ * @since 2.1.0
432
+ */
433
+
434
+ const activeVersion = computed(() => {
435
+ const { currentVersion } = props.versioningPlugin;
436
+ const segments = pathSegments.value;
437
+ const locale = currentLocale.value;
438
+
439
+ // Determine which segment might be the version
440
+ const versionCandidate = segments[0] === locale ? segments[1] : segments[0];
441
+
442
+ return versionSet.value.has(versionCandidate) ? versionCandidate : currentVersion;
443
+ });
444
+
445
+ /**
446
+ * The archived versions that the reader can switch to from the current page.
447
+ *
448
+ * @remarks
449
+ * The active version is left out, so the menu never links to the page the reader already reads.
450
+ *
451
+ * @since 2.1.0
452
+ */
453
+
454
+ const availableVersions = computed(() =>
455
+ Array.from(props.versioningPlugin.versions).filter(v => v !== activeVersion.value)
456
+ );
457
+
458
+ /**
459
+ * Whether the desktop menu needs an entry for the current version.
460
+ *
461
+ * @remarks
462
+ * The entry appears only while the reader is on an archived page.
463
+ *
464
+ * @since 2.1.0
465
+ */
466
+
467
+ const shouldShowCurrentVersion = computed(() =>
468
+ activeVersion.value !== props.versioningPlugin.currentVersion
469
+ );
470
+
471
+ /**
472
+ * Tells whether the leading segment of a path is the active locale.
473
+ *
474
+ * @param segments - The path segments to inspect
475
+ * @returns `true` when the first segment matches the active locale
476
+ *
477
+ * @remarks
478
+ * The root locale resolves to an empty prefix, which matches no segment,
479
+ * so a path under the root locale always reports `false`.
480
+ *
481
+ * @example
482
+ * ```ts
483
+ * isLocaleFirst([ 'de', 'guide', 'index.md' ]); // true
484
+ * isLocaleFirst([ 'guide', 'index.md' ]); // false
485
+ * ```
486
+ *
487
+ * @since 2.1.0
488
+ */
489
+
490
+ function isLocaleFirst(segments: Array<string>): boolean {
491
+ return segments[0] === currentLocale.value;
492
+ }
493
+
494
+ /**
495
+ * Strips every version segment out of a path.
496
+ *
497
+ * @param segments - The path segments to filter
498
+ * @returns The segments that name no version
499
+ *
500
+ * @remarks
501
+ * The result is the version-free form of the path,
502
+ * which the switcher then rebuilds for the version the reader picked.
503
+ *
504
+ * @example
505
+ * ```ts
506
+ * removeVersionSegments([ 'de', 'v1.0.x', 'guide', 'index.md' ]);
507
+ * // [ 'de', 'guide', 'index.md' ]
508
+ * ```
509
+ *
510
+ * @since 2.1.0
511
+ */
512
+
513
+ function removeVersionSegments(segments: Array<string>): Array<string> {
514
+ return segments.filter(seg => !versionSet.value.has(seg));
515
+ }
516
+
517
+ /**
518
+ * Tells whether a version publishes the given path.
519
+ *
520
+ * @param version - The version label to look the path up in
521
+ * @param path - The path to check, with its leading slash
522
+ * @returns `true` when the version publishes the path, or when the plugin lists no routes
523
+ *
524
+ * @remarks
525
+ * The plugin lists the routes of every version, so the switcher can check a link before it renders.
526
+ * An older plugin, or the component used on its own, ships no such list,
527
+ * and the check then passes for every path.
528
+ *
529
+ * @example
530
+ * ```ts
531
+ * routeExists('v1.0.x', '/v1.0.x/guide/index.md'); // true
532
+ * routeExists('v1.0.x', '/v1.0.x/api/index.md'); // false
533
+ * ```
534
+ *
535
+ * @since 2.1.0
536
+ */
537
+
538
+ function routeExists(version: string, path: string): boolean {
539
+ const routes = props.versioningPlugin.routes?.[version];
540
+ if (!routes) return true;
541
+
542
+ return routes.includes(path.replace(/^\//, ''));
543
+ }
544
+
545
+ /**
546
+ * Builds the home path of a version.
547
+ *
548
+ * @param version - The version label to build the home path for
549
+ * @returns The path of that version's home, with a trailing slash
550
+ *
551
+ * @remarks
552
+ * The path keeps the locale of the current page and drops every other segment.
553
+ * The current version takes no version segment, so its home is the locale root.
554
+ *
555
+ * @example
556
+ * ```ts
557
+ * // on /de/v1.0.x/guide/index.md
558
+ * versionHome('v0.9.x'); // '/de/v0.9.x/'
559
+ * versionHome('v2.0.x'); // '/de/'
560
+ * ```
561
+ *
562
+ * @since 2.1.0
563
+ */
564
+
565
+ function versionHome(version: string): string {
566
+ const { currentVersion } = props.versioningPlugin;
567
+ const baseSegments = removeVersionSegments(pathSegments.value);
568
+ const locale = isLocaleFirst(baseSegments) ? baseSegments[0] : '';
569
+ const segments = [ locale, version === currentVersion ? '' : version ].filter(Boolean);
570
+
571
+ return segments.length ? `/${ segments.join('/') }/` : '/';
572
+ }
573
+
574
+ /**
575
+ * Builds the path of the current page in another version.
576
+ *
577
+ * @param version - The version label to move the current page to
578
+ * @returns The path of the page in that version, or that version's home when the page is missing there
579
+ *
580
+ * @remarks
581
+ * The switcher drops every version segment first,
582
+ * then puts the picked version behind the locale where the path carries one, and in front otherwise.
583
+ * The current version takes no segment at all, since the site serves it at its root.
584
+ *
585
+ * A reader who leaves a page that the other version never published would land on a dead link,
586
+ * so {@link routeExists} checks the path and {@link versionHome} takes over when the check fails.
587
+ *
588
+ * @example
589
+ * ```ts
590
+ * // on /de/v1.0.x/guide/index.md
591
+ * buildVersionPath('v2.0.x'); // '/de/guide/index.md'
592
+ * buildVersionPath('v0.9.x'); // '/de/v0.9.x/' when v0.9.x has no guide page
593
+ * ```
594
+ *
595
+ * @see routeExists
596
+ * @see versionHome
597
+ *
598
+ * @since 2.1.0
599
+ */
600
+
601
+ function buildVersionPath(version: string): string {
602
+ const { currentVersion } = props.versioningPlugin;
603
+ const baseSegments = removeVersionSegments(pathSegments.value);
604
+ const hasLocale = isLocaleFirst(baseSegments);
605
+
606
+ let newSegments: Array<string>;
607
+
608
+ if (version === currentVersion) {
609
+ newSegments = baseSegments;
610
+ } else if (hasLocale) {
611
+ newSegments = [ baseSegments[0], version, ...baseSegments.slice(1) ];
612
+ } else {
613
+ newSegments = [ version, ...baseSegments ];
614
+ }
615
+
616
+ const path = `/${ newSegments.join('/') }`;
617
+
618
+ return routeExists(version, path) ? path : versionHome(version);
619
+ }
620
+
621
+ /**
622
+ * Builds the menu entry that links to a version.
623
+ *
624
+ * @param version - The version label that the entry points at
625
+ * @returns A {@link VersionMenuItemInterface} carrying the label and the path of that version
626
+ *
627
+ * @remarks
628
+ * The template calls this for every entry it renders.
629
+ *
630
+ * @since 2.1.0
631
+ */
632
+
633
+ function createVersionMenuItem(version: string): VersionMenuItemInterface {
634
+ return {
635
+ text: version,
636
+ link: buildVersionPath(version)
637
+ };
638
+ }
639
+
640
+ /**
641
+ * Opens or closes the mobile dropdown.
642
+ *
643
+ * @remarks
644
+ * Only the mobile layout calls this.
645
+ *
646
+ * @since 2.1.0
647
+ */
648
+
649
+ function toggle(): void {
650
+ isOpen.value = !isOpen.value;
651
+ }
652
+
653
+ return {
654
+ isOpen,
655
+ toggle,
656
+ hasVersions,
657
+ activeVersion,
658
+ availableVersions,
659
+ createVersionMenuItem,
660
+ shouldShowCurrentVersion
661
+ };
662
+ }