From 261d89ebed13691e72312f44339063fee93130ac Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Sat, 30 Mar 2024 16:32:17 +0000 Subject: Astro initial commit --- src/components/BlogFeed.astro | 76 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/components/BlogFeed.astro (limited to 'src/components/BlogFeed.astro') diff --git a/src/components/BlogFeed.astro b/src/components/BlogFeed.astro new file mode 100644 index 0000000..d785524 --- /dev/null +++ b/src/components/BlogFeed.astro @@ -0,0 +1,76 @@ +--- +import type { CollectionEntry } from 'astro:content'; +import { getCollection } from 'astro:content'; + +export interface Props { + headingLevel?: 1 | 2 | 3 | 4 | 5 | 6, + hideAuthor?: boolean, +}; + +const { headingLevel = 2, hideAuthor = false } = Astro.props; + +const posts = (await getCollection('blog')); + +const distinctYears: number[] = posts + .map(post => post.data.pubDate.year) + .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.year === year; +} + +function sortByPubDateDescending(post1: CollectionEntry<'blog'>, post2: CollectionEntry<'blog'>) { + const year1 = post1.data.pubDate.year; + const year2 = post2.data.pubDate.year; + const month1 = post1.data.pubDate.month; + const month2 = post2.data.pubDate.month; + const day1 = post1.data.pubDate.day; + const day2 = post2.data.pubDate.day; + + if (year1 !== year2) { + return year2 - year1; + } else if (month1 !== month2) { + return month2 - month1; + } else { + return day2 - day1; + } +} + +const headingElem = `h${headingLevel}`; + +const canonicalUrl = new URL(Astro.url.pathname, Astro.site) +--- + +
+ + My blog + + `} /> + + + +
    + { distinctYears.map(year => ( +
  • + {year} +
      + { posts.filter(matchesYear(year)).sort(sortByPubDateDescending).map(post => ( +
    • + {post.data.title} +
    • + )) } +
    +
  • + )) } +
+
\ No newline at end of file -- cgit v1.2.3