summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-12-14 22:39:57 +0000
committerJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2024-01-12 08:28:52 +0000
commita783b4a6858f25a1453d77931926ba93bc0b7052 (patch)
tree3da5677a921e9f0fc39c9452394ec923a15f0a67 /src/lib
parent4710fb1c87d5926f630716f003e736e019b3fcf5 (diff)
Reorder news (#42)
* Reslugs news items * Fixes typo in getMostRecentNewsItem() * Fixes typo in newUtils.getDate() * Sorts news index from most recent to oldest
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/newsUtils.ts20
1 files changed, 14 insertions, 6 deletions
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;
}