diff options
| author | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-08-25 17:56:03 +0100 |
|---|---|---|
| committer | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-08-25 17:56:03 +0100 |
| commit | 40921a489269e84f6930eaab7df955c4afd5ce7f (patch) | |
| tree | b1287c92fc9427f1bae85813b25236d3f20a12b7 /src/lib | |
| parent | 7c31c4cf897fef56dd05bc6afd6fc405f026c5e2 (diff) | |
Adds an RSS feed with news items
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/newsUtils.ts | 19 |
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; } |
