summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/content/news/en-GB/2023-03-17-eiks-30.mdx (renamed from src/content/news/en-GB/2023-03-17-eiks-an-ens-30.mdx)0
-rw-r--r--src/content/news/en-GB/2023-12-10-eiks-31.mdx (renamed from src/content/news/en-GB/2023-12-10-eiks-an-ens-31.mdx)0
-rw-r--r--src/content/news/sco/2023-03-17-eiks-30.mdx (renamed from src/content/news/sco/2023-03-17-eiks-an-ens-30.mdx)0
-rw-r--r--src/content/news/sco/2023-12-10-eiks-31.mdx (renamed from src/content/news/sco/2023-12-10-eiks-an-ens-31.mdx)0
-rw-r--r--src/lib/newsUtils.ts20
-rw-r--r--src/pages/[locale]/news/[...slug].astro23
-rw-r--r--src/pages/[locale]/news/index.astro6
7 files changed, 22 insertions, 27 deletions
diff --git a/src/content/news/en-GB/2023-03-17-eiks-an-ens-30.mdx b/src/content/news/en-GB/2023-03-17-eiks-30.mdx
index 34a83ac..34a83ac 100644
--- a/src/content/news/en-GB/2023-03-17-eiks-an-ens-30.mdx
+++ b/src/content/news/en-GB/2023-03-17-eiks-30.mdx
diff --git a/src/content/news/en-GB/2023-12-10-eiks-an-ens-31.mdx b/src/content/news/en-GB/2023-12-10-eiks-31.mdx
index 1158dde..1158dde 100644
--- a/src/content/news/en-GB/2023-12-10-eiks-an-ens-31.mdx
+++ b/src/content/news/en-GB/2023-12-10-eiks-31.mdx
diff --git a/src/content/news/sco/2023-03-17-eiks-an-ens-30.mdx b/src/content/news/sco/2023-03-17-eiks-30.mdx
index 201642c..201642c 100644
--- a/src/content/news/sco/2023-03-17-eiks-an-ens-30.mdx
+++ b/src/content/news/sco/2023-03-17-eiks-30.mdx
diff --git a/src/content/news/sco/2023-12-10-eiks-an-ens-31.mdx b/src/content/news/sco/2023-12-10-eiks-31.mdx
index 8aa6604..8aa6604 100644
--- a/src/content/news/sco/2023-12-10-eiks-an-ens-31.mdx
+++ b/src/content/news/sco/2023-12-10-eiks-31.mdx
diff --git a/src/lib/newsUtils.ts b/src/lib/newsUtils.ts
index aa711e3..bffc7d6 100644
--- a/src/lib/newsUtils.ts
+++ b/src/lib/newsUtils.ts
@@ -1,5 +1,6 @@
+import locales from '$i18n/locales';
import type Locale from '$types/Locale';
-import { CollectionEntry, getCollection } from 'astro:content';
+import { getCollection, type CollectionEntry } from 'astro:content';
export async function getMostRecentNewsItem(locale: Locale) {
const allNews = await getCollection('news');
@@ -8,7 +9,7 @@ export async function getMostRecentNewsItem(locale: Locale) {
if (!someNewsItem) {
return null;
}
- return allNewsInLocale.reduce((prev, curr) => (isBefore(curr, prev) ? curr : prev), someNewsItem);
+ return allNewsInLocale.reduce((prev, curr) => (isBefore(prev, curr) ? curr : prev), someNewsItem);
}
export function getLocale(newsItem: CollectionEntry<'news'>) {
@@ -18,7 +19,13 @@ export function getLocale(newsItem: CollectionEntry<'news'>) {
export function getHref(newsItem: CollectionEntry<'news'>) {
const locale = getLocale(newsItem);
- return `/${locale}/news/${newsItem.slug.replace('/', '-')}`;
+ return `/${locale}/news/${getSlug(newsItem)}`;
+}
+
+export function getSlug(newsItem: CollectionEntry<'news'>) {
+ const toLowerCase = (s: string) => s.toLowerCase();
+ const matchLocales = new RegExp(`(${locales.map(toLowerCase).join('|')})/`);
+ return newsItem.slug.replace(matchLocales, '');
}
export function isNewsItemInLocale(newsItem: CollectionEntry<'news'>, locale: Locale) {
@@ -26,7 +33,7 @@ export function isNewsItemInLocale(newsItem: CollectionEntry<'news'>, locale: Lo
return newsItemLocale === locale;
}
-function isBefore(item1: CollectionEntry<'news'>, item2: CollectionEntry<'news'>) {
+export function isBefore(item1: CollectionEntry<'news'>, item2: CollectionEntry<'news'>) {
const date1 = getDate(item1);
const date2 = getDate(item2);
if (!date2) {
@@ -39,6 +46,7 @@ function isBefore(item1: CollectionEntry<'news'>, item2: CollectionEntry<'news'>
}
export function getDate(newsItem: CollectionEntry<'news'>) {
- const match = newsItem.id.match(/(?<=(en-GB|sco))\/(\d\d\d\d-\d\d-\d\d)/)?.[2];
- return match ? new Date(match) : null;
+ const matchDate = new RegExp(`(${locales.join('|')})/(\\d\\d\\d\\d-\\d\\d-\\d\\d)`);
+ const date = newsItem.id.match(matchDate)?.[2];
+ return date ? new Date(date) : null;
}
diff --git a/src/pages/[locale]/news/[...slug].astro b/src/pages/[locale]/news/[...slug].astro
index e3b8b36..a24e65a 100644
--- a/src/pages/[locale]/news/[...slug].astro
+++ b/src/pages/[locale]/news/[...slug].astro
@@ -1,28 +1,13 @@
---
-import locales from '$i18n/locales';
import Article from '$layouts/Article.astro';
-import type Locale from '$types/Locale';
-import { CollectionEntry, getCollection } from 'astro:content';
+import { getCollection } from 'astro:content';
+import { getLocale, getSlug } from '$lib/newsUtils';
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;
- }
- }
-
return (await getCollection('news')).map((entry) => ({
params: {
- slug: entry.slug.replace('/', '-'),
- locale: localeFromSlug(entry.slug),
+ slug: getSlug(entry),
+ locale: getLocale(entry),
},
props: { entry },
}));
diff --git a/src/pages/[locale]/news/index.astro b/src/pages/[locale]/news/index.astro
index 37e7dd6..112f87b 100644
--- a/src/pages/[locale]/news/index.astro
+++ b/src/pages/[locale]/news/index.astro
@@ -5,12 +5,14 @@ import Page from '$layouts/Page.astro';
import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath';
import localeStaticPaths from '$lib/localeStaticPaths';
import { getCollection } from 'astro:content';
-import { isNewsItemInLocale } from '$lib/newsUtils';
+import { isBefore, isNewsItemInLocale } from '$lib/newsUtils';
import NewsIndex from '$components/NewsIndex/NewsIndex.astro';
const allNews = await getCollection('news');
const locale = getLocaleFromPathOrThrow(Astro.url.pathname);
-const allNewsInLocale = allNews.filter((item) => isNewsItemInLocale(item, locale));
+const allNewsInLocale = allNews
+ .filter((item) => isNewsItemInLocale(item, locale))
+ .sort((a, b) => (isBefore(a, b) ? 1 : isBefore(b, a) ? -1 : 0));
const t = translate(locale);
export async function getStaticPaths() {