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
src/de/guide/intro.md → rewritesHook('guide/intro.md', 'next', 'de') → de/next/guide/introThree things happen before your hook runs:
- The folder of the version is stripped from the front of the path:
sourcesfor the current version,archive/<name>for an archived one, or itspath. - The version's name is taken as
version, so every version, thelatestone included, lands under its name. - The next segment is taken as
localeif 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
// Default implementation
function rewritesHook(source: string, version: string, locale: string): string {
return join(locale, version, source);
}| Argument | Example | Meaning |
|---|---|---|
source | guide/intro.md | The path with directory, version, and locale removed |
version | v2.0.0 | The version's name |
locale | de | Configured locale, or '' for the root locale |
Default output, given current: 'next', latest: 'v2.0.0', and a root locale of en:
| File | URL |
|---|---|
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:
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.
