aboutsummaryrefslogtreecommitdiff
path: root/packages/integrations/sitemap/src/utils/parse-i18n-url.ts
diff options
context:
space:
mode:
authorGravatar github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> 2025-06-05 14:25:23 +0000
committerGravatar github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> 2025-06-05 14:25:23 +0000
commite586d7d704d475afe3373a1de6ae20d504f79d6d (patch)
tree7e3fa24807cebd48a86bd40f866d792181191ee9 /packages/integrations/sitemap/src/utils/parse-i18n-url.ts
downloadastro-latest.tar.gz
astro-latest.tar.zst
astro-latest.zip
Sync from a8e1c0a7402940e0fc5beef669522b315052df1blatest
Diffstat (limited to 'packages/integrations/sitemap/src/utils/parse-i18n-url.ts')
-rw-r--r--packages/integrations/sitemap/src/utils/parse-i18n-url.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/packages/integrations/sitemap/src/utils/parse-i18n-url.ts b/packages/integrations/sitemap/src/utils/parse-i18n-url.ts
new file mode 100644
index 000000000..86221ca9d
--- /dev/null
+++ b/packages/integrations/sitemap/src/utils/parse-i18n-url.ts
@@ -0,0 +1,42 @@
+interface ParsedI18nUrl {
+ locale: string;
+ path: string;
+}
+
+// NOTE: The parameters have been schema-validated with Zod
+export function parseI18nUrl(
+ url: string,
+ defaultLocale: string,
+ locales: Record<string, string>,
+ base: string,
+): ParsedI18nUrl | undefined {
+ if (!url.startsWith(base)) {
+ return undefined;
+ }
+
+ let s = url.slice(base.length);
+
+ // Handle root URL
+ if (!s || s === '/') {
+ return { locale: defaultLocale, path: '/' };
+ }
+
+ if (s[0] !== '/') {
+ s = '/' + s;
+ }
+
+ // Get locale from path, e.g.
+ // "/en-US/" -> "en-US"
+ // "/en-US/foo" -> "en-US"
+ const locale = s.split('/')[1];
+ if (locale in locales) {
+ // "/en-US/foo" -> "/foo"
+ let path = s.slice(1 + locale.length);
+ if (!path) {
+ path = '/';
+ }
+ return { locale, path };
+ }
+
+ return { locale: defaultLocale, path: s };
+}