summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/newsUtils.ts19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/lib/newsUtils.ts b/src/lib/newsUtils.ts
index ededb52..aa711e3 100644
--- a/src/lib/newsUtils.ts
+++ b/src/lib/newsUtils.ts
@@ -11,15 +11,24 @@ export async function getMostRecentNewsItem(locale: Locale) {
return allNewsInLocale.reduce((prev, curr) => (isBefore(curr, prev) ? curr : prev), someNewsItem);
}
-export function isNewsItemInLocale(newsItem: CollectionEntry<'news'>, locale: Locale) {
+export function getLocale(newsItem: CollectionEntry<'news'>) {
const srcPath = newsItem.id;
- const newsItemLocale = srcPath.slice(0, srcPath.indexOf('/'));
+ return srcPath.slice(0, srcPath.indexOf('/'));
+}
+
+export function getHref(newsItem: CollectionEntry<'news'>) {
+ const locale = getLocale(newsItem);
+ return `/${locale}/news/${newsItem.slug.replace('/', '-')}`;
+}
+
+export function isNewsItemInLocale(newsItem: CollectionEntry<'news'>, locale: Locale) {
+ const newsItemLocale = getLocale(newsItem);
return newsItemLocale === locale;
}
function isBefore(item1: CollectionEntry<'news'>, item2: CollectionEntry<'news'>) {
- const date1 = dateOf(item1);
- const date2 = dateOf(item2);
+ const date1 = getDate(item1);
+ const date2 = getDate(item2);
if (!date2) {
return false;
}
@@ -29,7 +38,7 @@ function isBefore(item1: CollectionEntry<'news'>, item2: CollectionEntry<'news'>
return date1.valueOf() < date2.valueOf();
}
-function dateOf(newsItem: 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;
}