@viteplus/versions 2.3.0 → 3.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.
@@ -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
 
@@ -52,10 +52,6 @@ const {
52
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"
@@ -81,9 +77,8 @@ const {
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 versioningPlugin.versions"
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
- * `${version}/${locale}/${source}`
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
- * allowing complete control over URL or file structure.
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 currently active version.
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: "2.0",
582
+ * current: "next",
583
+ * latest: "2.0",
518
584
  * sources: "src",
519
585
  * archive: "archive",
520
586
  * hooks: {
521
- * rewritesHook: (source, version) => `/v${version}/${source}`
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 currently active documentation version.
598
+ * The label of the version that the `sources` folder holds.
532
599
  *
533
600
  * @remarks
534
- * Used to determine which version to serve by default and which version
535
- * should be highlighted in the version switcher.
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) => `/v${ver}/${src}` },
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 S=Object.defineProperty;var K=Object.getOwnPropertyDescriptor;var s=(e,t)=>S(e,"name",{value:t,configurable:!0});var O=(e,t,o,r)=>{for(var n=r>1?void 0:r?K(t,o):t,i=e.length-1,a;i>=0;i--)(a=e[i])&&(n=(r?a(t,o,n):a(n))||n);return r&&n&&S(t,o,n),n};import{join as b,posix as ie}from"path";import{Injectable as se}from"@remotex-labs/xinject";import{xterm as ae}from"@remotex-labs/xansi/xterm.component";import{join as B}from"path/posix";import{inject as J}from"@remotex-labs/xinject";function x(e){return e.split(/[-_]/)[0]}s(x,"getLanguageOnly");function R(e){let t=Object.assign({},e);return t.themeConfig&&Object.keys(t.themeConfig).length>0&&(t.themeConfig=Object.assign({},t.themeConfig),delete t.themeConfig.nav,delete t.themeConfig.sidebar),t}s(R,"createSanitizedLocale");function N(e,t){return e?"/":`/${x(t)}/`}s(N,"generateLocaleLink");function q(e,t,o){let r=R(e);return r.label=" ",r.link=N(t,o),r}s(q,"createVersionedLocale");function w(){let e=J(p),{versionsList:t,vitepressConfig:o,configuration:r,localesMap:n}=e,{locales:i}=o,a=r.locales,c="root"in a;for(let[f,l]of Object.entries(n)){let g=l==="root",m=c?a[l]:a[f],u=R(m),h=u.lang??"";i[l]=u,i[l].link=N(g,h);for(let y of t){let j=g?y:B(f,y);i[j]=q(u,g,h)}}}s(w,"parseLocale");import{existsSync as M,mkdirSync as ce,readdirSync as fe,writeFileSync as pe}from"fs";import{existsSync as te,readFileSync as re,realpathSync as oe}from"fs";import{join as I}from"path/posix";import{join as Y}from"path";import{inject as L}from"@remotex-labs/xinject";import{readdirSync as G}from"fs";import{join as Q,relative as X}from"path/posix";function T(e,t){let o={...e};for(let r in t)if(Object.prototype.hasOwnProperty.call(t,r)){let n=t[r],i=e[r];i&&typeof i=="object"&&n&&typeof n=="object"&&!Array.isArray(n)&&!Array.isArray(n)?o[r]=T(i,n):o[r]=n}return o}s(T,"deepMerge");function d(e,t=e){let o=[],r=G(t,{withFileTypes:!0});for(let n of r){let i=Q(t,n.name);if(n.isDirectory())o=o.concat(d(e,i));else if(n.isFile()&&n.name.endsWith(".md")){let a=X(e,i);o.push(a)}}return o}s(d,"getAllMarkdownFilesRelative");function P(e,t,o){return I(o,t,e)}s(P,"rewritesHook");function Z(e,t,o){let r=o.includes(e[0])?e.shift():"";return{lang:t.includes(e[0])?e.shift():"",version:r}}s(Z,"extractLocale");function _(){let e=L(p),t=Object.keys(e.localesMap),o=e.versionsList,r=`${e.sources}/`,n=`${e.archive}/`;return function(i){if(i.startsWith(r)){let a=i.slice(r.length),c=t.find(f=>a.startsWith(f+"/"));if(c){let f=e.localesMap[c]==="root"?"":c,l=a.slice(c.length+1);return e.versionsConfig.hooks.rewritesHook(l,"",f)}return a}if(i.startsWith(n)){let c=i.slice(n.length).split("/"),{lang:f,version:l}=Z(c,t,o),g=c.join("/");return e.versionsConfig.hooks.rewritesHook(g,l,e.localesMap[f]==="root"?"":f)}return i}}s(_,"createRouteResolver");function ee(e){let t=L(p),o={};o[t.versionsConfig.current]=d(t.sourcesPath).map(r=>e(I(t.sources,r)));for(let r of t.versionsList)o[r]=d(Y(t.archivePath,r)).map(n=>e(I(t.archive,r,n)));return o}s(ee,"parseVersionRoutes");function V(){let e=L(p),t=_();e.vitepressConfig.rewrites=t,Object.assign(e.routesMap,ee(t))}s(V,"parseRoutesComponent");var ne={fileExists:s(e=>te(e),"fileExists"),readFile:s(e=>re(e,"utf8"),"readFile"),realpath:s(e=>oe(e),"realpath")},C={locales:{},rewrites:{},versionsConfig:{current:"latest",sources:"src",archive:"archive",hooks:{rewritesHook:P},versionSwitcher:{text:"Switch Version",includeCurrentVersion:!0}},vue:{script:{fs:ne}},vite:{resolve:{dedupe:["vue","vitepress"]}}};var p=class{constructor(t=C){this.configuration=t;let{sources:o,archive:r}=t.versionsConfig;if(this.sources=o,this.archive=r,this.versionsConfig=t.versionsConfig,this.root=b(process.cwd(),process.argv[3]??"",t.srcDir??""),this.sourcesPath=b(this.root,this.sources),this.archivePath=b(this.root,this.archive),!M(this.sourcesPath))throw console.error(ae.red("[ viteplus ]"),`Source directory ${this.sourcesPath} does not exist
2
- `),new Error(`Source directory ${this.sourcesPath} does not exist`);this.makeArchiveDirectory(this.archivePath),this.vitepressConfig=this.createVitepressConfig()}configuration;root;sources;archive;sourcesPath;archivePath;versionsConfig;vitepressConfig;versionsList=[];localesMap={};routesMap={};init(){this.vitepressConfig.locales.root={themeConfig:{}};let t=fe(this.archivePath,{withFileTypes:!0}).filter(o=>o.isDirectory()).map(o=>o.name);return this.versionsList.push(...t),this.initializeLocalesMap(),this}initializeLocalesMap(){let t=this.vitepressConfig.locales,o=this.configuration.locales;if(t.root={},Object.keys(o).length>0){let r=Object.keys(o),n="root"in o?"root":r[0];for(let i of r){let a=x(o[i].lang??i);for(let c of this.versionsList){if(i===n)continue;let f=ie.join(a,c);t[f]={}}i===n?this.localesMap[o[i].link??a]="root":(t[i]={},this.localesMap[o[i].link??a]=i)}}for(let r of this.versionsList)t[r]={}}makeArchiveDirectory(t){t&&(M(t)||(ce(t),pe(b(t,".gitkeep"),"")))}createVitepressConfig(){let t={},o=["locales","rewrites","themeConfig","versionsConfig"];for(let r of Object.keys(this.configuration))if(!o.includes(r))t[r]=this.configuration[r];else if(r==="themeConfig"){let n=this.configuration.themeConfig??{};t.themeConfig=Object.fromEntries(Object.entries(n).filter(([i])=>i!=="nav"&&i!=="sidebar"))}else r!=="versionsConfig"&&(t[r]={});return t}};s(p,"StateModel"),p=O([se({scope:"singleton"})],p);import{forceInject as je}from"@remotex-labs/xinject";import{join as F}from"path/posix";import{inject as z}from"@remotex-labs/xinject";function $(e,t,o="Latest"){if(e.versionSwitcher===!1)return;let{text:r,includeCurrentVersion:n}=e.versionSwitcher,i=[];n&&i.push({link:"/",text:o==="Latest"?o:`${o} (latest)`,skipVersioning:!0});for(let a of t)i.push({text:a,link:`/${a}/`,skipVersioning:!0});return{text:r,items:i}}s($,"versionSwitcher");function le(e){return"items"in e&&Array.isArray(e.items)}s(le,"isNavItemWithChildren");function ge(e){return"link"in e&&!!e.link&&typeof e.link=="string"&&!e.link.includes("http")}s(ge,"isNavItemWithLink");function U(e,t){return le(t)&&!t.skipVersioning?{...t,items:t.items.map(o=>U(e,o))}:ge(t)&&!t.skipVersioning?{...t,link:typeof t.link=="string"?F("/",e,t.link):t.link}:t}s(U,"replaceLinksRecursive");function ue(e,t){let o=z(p);return t==="root"&&(t=""),e.map(r=>"component"in r&&r.component?{...r,props:{...r.props,versioningPlugin:{routes:o.routesMap,versions:o.versionsList,currentVersion:o.versionsConfig.current}}}:U(t,r))}s(ue,"populateNav");function W(e,t,o="",r={}){if(Array.isArray(t)){let n=o||"root";e[n]??=[],e[n].push(...t),r[n]?e[n].push(...r[n]):r.root&&e[n].push(...r.root);return}for(let[n,i]of Object.entries(t)){let c=n==="root"?n==="root"&&o===""?"root":o:F(o,n);e[c]??=[],e[c].push(...i),r[n]?e[c].push(...r[n]):r.root&&e[c].push(...r.root)}}s(W,"normalizeNav");function me(e){let t=e.configuration.locales,o=Object.keys(t),r={root:[]};e.configuration.themeConfig?.nav&&W(r,e.configuration.themeConfig.nav);let n=$(e.versionsConfig,e.versionsList,e.versionsConfig.current);if(n&&r.root.push(n),!o.length)return r;let i={root:[]};for(let a of o){let c=a==="root"||e.localesMap[a]==="root"?"root":a;W(i,t[a].themeConfig?.nav??[],c,r)}return i}s(me,"normalizeNavs");function ye(e,t){if(e[t])return e[t];if(t.includes("/")){let o=t.split("/")[0];if(e[o])return e[o]}return e.root}s(ye,"getNavForLocale");function H(){let e=z(p),{vitepressConfig:{locales:t}}=e,o=me(e);for(let[r,n]of Object.entries(t)){let i=ye(o,r);n.themeConfig??={},n.themeConfig.nav=ue(i,r)}}s(H,"parseNavs");import{join as he}from"path/posix";import{inject as ve}from"@remotex-labs/xinject";function v(...e){let t=he(...e.filter(Boolean));return t==="."?"":t}s(v,"safeJoin");function de(e,t){let o=t==="root"?"":t;return e.map(r=>r.skipVersioning||r?.link?.startsWith("http")||!(r.link||r.items)||!o?r:{...r,base:v("/",o,r.base??"","/")})}s(de,"populateSidebar");function k(e,t,o){if(t.length===0&&(t=["root"]),o.includes(e[0]))return{lang:"",version:e.shift()};let r=t.includes(e[0])?e.shift():"",n=o.includes(e[0])?e.shift():"";return{lang:r,version:n}}s(k,"extractLocale");function A(e,t,o,r=""){if(!o)return;let n=r==="root"?"":r;if(Array.isArray(o)){let c=n||"/";t[r]??={},t[r][c]??=[],t[r][c].push(...o);return}let i=Object.values(e.localesMap),a=e.versionsList;for(let[c,f]of Object.entries(o)){let l=c.split("/").filter(Boolean),{lang:g,version:m}=k(l,i,a);if(g&&!r.startsWith(g))continue;let u=m?v(n,m):n,h=v(u,...l)||"/",y=u||"root";t[y]??={},t[y][h]??=[];let j=Array.isArray(f)?f:[f];t[y][h].push(...j)}}s(A,"normalizeSidebar");function Ce(e){let{vitepressConfig:t,configuration:{locales:o,themeConfig:r}}=e,n={};if(Object.keys(o).length>0)for(let[c,f]of Object.entries(o))A(e,n,r?.sidebar,c),A(e,n,f.themeConfig?.sidebar,c);else A(e,n,r?.sidebar,"root");let i=Object.values(e.localesMap),a=e.versionsList;for(let c of Object.keys(t.locales)){if(n[c])continue;let{lang:f}=k(c.split("/"),i,a),l=n[f||"root"];if(l){n[c]={};for(let[g,m]of Object.entries(l)){let u=g.split("/").filter(Boolean);k(u,i,a),n[c][v(c,...u)]=[...m]}}}return n}s(Ce,"normalizeSidebars");function D(){let e=ve(p),{vitepressConfig:{locales:t}}=e,o=Object.values(e.localesMap),r=e.versionsList,n=Ce(e);for(let[i,a]of Object.entries(t))if(n[i]){a.themeConfig??={},(!a.themeConfig.sidebar||Array.isArray(a.themeConfig.sidebar))&&(a.themeConfig.sidebar={});for(let[c,f]of Object.entries(n[i])){let{lang:l,version:g}=k(c.split("/"),o,r);a.themeConfig.sidebar[c]=de(f,v(l,g))}}}s(D,"parseSidebars");import{inject as be}from"@remotex-labs/xinject";function ke(e,t,o){let r=s(i=>i.replace(/[.*+?^${}()|[\]\\/]/g,"\\$&"),"escape"),n=o.length?`(?:(?:${o.map(r).join("|")})\\/)?`:"";return new RegExp(`^(${r(e)}${n})${r(t)}(?:\\/|$)`)}s(ke,"currentVersionPattern");function E(){let e=be(p),{current:t}=e.versionsConfig;if(!t||e.versionsList.includes(t))return;let o=`/${e.vitepressConfig.base??""}/`.replace(/\/+/g,"/"),r=Object.entries(e.localesMap).filter(([,a])=>a!=="root").map(([a])=>a.replace(/^\/+|\/+$/g,"")),n=ke(o,t,r),i=["script",{},`var p=location.pathname.replace(${n},'$1');p!==location.pathname&&location.replace(p+location.search+location.hash)`];e.vitepressConfig.head=[...e.vitepressConfig.head??[],i]}s(E,"parseCurrentVersionRedirect");function Tt(e){let t=T(C,e),o=je(p,t).init();return w(),V(),H(),D(),E(),o.vitepressConfig}s(Tt,"defineVersionedConfig");export{Tt as defineVersionedConfig};
1
+ var A=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var c=(e,t)=>A(e,"name",{value:t,configurable:!0});var O=(e,t,i,o)=>{for(var r=o>1?void 0:o?Z(t,i):t,n=e.length-1,a;n>=0;n--)(a=e[n])&&(r=(o?a(t,i,r):a(r))||r);return o&&r&&A(t,i,r),r};import{Injectable as me}from"@remotex-labs/xinject";import{xterm as ue}from"@remotex-labs/xansi/xterm.component";import{join as w}from"path/posix";import{inject as ee}from"@remotex-labs/xinject";function P(e){return e.split(/[-_]/)[0]}c(P,"getLanguageOnly");function N(e={}){let t={...e};return t.themeConfig&&(t.themeConfig={...t.themeConfig},delete t.themeConfig.nav,delete t.themeConfig.sidebar),t}c(N,"createSanitizedLocale");function R(e){return e?`/${e}/`:"/"}c(R,"generateLocaleLink");function te(e,t){return{...N(e),label:" ",link:R(t)}}c(te,"createVersionedLocale");function V(){let{latest:e,versionsList:t,vitepressConfig:{locales:i,base:o="/"},configuration:r,localePrefixes:n}=ee(p),a=c((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 g of t)i[w(f,g)]=a(te(l,f),f,g);i[f||"root"]=a({...l,link:R(f)},f,e)}i.root=a(i.root,"",e);for(let s of t)i[s]??=a({},"",s)}c(V,"parseLocale");import{existsSync as $,mkdirSync as he,readdirSync as de,writeFileSync as ve}from"fs";import{isAbsolute as ye,join as d,posix as be,relative as W,resolve as F,sep as E}from"path";import{existsSync as fe,readFileSync as le,realpathSync as pe}from"fs";import{join as L}from"path/posix";import{join as ne}from"path";import{inject as b}from"@remotex-labs/xinject";import{readdirSync as re}from"fs";import{join as oe,relative as ie}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}c(x,"deepMerge");function j(e,t=e){let i=[],o=re(t,{withFileTypes:!0});for(let r of o){let n=oe(t,r.name);if(r.isDirectory())i=i.concat(j(e,n));else if(r.isFile()&&r.name.endsWith(".md")){let a=ie(e,n);i.push(a)}}return i}c(j,"getAllMarkdownFilesRelative");function M(e,t,i){return L(i,t,e)}c(M,"rewritesHook");function se(){let e=b(p),{localesMap:t,versionsConfig:i}=e,o=Object.keys(t),r=Object.entries(e.versionDirs);return function(n){let a=r.find(([,m])=>n.startsWith(`${m}/`));if(!a)return n;let[s,f]=a,l=n.slice(f.length+1),g=o.find(m=>l.startsWith(`${m}/`));if(!g)return i.hooks.rewritesHook(l,s,"");let u=t[g]==="root"?"":g;return i.hooks.rewritesHook(l.slice(g.length+1),s,u)}}c(se,"createRouteResolver");function ae(e){let{root:t,versionDirs:i}=b(p),o=c(r=>j(ne(t,r)).map(n=>e(L(r,n))),"routes");return Object.fromEntries(Object.entries(i).map(([r,n])=>[r,o(n)]))}c(ae,"parseVersionRoutes");function ce(){let e=b(p),{versionsList:t,localeSegments:i,versionsConfig:o}=e;for(let r of["",...i])for(let n of t){let a=`${L(r,n)}/`,s=o.hooks.rewritesHook("index.md",n,r);s.startsWith(a)||e.fail(`rewritesHook has to keep ${a} at the front of every route, it returned ${s}`)}}c(ce,"assertRewritesHook");function D(){let e=b(p);ce();let t=se();e.vitepressConfig.rewrites=t,Object.assign(e.routesMap,ae(t))}c(D,"parseRoutesComponent");var ge={fileExists:c(e=>fe(e),"fileExists"),readFile:c(e=>le(e,"utf8"),"readFile"),realpath:c(e=>pe(e),"realpath")},C={locales:{},rewrites:{},versionsConfig:{current:"latest",latest:"",sources:"src",archive:"archive",hooks:{rewritesHook:M},versionSwitcher:{text:"Switch Version",includeCurrentVersion:!0},versions:{}},vue:{script:{fs:ge}},vite:{resolve:{dedupe:["vue","vitepress"]}}};var p=class{constructor(t=C){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,F(this.root,i)).split(E).join("/"),this.archive=o,this.versionsConfig=t.versionsConfig,this.sourcesPath=d(this.root,this.sources),this.archivePath=d(this.root,this.archive),$(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,r=[];for(let n of de(this.archivePath,{withFileTypes:!0}))n.isDirectory()&&r.push([n.name,be.join(this.archive,n.name)]);for(let[n,{path:a}]of Object.entries(o))a&&r.push([n,this.versionFolder(n,a)]);r.sort(([n],[a])=>a.localeCompare(n,"en",{numeric:!0})),this.addVersion(t,this.sources);for(let[n,a]of r)this.addVersion(n,a);for(let n of[i,...Object.keys(o)])n&&!this.versionDirs[n]&&this.fail(`Version ${n} is neither the current version nor an archive folder`);this.latest=i||t;for(let n of this.versionsList)this.labels[n]=o[n]?.label??n;return this.initializeLocalesMap(),this}fail(t){throw console.error(ue.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:a}=t[r],s=a?a.replace(/^\/+|\/+$/g,""):P(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,F(this.root,i)).split(E).join("/");return(!o||o.startsWith("..")||ye(o)||!$(d(this.root,o)))&&this.fail(`Path ${i} of version ${t} has to be an existing folder inside ${this.root}`),o}makeArchiveDirectory(t){t&&($(t)||(he(t),ve(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}};c(p,"StateModel"),p=O([me({scope:"singleton"})],p);import{forceInject as Me}from"@remotex-labs/xinject";import{join as S}from"path/posix";import{inject as ke}from"@remotex-labs/xinject";import{inject as Ce}from"@remotex-labs/xinject";function U(e,t,i,o={}){if(e.versionSwitcher===!1)return;let{text:r,includeCurrentVersion:n}=e.versionSwitcher,a=c((f,l=o[f]??f)=>({text:l,link:`/${f}/`,skipVersioning:!0}),"item"),s=t.filter(f=>f!==i).map(f=>a(f));if(n){let f=o[i]??i;s.unshift(a(i,f.toLowerCase()==="latest"?f:`${f} (latest)`))}return{text:r,items:s}}c(U,"versionSwitcher");function z(){let{vitepressConfig:e,routesMap:t,labels:i,versionsList:o,latest:r,localeSegments:n}=Ce(p),a={routes:t,labels:i,versions:o,latestVersion:r,locales:n};e.themeConfig={...e.themeConfig,versioning:a}}c(z,"parseVersioningData");function xe(e){return"items"in e&&Array.isArray(e.items)}c(xe,"isNavItemWithChildren");function je(e){return"link"in e&&!!e.link&&typeof e.link=="string"&&!/^([a-z][\w+.-]*:|\/\/)/i.test(e.link)}c(je,"isNavItemWithLink");function B(e,t,i=[]){if(xe(t)&&!t.skipVersioning)return{...t,items:t.items.map(o=>B(e,o,i))};if(je(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:S("/",e,t.link)}}return t}c(B,"replaceLinksRecursive");function Le(e,t,i=[]){return t==="root"&&(t=""),e.map(o=>B(t,o,i))}c(Le,"populateNav");function H(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:S(i,r);e[s]??=[],e[s].push(...n),o[r]?e[s].push(...o[r]):o.root&&e[s].push(...o.root)}}c(H,"normalizeNav");function $e(e){let t=e.configuration.locales,i=Object.keys(t),o={root:[]};e.configuration.themeConfig?.nav&&H(o,e.configuration.themeConfig.nav);let r=U(e.versionsConfig,e.versionsList,e.latest,e.labels);if(r&&o.root.push(r),!i.length)return o;let n={root:[]};for(let a of i){let s=e.localePrefixes[a]||"root";H(n,t[a].themeConfig?.nav??[],s,o)}return n}c($e,"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}c(Se,"getNavForLocale");function K(){let e=ke(p),{vitepressConfig:{locales:t}}=e,i=$e(e),o=["root",...Object.values(e.localePrefixes)];for(let[r,n]of Object.entries(t)){let a=o.includes(r)?S(r==="root"?"":r,e.latest):r;n.themeConfig??={},n.themeConfig.nav=Le(Se(i,a),a,e.versionsList)}}c(K,"parseNavs");import{join as Te}from"path/posix";import{inject as Ie}from"@remotex-labs/xinject";function v(...e){let t=Te(...e.filter(Boolean));return t==="."?"":t}c(v,"safeJoin");function Ae(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??"","/")})}c(Ae,"populateSidebar");function k(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}}c(k,"extractLocale");function T(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),a=e.versionsList;for(let[s,f]of Object.entries(i)){let l=s.split("/").filter(Boolean),{lang:g,version:u}=k(l,n,a);if(g&&!o.startsWith(g))continue;let m=u?v(r,u):r,y=v(m,...l)||"/",h=m||"root";t[h]??={},t[h][y]??=[];let Y=Array.isArray(f)?f:[f];t[h][y].push(...Y)}}c(T,"normalizeSidebar");function Oe(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))T(e,r,o?.sidebar,s),T(e,r,f.themeConfig?.sidebar,s);else T(e,r,o?.sidebar,"root");let n=Object.values(e.localesMap),a=e.versionsList;for(let s of Object.keys(t.locales)){if(r[s])continue;let{lang:f}=k(s.split("/"),n,a),l=r[f||"root"];if(l){r[s]={};for(let[g,u]of Object.entries(l)){let m=g.split("/").filter(Boolean);k(m,n,a),r[s][v(s,...m)]=[...u]}}}return r}c(Oe,"normalizeSidebars");function J(){let e=Ie(p),{vitepressConfig:{locales:t}}=e,i=Object.values(e.localesMap),o=e.versionsList,r=Oe(e);for(let[n,a]of Object.entries(t))if(r[n]){a.themeConfig??={},(!a.themeConfig.sidebar||Array.isArray(a.themeConfig.sidebar))&&(a.themeConfig.sidebar={});for(let[s,f]of Object.entries(r[n])){let{lang:l,version:g}=k(s.split("/"),i,o);a.themeConfig.sidebar[s]=Ae(f,v(l,g))}}}c(J,"parseSidebars");import{join as I}from"path";import{inject as _}from"@remotex-labs/xinject";import{readFileSync as we,writeFileSync as Pe}from"fs";function Ne(e){let t=location.pathname===e.base.slice(0,-1)?e.base:location.pathname;if(t.indexOf(e.base)!==0)return;let i=decodeURI(t.slice(e.base.length)).split("/"),o=e.locales.indexOf(i[0])>-1?`${i.shift()}/`:"";if(e.versions.indexOf(i[0])>-1)return;let r=i.join("/").replace(/\.html$/,"").replace(/\/$/,"").replace(/(^|\/)index$/,""),n=`${o}${e.latest}/`,s=(r?[`${n}${r}.md`,`${n}${r}/index.md`]:[`${n}index.md`]).find(l=>e.routes.indexOf(l)>-1);if(!s)return;let f=s.replace(/(^|\/)index\.md$/,"$1").replace(/\.md$/,e.cleanUrls?"":".html");history.replaceState(history.state,"",`${encodeURI(e.base+f)}${location.search}${location.hash}`)}c(Ne,"openLatestPage");function Re(){let{latest:e,localeSegments:t,versionsList:i,routesMap:o,vitepressConfig:r}=_(p),{base:n="/",cleanUrls:a=!1}=r,s=n.replace(/^\/+|\/+$/g,""),f={base:s?`/${s}/`:"/",latest:e,cleanUrls:a,locales:t,versions:i,routes:o[e]??[]},l=JSON.stringify(f).replace(/</g,"\\u003c");return`<script>(${Ne.toString()})(${l})</script>`}c(Re,"latestScript");function G(){let{latest:e,localeSegments:t,vitepressConfig:i}=_(p),{buildEnd:o,vite:r={}}=i,n=Re(),a=c(f=>f.replace("<head>",`<head>${n}`),"withScript"),s={name:"viteplus-versions:latest",transformIndexHtml:a};i.vite={...r,plugins:[...r.plugins??[],s]},i.buildEnd=async f=>{let l=c((u,m,y)=>{try{Pe(m,a(we(u,"utf8")),{flag:y})}catch(h){if(!["ENOENT","EEXIST"].includes(h.code))throw h}},"write");for(let u of["",...t])l(I(f.outDir,u,e,"index.html"),I(f.outDir,u,"index.html"),"wx");let g=I(f.outDir,"404.html");l(g,g,"w"),await o?.(f)}}c(G,"parseLatestEntry");import{inject as Ve}from"@remotex-labs/xinject";function X(e,t,i,o){if(!e.startsWith("/")||e.startsWith("//"))return e;let r=e.replace(/[?#].*$/,""),n=r.split("/"),a=i.includes(n[1])||!o.includes(n[1])?1:2;if(i.includes(n[a])||/\.(?!(md|html)$)[^./]+$/.test(r))return e;let s=n.slice(0,a).join("/");return`${s}/${t}${e.slice(s.length)}`}c(X,"versionLink");function q(e,t,i,o){if(!e||typeof e!="object")return;let r=e;for(let[n,a]of Object.entries(r))n==="link"&&typeof a=="string"?r[n]=X(a,t,i,o):q(a,t,i,o)}c(q,"versionFrontmatterLinks");function Q(){let e=Ve(p),{versionsList:t,localeSegments:i}=e,{config:o,...r}=e.vitepressConfig.markdown??{},{transformPageData:n}=e.vitepressConfig,a=c(s=>s?.split("/").slice(0,2).find(f=>t.includes(f)),"versionOf");e.vitepressConfig.transformPageData=async(s,f)=>{let l=a(s.relativePath);return l&&q(s.frontmatter,l,t,i),n?.(s,f)},e.vitepressConfig.markdown={...r,async config(s){s.core.ruler.push("viteplus_version_links",({env:f,tokens:l})=>{let g=a(f?.relativePath);if(g)for(let u of l.flatMap(m=>m.children??[])){let m=u.type==="link_open"&&u.attrGet("href");m&&u.attrSet("href",X(m,g,t,i))}}),await o?.(s)}}}c(Q,"parseVersionLinks");function Gt(e){let t=x(C,e),i=Me(p,t).init();return V(),D(),z(),Q(),G(),K(),J(),i.vitepressConfig}c(Gt,"defineVersionedConfig");export{Gt as defineVersionedConfig};
3
3
  //# sourceMappingURL=index.js.map