Files
vercel__workflow/docs/lib/geistdocs/versions.ts
Rich Haines aa93cc5e69 Migrate docs to package-backed geistdocs (#2222)
* Migrate docs to package-backed geistdocs

* update agent install cmd on home page

* add copy prompt component usage

* update docs test for sitemap inclusion

* cut unused components

* address docs migration review feedback

* address stale review feedback: geistdocs 1.8.2, version icons, cookbook prompts

* drop Workflow from OSS products dropdown (self-link)

* bump @vercel/geistdocs to 1.11.0

* fix: resolve pnpm-lock.yaml conflict marker from main merge

---------

Co-authored-by: Peter Wielander <peter.wielander@vercel.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-07-13 18:44:11 +02:00

83 lines
2.6 KiB
TypeScript

export type DocsVersionId = 'v4' | 'v5';
export interface DocsVersion {
id: DocsVersionId;
label: string;
subtitle: string;
prefix: string;
preRelease: boolean;
}
export const VERSIONS: DocsVersion[] = [
{
id: 'v5',
label: 'v5 (Pre-release)',
subtitle: 'Workflow 5.x',
prefix: '/v5',
preRelease: true,
},
{
id: 'v4',
label: 'v4 (Latest)',
subtitle: 'Workflow 4.x',
prefix: '',
preRelease: false,
},
];
export const PRE_RELEASE_VERSION: DocsVersion = VERSIONS[0];
export const LATEST_VERSION: DocsVersion = VERSIONS[1];
/**
* Derive the active docs version from a pathname. Matches `/v5/...` (or
* `/<lang>/v5/...` once locale prefix is applied) against the pre-release
* prefix; everything else is v4.
*/
export function getVersionFromPathname(pathname: string): DocsVersion {
// The v5 segment sits either at the root (default locale hidden) or right
// after a locale segment — both cases are covered by checking positions
// 0 and 1.
const segments = pathname.split('/').filter(Boolean);
if (segments[0] === 'v5' || segments[1] === 'v5') {
return PRE_RELEASE_VERSION;
}
return LATEST_VERSION;
}
/**
* Build a URL for the same page under a different version. Preserves the
* trailing path after `/docs/` and any locale prefix.
*
* `/docs/...` and `/cookbook/...` paths are version-specific. All other
* routes (e.g. `/worlds`) are shared across versions and are returned
* unchanged.
*
* `usePathname()` can return either `/docs/...` (default locale hidden by
* the i18n middleware) or `/<locale>/docs/...` (non-default locale shown).
* We detect the locale segment by checking whether segment 0 is a
* structural path token (`docs` or `v5`) rather than assuming position.
*/
export function buildVersionUrl(
pathname: string,
targetVersion: DocsVersion
): string {
// Worlds and other shared routes have no versioned equivalent — unchanged.
if (!pathname.includes('/docs') && !pathname.includes('/cookbook')) {
return pathname;
}
const segments = pathname.split('/').filter(Boolean);
// Structural segments are path tokens that are never locale prefixes.
const isStructural = (s: string | undefined) =>
s === 'docs' || s === 'v5' || s === 'cookbook';
const localeSegments =
segments[0] && !isStructural(segments[0]) ? segments.slice(0, 1) : [];
let rest = segments.slice(localeSegments.length);
if (rest[0] === 'v5') rest = rest.slice(1);
const prefixSegments = targetVersion.prefix
? [targetVersion.prefix.replace(/^\//, '')]
: [];
const joined = [...localeSegments, ...prefixSegments, ...rest].join('/');
return `/${joined}`.replace(/\/+$/, '') || '/';
}