77 lines
3.2 KiB
TypeScript
77 lines
3.2 KiB
TypeScript
import type { I18nConfig } from './types.js';
|
|
/**
|
|
* Minimal shape the path builder needs from a document.
|
|
*
|
|
* The plugin doesn't know the client's Pages type, so it depends only on
|
|
* this contract: a map of locale code → slug for that locale. The template
|
|
* supplies it (e.g. by reading the localized slug field across locales).
|
|
*/
|
|
export type LocalizedSlugs = Record<string, string>;
|
|
type BuildPathArgs = {
|
|
/** slug per locale, e.g. { pl: 'strona-glowna', en: 'home' } */
|
|
slugs: LocalizedSlugs;
|
|
/** Target locale to build the path for */
|
|
locale: string;
|
|
config: I18nConfig;
|
|
/**
|
|
* Slug(s) representing the site root (served at /{locale} with no trailing
|
|
* segment). Defaults to 'home'. Matched against the slug in the target locale.
|
|
*
|
|
* Can be a single string (same home slug in every locale) OR a per-locale map
|
|
* (`{ pl: 'strona-glowna', de: 'startseite', en: 'home' }`). The map form is
|
|
* REQUIRED for multilingual homepages whose slug differs per language —
|
|
* otherwise the home page collapses to '/pl' but '/de/startseite' stays
|
|
* un-collapsed, breaking hreflang return tags (a real GSC error).
|
|
*/
|
|
homeSlug?: string | Record<string, string>;
|
|
/**
|
|
* Localized segment the document lives under, e.g.
|
|
* `{ pl: 'artykuly', en: 'articles' }` → /pl/artykuly/moj-post.
|
|
*
|
|
* These are the slugs of the collection's archive page, so the prefix is
|
|
* whatever an editor named that page — and it differs per locale for free.
|
|
* A document under a prefix is never the home page, so homeSlug is ignored.
|
|
*/
|
|
prefix?: LocalizedSlugs;
|
|
};
|
|
/**
|
|
* Builds a locale-prefixed path for a document in a target locale.
|
|
*
|
|
* Always prefixes the locale: /{locale} or /{locale}/{slug}. The home slug
|
|
* collapses to the locale root. Returns undefined if the document has no slug
|
|
* in the target locale (caller decides fallback behavior).
|
|
*
|
|
* @example
|
|
* buildLocalizedPath({ slugs: { pl: 'strona-glowna', en: 'home' }, locale: 'en', config })
|
|
* // → '/en' (home slug collapses to root)
|
|
*
|
|
* buildLocalizedPath({ slugs: { pl: 'o-nas', en: 'about' }, locale: 'en', config })
|
|
* // → '/en/about'
|
|
*
|
|
* buildLocalizedPath({
|
|
* slugs: { pl: 'moj-post', en: 'my-post' },
|
|
* prefix: { pl: 'artykuly', en: 'articles' },
|
|
* locale: 'en',
|
|
* config,
|
|
* })
|
|
* // → '/en/articles/my-post'
|
|
*/
|
|
export declare function buildLocalizedPath({ slugs, locale, config, homeSlug, prefix, }: BuildPathArgs): string | undefined;
|
|
type SwitchLocaleArgs = {
|
|
slugs: LocalizedSlugs;
|
|
targetLocale: string;
|
|
config: I18nConfig;
|
|
homeSlug?: string | Record<string, string>;
|
|
prefix?: LocalizedSlugs;
|
|
};
|
|
/**
|
|
* Resolves the equivalent path for the same document in a different locale —
|
|
* the language-switcher use case (/pl/strona-glowna → /en/home).
|
|
*
|
|
* Never dead-ends on a 404. When the document has no slug in the target locale,
|
|
* falls back to the archive it belongs to (/en/articles) if there is one, and
|
|
* to the locale root otherwise — the closest place the visitor would want.
|
|
*/
|
|
export declare function switchLocalePath({ slugs, targetLocale, config, homeSlug, prefix, }: SwitchLocaleArgs): string;
|
|
export {};
|