blob: ededb526511c90394b1c2439824795a5727c34f7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import type Locale from '$types/Locale';
import { CollectionEntry, getCollection } from 'astro:content';
export async function getMostRecentNewsItem(locale: Locale) {
const allNews = await getCollection('news');
const allNewsInLocale = allNews.filter((item) => isNewsItemInLocale(item, locale));
const someNewsItem = allNewsInLocale.at(0);
if (!someNewsItem) {
return null;
}
return allNewsInLocale.reduce((prev, curr) => (isBefore(curr, prev) ? curr : prev), someNewsItem);
}
export function isNewsItemInLocale(newsItem: CollectionEntry<'news'>, locale: Locale) {
const srcPath = newsItem.id;
const newsItemLocale = srcPath.slice(0, srcPath.indexOf('/'));
return newsItemLocale === locale;
}
function isBefore(item1: CollectionEntry<'news'>, item2: CollectionEntry<'news'>) {
const date1 = dateOf(item1);
const date2 = dateOf(item2);
if (!date2) {
return false;
}
if (!date1) {
return true;
}
return date1.valueOf() < date2.valueOf();
}
function dateOf(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;
}
|