Skip to content

Getting Started with @viteplus/versions ​

@viteplus/versions is a powerful VitePress plugin that enables versioned documentation with seamless navigation, localization support, and automatic route generation. This guide will help you set up and configure the plugin for your documentation needs.

Community project

This is a community project and is not affiliated with or endorsed by VoidZero, Vite, VitePress, or Vite+. See #59 for the background of the name.

Upgrading from v2

Since v3 every version is served under its name, the latest one included. An address without a version, such as /guide, opens the same page of the latest version, but a nav activeMatch such as ^/guide/ has to become ^/[^/]+/guide/. See Moving from v2.

Installation ​

Install the package using your preferred package manager:

bash
npm install @viteplus/versions
bash
pnpm add @viteplus/versions
bash
yarn add @viteplus/versions

Basic Setup ​

1. Initialize Your VitePress Project ​

If you haven't already, create a VitePress project:

bash
npm init vitepress

2. Configure Versioning ​

Create or modify your VitePress configuration file (): .vitepress/config.ts

ts
import { defineVersionedConfig } from '@viteplus/versions';

export default defineVersionedConfig({
  title: 'My Documentation',
  description: 'Versioned documentation with VitePress',
  
  // Versioning configuration
  versionsConfig: {
    current: 'next',      // Label of the version in sources
    latest: 'v2.0',       // The stable version, opened at the root
    sources: 'src',       // Current version source directory
    archive: 'archive',   // Archive directory for older versions
    versions: {
      'v1.0': { label: 'v1.0 (LTS)' }  // Per-version switcher label
    },
    versionSwitcher: {
      text: 'Version',
      includeCurrentVersion: true
    }
  },
  
  // Other VitePress configuration...
  themeConfig: {
    // ...
  }
});

Project Structure ​

Organize your documentation files following this structure:

text
docs/
├── .vitepress/
│   └── config.ts
├── src/            // Current version docs  ← sources   → /next/
│   ├── index.md
│   ├── guide/
│   │   └── ...
│   └── api/
│       └── ...
└── archive/       // Archived versions      ← archive
    ├── v1.0/                                          → /v1.0/
    │   ├── index.md
    │   └── ...
    └── v2.0/                                          → /v2.0/  (the latest version, also opened at /)
        ├── index.md
        └── ...

Every version is served under its own name, and / opens the home page of the latest version. Leave latest out and the current version is the latest one. Both directory names are configurable through sources and archive. They are filesystem locations only, so renaming them does not change a single URL.

Core Features ​

Version Switching ​

The plugin automatically adds a version switcher that allows users to navigate between documentation versions:

  • Simple Configuration: Enable with minimal setup via versionSwitcher option
  • Custom Component: For advanced needs, use the VersionSwitcher component directly

Localization ​

Support multiple languages with version-aware localization:

ts
export default defineVersionedConfig({
  // ...
  locales: {
    root: {
      lang: 'en',
      label: 'English'
    },
    fr: {
      lang: 'fr',
      label: 'Français'
    }
  }
});

Version-Specific Navigation ​

Define different navigation structures for each version:

ts
export default defineVersionedConfig({
  // ...
  themeConfig: {
    nav: {
      root: [
        { text: 'Home', link: '/' },
        { text: 'Guide', link: '/guide/' }
      ],
      'v1.0': [
        { text: 'Home', link: '/' },
        { text: 'Legacy API', link: '/legacy-api/' }
      ]
    }
  }
});

Stable and Unreleased Versions ​

Point latest at an archived version while sources holds the docs of a coming release. Readers keep landing on the stable version at /, which shows its versioned URL such as /v2.0/, the coming one is reviewed at /next/, and every version keeps a permanent URL under its name.

Automatic Path Rewriting ​

URLs are automatically structured based on versions and locales, creating clean, predictable paths. Absolute links in your markdown, such as /guide/, stay inside the version of the page that holds them.

Advanced Configuration ​

Custom URL Structure ​

Customize how URLs are generated with the rewritesHook:

ts
import { join } from 'path/posix';

export default defineVersionedConfig({
  // ...
  versionsConfig: {
    // ...
    hooks: {
      rewritesHook: (source, version, locale) => {
        // The locale and the version stay in front, the rest is yours
        return join(locale, version, source.toLowerCase());
      }
    }
  }
});

Enhanced Theme Integration ​

Integrate the version switcher into your custom theme:

ts
// .vitepress/theme/index.ts
import DefaultTheme from 'vitepress/theme';
import VersionSwitcher from '@viteplus/versions/components/version-switcher.component.vue';

export default {
  extends: DefaultTheme,
  enhanceApp({ app }) {
    app.component('VersionSwitcher', VersionSwitcher);
  }
};

Key Configuration Options ​

OptionDescription
versionsConfig.currentLabel of the version in sources (e.g., "next")
versionsConfig.latestThe stable version, opened at the root
versionsConfig.versionsPer-version settings: a switcher label, a path
versionsConfig.sourcesDirectory containing current version docs
versionsConfig.archiveDirectory containing archived versions
versionsConfig.versionSwitcherConfiguration for the version switcher
versionsConfig.hooks.rewritesHookCustomize URL structure

See also ​

Released under the Mozilla Public License 2.0