@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
|
@@ -33,11 +33,11 @@ const props = defineProps<PropsInterface>();
|
|
|
33
33
|
const {
|
|
34
34
|
isOpen,
|
|
35
35
|
toggle,
|
|
36
|
+
versions,
|
|
36
37
|
hasVersions,
|
|
37
38
|
activeVersionText,
|
|
38
39
|
availableVersions,
|
|
39
|
-
createVersionMenuItem
|
|
40
|
-
shouldShowCurrentVersion
|
|
40
|
+
createVersionMenuItem
|
|
41
41
|
} = useVersionSwitcher(props);
|
|
42
42
|
</script>
|
|
43
43
|
|
|
@@ -49,13 +49,9 @@ const {
|
|
|
49
49
|
class="VPVersionSwitcher"
|
|
50
50
|
icon="vpi-versioning"
|
|
51
51
|
:button="activeVersionText"
|
|
52
|
-
label="Switch Version"
|
|
52
|
+
:label="label ?? 'Switch Version'"
|
|
53
53
|
>
|
|
54
54
|
<ul class="items">
|
|
55
|
-
<VPMenuLink
|
|
56
|
-
v-if="shouldShowCurrentVersion"
|
|
57
|
-
:item="createVersionMenuItem(versioningPlugin.currentVersion)"
|
|
58
|
-
/>
|
|
59
55
|
<VPMenuLink
|
|
60
56
|
v-for="version in availableVersions"
|
|
61
57
|
:key="version"
|
|
@@ -75,15 +71,14 @@ const {
|
|
|
75
71
|
>
|
|
76
72
|
<span class="button-text">
|
|
77
73
|
<span class="vpi-versioning icon" />
|
|
78
|
-
Switch Version
|
|
74
|
+
{{ label ?? 'Switch Version' }}
|
|
79
75
|
</span>
|
|
80
76
|
<span class="vpi-plus button-icon" />
|
|
81
77
|
</button>
|
|
82
78
|
|
|
83
79
|
<ul id="navbar-group-version" class="items">
|
|
84
|
-
<VPMenuLink :item="createVersionMenuItem(versioningPlugin.currentVersion)" />
|
|
85
80
|
<VPMenuLink
|
|
86
|
-
v-for="version in
|
|
81
|
+
v-for="version in versions"
|
|
87
82
|
:key="version"
|
|
88
83
|
:item="createVersionMenuItem(version)"
|
|
89
84
|
/>
|
package/dist/index.d.ts
CHANGED
|
@@ -434,12 +434,13 @@ type NavType = Array<NavItemType> | NavObjectType;
|
|
|
434
434
|
* @remarks
|
|
435
435
|
* The `rewritesHook` function allows you to programmatically rewrite
|
|
436
436
|
* paths or content based on the documentation version and locale being served.
|
|
437
|
+
* Its output has to start with `<locale>/<version>/`, or `<version>/` for the root locale.
|
|
437
438
|
*
|
|
438
439
|
* @example
|
|
439
440
|
* ```ts
|
|
440
441
|
* const hooks: VersionsHooksInterface = {
|
|
441
442
|
* rewritesHook: (source, version, locale) =>
|
|
442
|
-
*
|
|
443
|
+
* join(locale, version, source.replace(/^old-guide\//, 'guide/'))
|
|
443
444
|
* };
|
|
444
445
|
* ```
|
|
445
446
|
*
|
|
@@ -456,8 +457,10 @@ interface VersionsHooksInterface {
|
|
|
456
457
|
* @returns The rewritten path string, which can be used for routing or file organization.
|
|
457
458
|
*
|
|
458
459
|
* @remarks
|
|
459
|
-
* This hook is invoked whenever paths need to be version- and locale-aware
|
|
460
|
-
*
|
|
460
|
+
* This hook is invoked whenever paths need to be version- and locale-aware.
|
|
461
|
+
* It may reshape the source part freely, but the locale and the version stay the first segments,
|
|
462
|
+
* since the locale entries, the navigation, the switchers, and the root URL look for them there.
|
|
463
|
+
* A hook that moves them fails the build.
|
|
461
464
|
*
|
|
462
465
|
* @since 2.0.0
|
|
463
466
|
*/
|
|
@@ -501,11 +504,73 @@ interface VersionsSwitcherInterface {
|
|
|
501
504
|
*/
|
|
502
505
|
includeCurrentVersion: boolean;
|
|
503
506
|
}
|
|
507
|
+
/**
|
|
508
|
+
* The settings of a single documentation version.
|
|
509
|
+
*
|
|
510
|
+
* @remarks
|
|
511
|
+
* `versionsConfig.versions` keys these settings by version name,
|
|
512
|
+
* which is the `current` label for the sources and the folder name for an archived version.
|
|
513
|
+
* The name always stays the URL segment of the version.
|
|
514
|
+
*
|
|
515
|
+
* @example
|
|
516
|
+
* ```ts
|
|
517
|
+
* const versions: Record<string, VersionOptionsInterface> = {
|
|
518
|
+
* 'v1.x': { label: 'v1.x (LTS)' },
|
|
519
|
+
* 'v0.x': { path: 'legacy/v0' }
|
|
520
|
+
* };
|
|
521
|
+
* ```
|
|
522
|
+
*
|
|
523
|
+
* @see https://github.com/viteplus/versions/issues/55
|
|
524
|
+
* @since 3.0.0
|
|
525
|
+
*/
|
|
526
|
+
interface VersionOptionsInterface {
|
|
527
|
+
/**
|
|
528
|
+
* The text that the version switchers show for the version.
|
|
529
|
+
*
|
|
530
|
+
* @remarks
|
|
531
|
+
* When omitted, the switchers show the version name.
|
|
532
|
+
* The URL keeps the version name either way, so a label can change without moving any page.
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* ```ts
|
|
536
|
+
* const options: VersionOptionsInterface = { label: 'v1.x (LTS)' };
|
|
537
|
+
* // the switcher reads 'v1.x (LTS)', and the pages stay under /v1.x/
|
|
538
|
+
* ```
|
|
539
|
+
*
|
|
540
|
+
* @since 3.0.0
|
|
541
|
+
*/
|
|
542
|
+
label: string;
|
|
543
|
+
/**
|
|
544
|
+
* The folder that holds the pages of the version, relative to the documentation root.
|
|
545
|
+
*
|
|
546
|
+
* @remarks
|
|
547
|
+
* Set it to publish a version whose pages live outside the archive folder,
|
|
548
|
+
* such as a worktree of a release branch checked out inside the documentation root.
|
|
549
|
+
* The version joins the published versions under its name, so its pages are served at `/<name>/`.
|
|
550
|
+
*
|
|
551
|
+
* VitePress builds only the files under its source directory,
|
|
552
|
+
* so the folder has to exist inside the documentation root, the folder that holds `sources` and `archive`.
|
|
553
|
+
* A name that the current version or an archive folder already carries fails the build,
|
|
554
|
+
* since both would publish under one URL.
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```ts
|
|
558
|
+
* const options: VersionOptionsInterface = { path: 'legacy/v0' };
|
|
559
|
+
* // docs/legacy/v0/guide/index.md is served at /v0.x/guide/
|
|
560
|
+
* ```
|
|
561
|
+
*
|
|
562
|
+
* @see https://github.com/viteplus/versions/issues/55
|
|
563
|
+
* @since 3.0.0
|
|
564
|
+
*/
|
|
565
|
+
path: string;
|
|
566
|
+
}
|
|
504
567
|
/**
|
|
505
568
|
* Configuration for documentation versions.
|
|
506
569
|
*
|
|
507
570
|
* @remarks
|
|
508
|
-
* - `current` specifies the
|
|
571
|
+
* - `current` specifies the label of the version in `sources`.
|
|
572
|
+
* - `latest` specifies the stable version, which is served at the root of the site.
|
|
573
|
+
* - `versions` holds per-version settings such as the switcher label.
|
|
509
574
|
* - `sources` is the folder or path where current versioned sources reside.
|
|
510
575
|
* - `archive` is the folder or path for archived versions.
|
|
511
576
|
* - `hooks` allows for custom rewriting logic per version.
|
|
@@ -514,12 +579,14 @@ interface VersionsSwitcherInterface {
|
|
|
514
579
|
* @example
|
|
515
580
|
* ```ts
|
|
516
581
|
* const versionsConfig: VersionsConfigInterface = {
|
|
517
|
-
* current: "
|
|
582
|
+
* current: "next",
|
|
583
|
+
* latest: "2.0",
|
|
518
584
|
* sources: "src",
|
|
519
585
|
* archive: "archive",
|
|
520
586
|
* hooks: {
|
|
521
|
-
* rewritesHook: (source, version) =>
|
|
587
|
+
* rewritesHook: (source, version, locale) => join(locale, version, source.toLowerCase())
|
|
522
588
|
* },
|
|
589
|
+
* versions: { "1.0": { label: "1.0 (LTS)" } },
|
|
523
590
|
* versionSwitcher: { text: "Version", includeCurrentVersion: true }
|
|
524
591
|
* };
|
|
525
592
|
* ```
|
|
@@ -528,15 +595,43 @@ interface VersionsSwitcherInterface {
|
|
|
528
595
|
*/
|
|
529
596
|
interface VersionsConfigInterface {
|
|
530
597
|
/**
|
|
531
|
-
* The
|
|
598
|
+
* The label of the version that the `sources` folder holds.
|
|
532
599
|
*
|
|
533
600
|
* @remarks
|
|
534
|
-
*
|
|
535
|
-
*
|
|
601
|
+
* It is the stable version, served at the root, unless {@link VersionsConfigInterface.latest} names another one.
|
|
602
|
+
* Otherwise, the pages of the sources publish under this label, like the pages of an archived version.
|
|
603
|
+
*
|
|
604
|
+
* @example
|
|
605
|
+
* ```ts
|
|
606
|
+
* const versionsConfig = { current: 'next', latest: 'v2.0.x' };
|
|
607
|
+
* // src/guide/index.md is served at /next/guide/
|
|
608
|
+
* ```
|
|
536
609
|
*
|
|
537
610
|
* @since 2.0.0
|
|
538
611
|
*/
|
|
539
612
|
current: string;
|
|
613
|
+
/**
|
|
614
|
+
* The name of the stable version.
|
|
615
|
+
*
|
|
616
|
+
* @remarks
|
|
617
|
+
* Its pages are served at the root of the site without a version segment,
|
|
618
|
+
* and the version switchers tag it as the latest one.
|
|
619
|
+
* Every other version is served under its own name.
|
|
620
|
+
* Set it to an archived version while the sources hold documentation for an unreleased release.
|
|
621
|
+
* When empty, the stable version is {@link VersionsConfigInterface.current}.
|
|
622
|
+
*
|
|
623
|
+
* The name has to be `current` or an archive folder, or the configuration fails to load.
|
|
624
|
+
*
|
|
625
|
+
* @example
|
|
626
|
+
* ```ts
|
|
627
|
+
* const versionsConfig = { current: 'next', latest: 'v2.x' };
|
|
628
|
+
* // archive/v2.x/guide.md is served at /guide, and src/guide.md at /next/guide
|
|
629
|
+
* ```
|
|
630
|
+
*
|
|
631
|
+
* @see https://github.com/viteplus/versions/issues/55
|
|
632
|
+
* @since 3.0.0
|
|
633
|
+
*/
|
|
634
|
+
latest: string;
|
|
540
635
|
/**
|
|
541
636
|
* The folder or path where the current versioned sources reside.
|
|
542
637
|
*
|
|
@@ -571,6 +666,25 @@ interface VersionsConfigInterface {
|
|
|
571
666
|
* @since 2.0.0
|
|
572
667
|
*/
|
|
573
668
|
versionSwitcher: false | VersionsSwitcherInterface;
|
|
669
|
+
/**
|
|
670
|
+
* The per-version settings, keyed by version name.
|
|
671
|
+
*
|
|
672
|
+
* @remarks
|
|
673
|
+
* A key has to be `current`, an archive folder, or a version that carries its own `path`,
|
|
674
|
+
* or the configuration fails to load, so a misspelled name never goes unnoticed.
|
|
675
|
+
* A version without an entry keeps the defaults of {@link VersionOptionsInterface}.
|
|
676
|
+
*
|
|
677
|
+
* @example
|
|
678
|
+
* ```ts
|
|
679
|
+
* const versionsConfig = { versions: { 'v1.x': { label: 'v1.x (LTS)' } } };
|
|
680
|
+
* ```
|
|
681
|
+
*
|
|
682
|
+
* @see VersionOptionsInterface
|
|
683
|
+
* @see https://github.com/viteplus/versions/issues/55
|
|
684
|
+
*
|
|
685
|
+
* @since 3.0.0
|
|
686
|
+
*/
|
|
687
|
+
versions: Record<string, VersionOptionsInterface>;
|
|
574
688
|
}
|
|
575
689
|
/**
|
|
576
690
|
* Main configuration interface for a VitePress site with extended features.
|
|
@@ -595,7 +709,7 @@ interface VersionsConfigInterface {
|
|
|
595
709
|
* current: "2.0",
|
|
596
710
|
* sources: "src",
|
|
597
711
|
* archive: "archive",
|
|
598
|
-
* hooks: { rewritesHook: (src, ver) =>
|
|
712
|
+
* hooks: { rewritesHook: (src, ver, locale) => join(locale, ver, src.toLowerCase()) },
|
|
599
713
|
* versionSwitcher: { text: "Version", includeCurrentVersion: true }
|
|
600
714
|
* }
|
|
601
715
|
* };
|
|
@@ -879,7 +993,7 @@ type RequireKeysType<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
|
|
|
879
993
|
* @example
|
|
880
994
|
* ```ts
|
|
881
995
|
* const resolve: RouteResolverType = createRouteResolver();
|
|
882
|
-
* resolve('src/guide/index.md'); // 'guide/index.md'
|
|
996
|
+
* resolve('src/guide/index.md'); // 'v2.0.x/guide/index.md'
|
|
883
997
|
* resolve('archive/v1.0.x/guide/index.md'); // 'v1.0.x/guide/index.md'
|
|
884
998
|
* ```
|
|
885
999
|
*
|
|
@@ -906,6 +1020,7 @@ export {
|
|
|
906
1020
|
SidebarObjectType,
|
|
907
1021
|
SidebarType,
|
|
908
1022
|
ThemeInterface,
|
|
1023
|
+
VersionOptionsInterface,
|
|
909
1024
|
VersionsConfigInterface,
|
|
910
1025
|
VersionsHooksInterface,
|
|
911
1026
|
VersionsSwitcherInterface,
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
`),new Error(
|
|
1
|
+
var A=Object.defineProperty;var Q=Object.getOwnPropertyDescriptor;var a=(e,t)=>A(e,"name",{value:t,configurable:!0});var O=(e,t,i,o)=>{for(var r=o>1?void 0:o?Q(t,i):t,n=e.length-1,c;n>=0;n--)(c=e[n])&&(r=(o?c(t,i,r):c(r))||r);return o&&r&&A(t,i,r),r};import{Injectable as pe}from"@remotex-labs/xinject";import{xterm as ge}from"@remotex-labs/xansi/xterm.component";import{join as w}from"path/posix";import{inject as Y}from"@remotex-labs/xinject";function $(e){return e.split(/[-_]/)[0]}a($,"getLanguageOnly");function N(e={}){let t={...e};return t.themeConfig&&(t.themeConfig={...t.themeConfig},delete t.themeConfig.nav,delete t.themeConfig.sidebar),t}a(N,"createSanitizedLocale");function R(e){return e?`/${e}/`:"/"}a(R,"generateLocaleLink");function Z(e,t){return{...N(e),label:" ",link:R(t)}}a(Z,"createVersionedLocale");function V(){let{latest:e,versionsList:t,vitepressConfig:{locales:i,base:o="/"},configuration:r,localePrefixes:n}=Y(g),c=a((s,f,l)=>r.themeConfig?.logoLink||s.themeConfig?.logoLink?s:{...s,themeConfig:{...s.themeConfig,logoLink:w("/",o,f,l,"/")}},"withLogoLink");i.root={};for(let[s,f]of Object.entries(n)){let l=N(r.locales[s]);for(let p of t)i[w(f,p)]=c(Z(l,f),f,p);i[f||"root"]=c({...l,link:R(f)},f,e)}i.root=c(i.root,"",e);for(let s of t)i[s]??=c({},"",s)}a(V,"parseLocale");import{existsSync as L,mkdirSync as me,readdirSync as he,writeFileSync as ue}from"fs";import{isAbsolute as de,join as d,posix as ve,relative as W,resolve as D,sep as F}from"path";import{existsSync as ae,readFileSync as ce,realpathSync as fe}from"fs";import{join as S}from"path/posix";import{join as oe}from"path";import{inject as y}from"@remotex-labs/xinject";import{readdirSync as ee}from"fs";import{join as te,relative as re}from"path/posix";function x(e,t){let i={...e};for(let o in t)if(Object.prototype.hasOwnProperty.call(t,o)){let r=t[o],n=e[o];n&&typeof n=="object"&&r&&typeof r=="object"&&!Array.isArray(n)&&!Array.isArray(r)?i[o]=x(n,r):i[o]=r}return i}a(x,"deepMerge");function j(e,t=e){let i=[],o=ee(t,{withFileTypes:!0});for(let r of o){let n=te(t,r.name);if(r.isDirectory())i=i.concat(j(e,n));else if(r.isFile()&&r.name.endsWith(".md")){let c=re(e,n);i.push(c)}}return i}a(j,"getAllMarkdownFilesRelative");function P(e,t,i){return S(i,t,e)}a(P,"rewritesHook");function ie(){let e=y(g),{localesMap:t,versionsConfig:i}=e,o=Object.keys(t),r=Object.entries(e.versionDirs);return function(n){let c=r.find(([,h])=>n.startsWith(`${h}/`));if(!c)return n;let[s,f]=c,l=n.slice(f.length+1),p=o.find(h=>l.startsWith(`${h}/`));if(!p)return i.hooks.rewritesHook(l,s,"");let m=t[p]==="root"?"":p;return i.hooks.rewritesHook(l.slice(p.length+1),s,m)}}a(ie,"createRouteResolver");function ne(e){let{root:t,versionDirs:i}=y(g),o=a(r=>j(oe(t,r)).map(n=>e(S(r,n))),"routes");return Object.fromEntries(Object.entries(i).map(([r,n])=>[r,o(n)]))}a(ne,"parseVersionRoutes");function se(){let e=y(g),{versionsList:t,localeSegments:i,versionsConfig:o}=e;for(let r of["",...i])for(let n of t){let c=`${S(r,n)}/`,s=o.hooks.rewritesHook("index.md",n,r);s.startsWith(c)||e.fail(`rewritesHook has to keep ${c} at the front of every route, it returned ${s}`)}}a(se,"assertRewritesHook");function M(){let e=y(g);se();let t=ie();e.vitepressConfig.rewrites=t,Object.assign(e.routesMap,ne(t))}a(M,"parseRoutesComponent");var le={fileExists:a(e=>ae(e),"fileExists"),readFile:a(e=>ce(e,"utf8"),"readFile"),realpath:a(e=>fe(e),"realpath")},b={locales:{},rewrites:{},versionsConfig:{current:"latest",latest:"",sources:"src",archive:"archive",hooks:{rewritesHook:P},versionSwitcher:{text:"Switch Version",includeCurrentVersion:!0},versions:{}},vue:{script:{fs:le}},vite:{resolve:{dedupe:["vue","vitepress"]}}};var g=class{constructor(t=b){this.configuration=t;let{sources:i,archive:o}=t.versionsConfig;this.root=d(process.cwd(),process.argv[3]??"",t.srcDir??""),this.sources=W(this.root,D(this.root,i)).split(F).join("/"),this.archive=o,this.versionsConfig=t.versionsConfig,this.sourcesPath=d(this.root,this.sources),this.archivePath=d(this.root,this.archive),L(this.sourcesPath)||this.fail(`Source directory ${this.sourcesPath} does not exist`),this.makeArchiveDirectory(this.archivePath),this.vitepressConfig=this.createVitepressConfig()}configuration;root;sources;archive;sourcesPath;archivePath;versionsConfig;vitepressConfig;get versionsList(){return Object.keys(this.versionDirs)}versionDirs={};latest="";labels={};localesMap={};localePrefixes={};get localeSegments(){return Object.values(this.localePrefixes).filter(Boolean)}routesMap={};init(){let{current:t,latest:i,versions:o}=this.versionsConfig;this.addVersion(t,this.sources);for(let r of he(this.archivePath,{withFileTypes:!0}))r.isDirectory()&&this.addVersion(r.name,ve.join(this.archive,r.name));for(let[r,{path:n}]of Object.entries(o))n&&this.addVersion(r,this.versionFolder(r,n));for(let r of[i,...Object.keys(o)])r&&!this.versionDirs[r]&&this.fail(`Version ${r} is neither the current version nor an archive folder`);this.latest=i||t;for(let r of this.versionsList)this.labels[r]=o[r]?.label??r;return this.initializeLocalesMap(),this}fail(t){throw console.error(ge.red("[ viteplus ]"),`${t}
|
|
2
|
+
`),new Error(t)}initializeLocalesMap(){let t=this.configuration.locales,i=Object.keys(t),o="root"in t?"root":i[0];for(let r of i){let{lang:n,link:c}=t[r],s=c?c.replace(/^\/+|\/+$/g,""):$(n??r),f=Object.keys(this.localePrefixes).find(l=>this.localePrefixes[l]===s);r!==o&&f&&this.fail(`Locales ${f} and ${r} share the prefix ${s}, give one a link of its own`),r!==o&&s.includes("/")&&this.fail(`Locale ${r} has the prefix ${s}, a prefix has to be one segment`),r!==o&&this.versionDirs[s]&&this.fail(`Locale ${r} and a version share the name ${s}, rename one of them`),this.localesMap[s]=r===o?"root":r,this.localePrefixes[r]=r===o?"":s}}addVersion(t,i){let o=this.versionDirs[t];o&&this.fail(`Version ${t} is published from both ${o} and ${i}, rename one of them`);for(let[r,n]of Object.entries(this.versionDirs))(`${i}/`.startsWith(`${n}/`)||`${n}/`.startsWith(`${i}/`))&&this.fail(`Version ${t} in ${i} overlaps version ${r} in ${n}, move it to a folder of its own`);this.versionDirs[t]=i}versionFolder(t,i){let o=W(this.root,D(this.root,i)).split(F).join("/");return(!o||o.startsWith("..")||de(o)||!L(d(this.root,o)))&&this.fail(`Path ${i} of version ${t} has to be an existing folder inside ${this.root}`),o}makeArchiveDirectory(t){t&&(L(t)||(me(t),ue(d(t,".gitkeep"),"")))}createVitepressConfig(){let t={},i=["locales","rewrites","themeConfig","versionsConfig"];for(let o of Object.keys(this.configuration))if(!i.includes(o))t[o]=this.configuration[o];else if(o==="themeConfig"){let r=this.configuration.themeConfig??{};t.themeConfig=Object.fromEntries(Object.entries(r).filter(([n])=>n!=="nav"&&n!=="sidebar"))}else o!=="versionsConfig"&&(t[o]={});return t}};a(g,"StateModel"),g=O([pe({scope:"singleton"})],g);import{forceInject as Ve}from"@remotex-labs/xinject";import{join as T}from"path/posix";import{inject as be}from"@remotex-labs/xinject";import{inject as ye}from"@remotex-labs/xinject";function E(e,t,i,o={}){if(e.versionSwitcher===!1)return;let{text:r,includeCurrentVersion:n}=e.versionSwitcher,c=a((f,l=o[f]??f)=>({text:l,link:`/${f}/`,skipVersioning:!0}),"item"),s=t.filter(f=>f!==i).map(f=>c(f));if(n){let f=o[i]??i;s.unshift(c(i,f.toLowerCase()==="latest"?f:`${f} (latest)`))}return{text:r,items:s}}a(E,"versionSwitcher");function z(){let{vitepressConfig:e,routesMap:t,labels:i,versionsList:o,latest:r,localeSegments:n}=ye(g),c={routes:t,labels:i,versions:o,latestVersion:r,locales:n};e.themeConfig={...e.themeConfig,versioning:c}}a(z,"parseVersioningData");function Ce(e){return"items"in e&&Array.isArray(e.items)}a(Ce,"isNavItemWithChildren");function ke(e){return"link"in e&&!!e.link&&typeof e.link=="string"&&!/^([a-z][\w+.-]*:|\/\/)/i.test(e.link)}a(ke,"isNavItemWithLink");function H(e,t,i=[]){if(Ce(t)&&!t.skipVersioning)return{...t,items:t.items.map(o=>H(e,o,i))};if(ke(t)&&!t.skipVersioning&&typeof t.link=="string"){let o=t.link.replace(/[?#].*$/,"");return i.includes(o.split("/").filter(Boolean)[0])||/\.(?!(md|html)$)[^./]+$/.test(o)?t:{...t,link:T("/",e,t.link)}}return t}a(H,"replaceLinksRecursive");function xe(e,t,i=[]){return t==="root"&&(t=""),e.map(o=>H(t,o,i))}a(xe,"populateNav");function U(e,t,i="",o={}){if(Array.isArray(t)){let r=i||"root";e[r]??=[],e[r].push(...t),o[r]?e[r].push(...o[r]):o.root&&e[r].push(...o.root);return}for(let[r,n]of Object.entries(t)){let s=r==="root"?r==="root"&&i===""?"root":i:T(i,r);e[s]??=[],e[s].push(...n),o[r]?e[s].push(...o[r]):o.root&&e[s].push(...o.root)}}a(U,"normalizeNav");function je(e){let t=e.configuration.locales,i=Object.keys(t),o={root:[]};e.configuration.themeConfig?.nav&&U(o,e.configuration.themeConfig.nav);let r=E(e.versionsConfig,e.versionsList,e.latest,e.labels);if(r&&o.root.push(r),!i.length)return o;let n={root:[]};for(let c of i){let s=e.localePrefixes[c]||"root";U(n,t[c].themeConfig?.nav??[],s,o)}return n}a(je,"normalizeNavs");function Se(e,t){if(e[t])return e[t];if(t.includes("/")){let i=t.split("/")[0];if(e[i])return e[i]}return e.root}a(Se,"getNavForLocale");function B(){let e=be(g),{vitepressConfig:{locales:t}}=e,i=je(e),o=["root",...Object.values(e.localePrefixes)];for(let[r,n]of Object.entries(t)){let c=o.includes(r)?T(r==="root"?"":r,e.latest):r;n.themeConfig??={},n.themeConfig.nav=xe(Se(i,c),c,e.versionsList)}}a(B,"parseNavs");import{join as Le}from"path/posix";import{inject as Te}from"@remotex-labs/xinject";function v(...e){let t=Le(...e.filter(Boolean));return t==="."?"":t}a(v,"safeJoin");function Ie(e,t){let i=t==="root"?"":t;return e.map(o=>o.skipVersioning||o?.link?.startsWith("http")||!(o.link||o.items)||!i?o:{...o,base:v("/",i,o.base??"","/")})}a(Ie,"populateSidebar");function C(e,t,i){if(t.length===0&&(t=["root"]),i.includes(e[0]))return{lang:"",version:e.shift()};let o=t.includes(e[0])?e.shift():"",r=i.includes(e[0])?e.shift():"";return{lang:o,version:r}}a(C,"extractLocale");function I(e,t,i,o=""){if(!i)return;let r=o==="root"?"":o;if(Array.isArray(i)){let s=r||"/";t[o]??={},t[o][s]??=[],t[o][s].push(...i);return}let n=Object.values(e.localesMap),c=e.versionsList;for(let[s,f]of Object.entries(i)){let l=s.split("/").filter(Boolean),{lang:p,version:m}=C(l,n,c);if(p&&!o.startsWith(p))continue;let h=m?v(r,m):r,u=v(h,...l)||"/",k=h||"root";t[k]??={},t[k][u]??=[];let q=Array.isArray(f)?f:[f];t[k][u].push(...q)}}a(I,"normalizeSidebar");function Ae(e){let{vitepressConfig:t,configuration:{locales:i,themeConfig:o}}=e,r={};if(Object.keys(i).length>0)for(let[s,f]of Object.entries(i))I(e,r,o?.sidebar,s),I(e,r,f.themeConfig?.sidebar,s);else I(e,r,o?.sidebar,"root");let n=Object.values(e.localesMap),c=e.versionsList;for(let s of Object.keys(t.locales)){if(r[s])continue;let{lang:f}=C(s.split("/"),n,c),l=r[f||"root"];if(l){r[s]={};for(let[p,m]of Object.entries(l)){let h=p.split("/").filter(Boolean);C(h,n,c),r[s][v(s,...h)]=[...m]}}}return r}a(Ae,"normalizeSidebars");function K(){let e=Te(g),{vitepressConfig:{locales:t}}=e,i=Object.values(e.localesMap),o=e.versionsList,r=Ae(e);for(let[n,c]of Object.entries(t))if(r[n]){c.themeConfig??={},(!c.themeConfig.sidebar||Array.isArray(c.themeConfig.sidebar))&&(c.themeConfig.sidebar={});for(let[s,f]of Object.entries(r[n])){let{lang:l,version:p}=C(s.split("/"),i,o);c.themeConfig.sidebar[s]=Ie(f,v(l,p))}}}a(K,"parseSidebars");import{join as J,posix as _}from"path";import{inject as Oe}from"@remotex-labs/xinject";import{readFileSync as we,writeFileSync as $e}from"fs";function G(){let{latest:e,localeSegments:t,vitepressConfig:i}=Oe(g),{buildEnd:o,base:r="/",vite:n={}}=i,c=["",...t],s=a((l,p)=>{let m=JSON.stringify(_.join("/",r,p,e,"/"));return l.replace("<head>",`<head><script>history.replaceState(null,'',${m}+location.search+location.hash)</script>`)},"withScript"),f={name:"viteplus-versions:latest",transformIndexHtml(l,{originalUrl:p=""}){let m=p.replace(/[?#].*/,"").replace(/\/?(index\.html)?$/,"/"),h=c.find(u=>m===_.join("/",r,u,"/"));return h===void 0?l:s(l,h)}};i.vite={...n,plugins:[...n.plugins??[],f]},i.buildEnd=async l=>{for(let p of c){let m=J(l.outDir,p,e,"index.html"),h=J(l.outDir,p,"index.html");try{$e(h,s(we(m,"utf8"),p),{flag:"wx"})}catch(u){if(!["ENOENT","EEXIST"].includes(u.code))throw u}}await o?.(l)}}a(G,"parseLatestEntry");import{inject as Ne}from"@remotex-labs/xinject";function Re(e,t,i,o){if(!e.startsWith("/")||e.startsWith("//"))return e;let r=e.replace(/[?#].*$/,""),n=r.split("/"),c=i.includes(n[1])||!o.includes(n[1])?1:2;if(i.includes(n[c])||/\.(?!(md|html)$)[^./]+$/.test(r))return e;let s=n.slice(0,c).join("/");return`${s}/${t}${e.slice(s.length)}`}a(Re,"versionLink");function X(){let e=Ne(g),{versionsList:t,localeSegments:i}=e,{config:o,...r}=e.vitepressConfig.markdown??{};e.vitepressConfig.markdown={...r,async config(n){n.core.ruler.push("viteplus_version_links",({env:c,tokens:s})=>{let l=c?.relativePath?.split("/").slice(0,2).find(p=>t.includes(p));if(l)for(let p of s.flatMap(m=>m.children??[])){let m=p.type==="link_open"&&p.attrGet("href");m&&p.attrSet("href",Re(m,l,t,i))}}),await o?.(n)}}}a(X,"parseVersionLinks");function Jt(e){let t=x(b,e),i=Ve(g,t).init();return V(),M(),z(),X(),G(),B(),K(),i.vitepressConfig}a(Jt,"defineVersionedConfig");export{Jt as defineVersionedConfig};
|
|
3
3
|
//# sourceMappingURL=index.js.map
|