|
| 1 | +--- |
| 2 | +import {getCollection} from 'astro:content'; |
| 3 | +import {asLocale, slugToPostLink, useTranslations} from '../i18n/utils'; |
| 4 | +import {Image} from 'astro:assets'; |
| 5 | +import {mdExcerpt, pictureWidths} from '../components/utils'; |
| 6 | +import Section from '../components/Section.astro'; |
| 7 | +import Layout from '../layouts/Layout.astro'; |
| 8 | +
|
| 9 | +const t = useTranslations(Astro.currentLocale); |
| 10 | +
|
| 11 | +const pageLang = Astro.currentLocale; |
| 12 | +
|
| 13 | +const pages = await getCollection('posts', (entry) => { |
| 14 | + const [lang, ...slug] = entry.slug.split('/'); |
| 15 | + if (lang !== pageLang) { |
| 16 | + return undefined; |
| 17 | + } |
| 18 | + return entry; |
| 19 | +}); |
| 20 | +
|
| 21 | +pages.sort((a, b) => b.data.date.getTime() - a.data.date.getTime()); |
| 22 | +
|
| 23 | +const paths = pages.map((page) => { |
| 24 | + const [lang, ...slug] = page.slug.split('/'); |
| 25 | + if (slug.length === 0) { |
| 26 | + throw new Error(`Empty slug: ${page.slug}`); |
| 27 | + } |
| 28 | + return {params: {lang: asLocale(lang), slug: slug.join('/')}, props: page}; |
| 29 | +}); |
| 30 | +
|
| 31 | +const newsItems: Array<{ |
| 32 | + title: string; |
| 33 | + date: string; |
| 34 | + image: ImageMetadata; |
| 35 | + abstract: string; |
| 36 | + link: string; |
| 37 | +}> = paths.map((page) => { |
| 38 | + return { |
| 39 | + title: page.props.data.title, |
| 40 | + date: page.props.data.date.toLocaleDateString(pageLang, { |
| 41 | + year: 'numeric', |
| 42 | + month: 'long', |
| 43 | + day: 'numeric', |
| 44 | + }), |
| 45 | + image: page.props.data.image, |
| 46 | + abstract: mdExcerpt(page.props.body, 200), |
| 47 | + link: slugToPostLink(page.params.lang, page.params.slug), |
| 48 | + }; |
| 49 | +}); |
| 50 | +--- |
| 51 | + |
| 52 | +<Layout title={t('news.allPosts')} description={t('news.subtitle')}> |
| 53 | + <Section> |
| 54 | + <!-- Title --> |
| 55 | + <div class="mx-auto mb-10 max-w-2xl text-center lg:mb-14"> |
| 56 | + <h1 class="text-3xl font-bold md:text-4xl md:leading-tight dark:text-white">{t('news.allPosts')}</h1> |
| 57 | + <p class="mt-1 text-gray-600 dark:text-neutral-400"> |
| 58 | + {t('news.subtitle')} |
| 59 | + </p> |
| 60 | + </div> |
| 61 | + <!-- End Title --> |
| 62 | + |
| 63 | + <!-- Grid --> |
| 64 | + <div class="grid gap-6 sm:grid-cols-2 lg:grid-cols-3"> |
| 65 | + <!-- Cards --> |
| 66 | + { |
| 67 | + newsItems.map((news) => ( |
| 68 | + <a |
| 69 | + class="group flex h-full flex-col rounded-xl border border-gray-200 p-5 transition duration-300 hover:border-transparent hover:shadow-lg focus:border-transparent focus:shadow-lg focus:outline-hidden dark:border-neutral-700 dark:hover:border-transparent dark:hover:shadow-black/40 dark:focus:border-transparent dark:focus:shadow-black/40" |
| 70 | + href={news.link} |
| 71 | + > |
| 72 | + <Image |
| 73 | + class="aspect-video w-full rounded-xl object-cover" |
| 74 | + src={news.image} |
| 75 | + alt={news.title} |
| 76 | + widths={pictureWidths()} |
| 77 | + /> |
| 78 | + <div class="my-6"> |
| 79 | + <h3 class="text-xl font-semibold text-gray-800 dark:text-neutral-300 dark:group-hover:text-white"> |
| 80 | + {news.title} |
| 81 | + </h3> |
| 82 | + <p class="mt-5 text-gray-600 dark:text-neutral-400">{news.abstract}</p> |
| 83 | + </div> |
| 84 | + <div class="mt-auto flex items-center gap-x-3"> |
| 85 | + <div> |
| 86 | + <h5 class="text-sm text-gray-800 dark:text-neutral-200">{news.date}</h5> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + </a> |
| 90 | + )) |
| 91 | + } |
| 92 | + <!-- End Cards --> |
| 93 | + </div> |
| 94 | + <!-- End Grid --> |
| 95 | + </Section> |
| 96 | +</Layout> |
0 commit comments