Skip to content

URL Path Rewrites ​

Rewrites map a content file on disk to the URL a reader sees. @viteplus/versions builds VitePress's rewrites for you from the version and locale it detects in each path, and lets you override the final shape through a hook.

How a path is resolved ​

text
src/de/guide/intro.md → rewritesHook('guide/intro.md', 'next', 'de') → de/next/guide/intro

Three things happen before your hook runs:

  1. The folder of the version is stripped from the front of the path: sources for the current version, archive/<name> for an archived one, or its path.
  2. The version's name is taken as version, so every version, the latest one included, lands under its name.
  3. The next segment is taken as locale if it matches a configured locale. The root locale resolves to an empty string.

Whatever is left is passed as source.

TIP

Because step 1 uses your configured directory names, renaming sources or archive does not change any URL.

The hook ​

ts
// Default implementation
function rewritesHook(source: string, version: string, locale: string): string {
    return join(locale, version, source);
}
ArgumentExampleMeaning
sourceguide/intro.mdThe path with directory, version, and locale removed
versionv2.0.0The version's name
localedeConfigured locale, or '' for the root locale

Default output, given current: 'next', latest: 'v2.0.0', and a root locale of en:

FileURL
archive/v2.0.0/en/guide/intro.md/v2.0.0/guide/intro
archive/v2.0.0/de/guide/intro.md/de/v2.0.0/guide/intro
src/en/guide/intro.md/next/guide/intro
src/de/guide/intro.md/de/next/guide/intro
archive/v1.0.0/de/guide/intro.md/de/v1.0.0/guide/intro

Customizing the structure ​

The hook may reshape everything after the version, such as lowercasing file names or moving a folder:

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

export default defineVersionedConfig({
    versionsConfig: {
        hooks: { 
            rewritesHook: (source, version, locale) =>
                join(locale, version, source.replace(/^old-guide\//, 'guide/')) 
        } 
    }
});

That turns /de/v1.0.0/old-guide/intro into /de/v1.0.0/guide/intro.

WARNING

locale is empty for the root locale. Join the segments rather than interpolating them, so an empty value does not leave stray slashes in the URL.

Every page of every version runs through the hook, including sites without locales. Its output has to start with <locale>/<version>/, or <version>/ for the root locale, since the locale entries, the navigation, the version switchers, the link rewriting, and the root URL script look for the version right after the locale. A hook that moves or drops them stops the build, see Errors that stop the build.

See also ​

Released under the Mozilla Public License 2.0