--- import type { CollectionEntry } from 'astro:content'; import { getCollection } from 'astro:content'; import FormattedDate from './FormattedDate.astro'; export interface Props { headingLevel?: 1 | 2 | 3 | 4 | 5 | 6, hideAuthor?: boolean, maxEntries?: number, }; const { headingLevel = 2, hideAuthor = false, maxEntries } = Astro.props; const allPosts = (await getCollection('blog')).filter((post) => !post.data.hidden); const posts = maxEntries === undefined ? allPosts : allPosts.sort(sortByPubDateDescending).slice(0, maxEntries); const distinctYears: number[] = posts .map(post => post.data.pubDate.getFullYear()) .reduce((acc, curr) => acc.includes(curr) ? acc : [...acc, curr], []) .sort((a, b) => b - a); function matchesYear(year: number) { return (post: CollectionEntry<'blog'>) => post.data.pubDate.getFullYear() === year; } function sortByPubDateDescending(post1: CollectionEntry<'blog'>, post2: CollectionEntry<'blog'>) { const date1 = post1.data.pubDate.getMilliseconds(); const date2 = post2.data.pubDate.getMilliseconds(); return date2 - date1; } const headingElem = `h${headingLevel}`; const subHeadingElem = `h${headingLevel + 1}` const canonicalBlogUrl = new URL('blog', Astro.site) ---
My blog `} /> { distinctYears.map(year => ( ${year} `} />
    { posts.filter(matchesYear(year)).sort(sortByPubDateDescending).map(post => (
  • {post.data.title}. Added:
  • )) }
)) } { (maxEntries !== undefined && maxEntries < allPosts.length) ? : <> }