From 315a5b6293b6aff419ef221df7efd2567bd0f246 Mon Sep 17 00:00:00 2001 From: Visual Ehrmanntraut <30368284+VisualEhrmanntraut@users.noreply.github.com> Date: Sat, 17 Aug 2024 16:13:46 +0300 Subject: [PATCH] Add redirect to English visiting pages without language specified --- src/content.ts | 5 +++++ src/pages/[...enRedirectSlug].astro | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/content.ts create mode 100644 src/pages/[...enRedirectSlug].astro diff --git a/src/content.ts b/src/content.ts new file mode 100644 index 0000000..5c9738a --- /dev/null +++ b/src/content.ts @@ -0,0 +1,5 @@ +import { getCollection } from "astro:content"; + +export const englishPages = await getCollection("docs", (entry) => { + return entry.slug.startsWith("en/"); +}); diff --git a/src/pages/[...enRedirectSlug].astro b/src/pages/[...enRedirectSlug].astro new file mode 100644 index 0000000..28a9108 --- /dev/null +++ b/src/pages/[...enRedirectSlug].astro @@ -0,0 +1,28 @@ +--- +import type { CollectionEntry } from 'astro:content'; +import { englishPages } from '~/content'; + +export const stripLangFromSlug = (slug: CollectionEntry<'docs'>['slug']) => { + let index = slug.indexOf("/"); + if (index == -1) { + return slug; + } else { + return slug.slice(index + 1); + } +}; + +export async function getStaticPaths() { + return englishPages.map((page) => { + return { + params: { + enRedirectSlug: stripLangFromSlug(page.slug), + }, + props: { + englishSlug: page.slug, + }, + }; + }); +} +--- + +