From 50ee98e20e0721375cf58b0e2de1c471a246abb0 Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Mon, 14 Aug 2023 21:10:08 +0100 Subject: Adds news item page --- src/pages/[locale]/news/[...slug].astro | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/pages/[locale]/news/[...slug].astro (limited to 'src') diff --git a/src/pages/[locale]/news/[...slug].astro b/src/pages/[locale]/news/[...slug].astro new file mode 100644 index 0000000..815387f --- /dev/null +++ b/src/pages/[locale]/news/[...slug].astro @@ -0,0 +1,44 @@ +--- +import locales from '$i18n/locales'; +import Article from '$layouts/Article.astro'; +import type Locale from '$types/Locale'; +import { CollectionEntry, getCollection } from 'astro:content'; + +export async function getStaticPaths() { + function localeFromSlug(slug: CollectionEntry<'news'>['slug']): Locale | undefined { + const lowerCaseLocale = new RegExp(`^(${locales.map((l) => l.toLocaleLowerCase()).join('|')})`); + const matchedLowerCaseLocale = slug.match(lowerCaseLocale)?.[1]; + switch (matchedLowerCaseLocale) { + case 'sco': + return 'sco'; + case 'en-gb': + return 'en-GB'; + default: + console.warn('Could not find locale in slug %s', slug); + return undefined; + } + } + + const paths = (await getCollection('news')).map((entry) => ({ + params: { + slug: entry.slug.replace('/', '-'), + locale: localeFromSlug(entry.slug), + }, + props: { entry }, + })); + + console.info('Paths: %s', JSON.stringify(paths)); + + return paths; +} + +const { entry } = Astro.props; +const { Content } = await entry.render(); +--- + +
+
+

{entry.data.title}

+ +
+
-- cgit v1.2.3