Feeds use max entries, and blog feed uses descriptions

This commit is contained in:
Joe Carstairs
2024-07-15 16:14:41 +01:00
parent ea96f40f7c
commit e7f41976f1
4 changed files with 44 additions and 11 deletions

View File

@@ -6,3 +6,11 @@
.h-feed .h-entry { .h-feed .h-entry {
margin-block-start: var(--spacing-block-xs); margin-block-start: var(--spacing-block-xs);
} }
.h-feed .full-feed-link {
text-align: end;
}
.h-feed :is(a.full-feed-link, .full-feed-link a)::after {
content: ' >'
}

View File

@@ -1,15 +1,21 @@
--- ---
import type { CollectionEntry } from 'astro:content'; import type { CollectionEntry } from 'astro:content';
import { getCollection } from 'astro:content'; import { getCollection } from 'astro:content';
import FormattedDate from './FormattedDate.astro';
export interface Props { export interface Props {
headingLevel?: 1 | 2 | 3 | 4 | 5 | 6, headingLevel?: 1 | 2 | 3 | 4 | 5 | 6,
hideAuthor?: boolean, hideAuthor?: boolean,
maxEntries?: number,
}; };
const { headingLevel = 2, hideAuthor = false } = Astro.props; const { headingLevel = 2, hideAuthor = false, maxEntries } = Astro.props;
const posts = (await getCollection('blog')).filter((post) => !post.data.hidden); 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 const distinctYears: number[] = posts
.map(post => post.data.pubDate.getFullYear()) .map(post => post.data.pubDate.getFullYear())
@@ -58,9 +64,16 @@ const canonicalBlogUrl = new URL('blog', Astro.site)
<ul> <ul>
{ posts.filter(matchesYear(year)).sort(sortByPubDateDescending).map(post => ( { posts.filter(matchesYear(year)).sort(sortByPubDateDescending).map(post => (
<li class="h-entry"> <li class="h-entry">
<a class="u-url p-name" href={`/blog/${post.slug}`}>{post.data.title}</a> <a class="u-url p-name" href={`/blog/${post.slug}`}>{post.data.title}</a>.
<Fragment set:html={post.data.description} />
Added: <FormattedDate date={post.data.pubDate} />
</li> </li>
)) } )) }
</ul> </ul>
)) } )) }
{ (maxEntries !== undefined && maxEntries < allPosts.length)
? <p class="full-feed-link"><a href="/blog">All blog posts</a></p>
: <></>
}
</section> </section>

View File

@@ -1,27 +1,34 @@
--- ---
import FormattedDate from '../components/FormattedDate.astro'; import FormattedDate from '../components/FormattedDate.astro';
import LINKS from '../data/links.ts'; import allLinks from '../data/links.ts';
type Link = (typeof allLinks)[number];
export interface Props { export interface Props {
headingLevel?: 1 | 2 | 3 | 4 | 5 | 6, headingLevel?: 1 | 2 | 3 | 4 | 5 | 6,
hideAuthor?: boolean, hideAuthor?: boolean,
maxEntries?: number,
}; };
const { headingLevel = 2, hideAuthor = false } = Astro.props; const { headingLevel = 2, hideAuthor = false, maxEntries } = Astro.props;
const headingElem = `h${headingLevel}`; const headingElem = `h${headingLevel}`;
const subHeadingElem = `h${headingLevel + 1}` const subHeadingElem = `h${headingLevel + 1}`
const distinctYears: string[] = LINKS const links: Link[] = maxEntries === undefined
? allLinks
: allLinks.sort(sortByDateAddedDescending).slice(0, maxEntries);
const distinctYears: string[] = links
.map(link => link.isoDateAdded.slice(0,4)) .map(link => link.isoDateAdded.slice(0,4))
.reduce<string[]>((acc, curr) => acc.includes(curr) ? acc : [...acc, curr], []) .reduce<string[]>((acc, curr) => acc.includes(curr) ? acc : [...acc, curr], [])
.sort((a, b) => Number.parseInt(b) - Number.parseInt(a)); .sort((a, b) => Number.parseInt(b) - Number.parseInt(a));
function matchesYear(year: string) { function matchesYear(year: string) {
return (link: (typeof LINKS)[number]) => link.isoDateAdded.slice(0,4) === year; return (link: (typeof links)[number]) => link.isoDateAdded.slice(0,4) === year;
} }
function sortByDateAddedDescending(link1: (typeof LINKS)[number], link2: (typeof LINKS)[number]) { function sortByDateAddedDescending(link1: (typeof links)[number], link2: (typeof links)[number]) {
const year1 = Number.parseInt(link1.isoDateAdded.slice(0, 4)); const year1 = Number.parseInt(link1.isoDateAdded.slice(0, 4));
const year2 = Number.parseInt(link2.isoDateAdded.slice(0, 4)); const year2 = Number.parseInt(link2.isoDateAdded.slice(0, 4));
const month1 = Number.parseInt(link1.isoDateAdded.slice(5, 7)); const month1 = Number.parseInt(link1.isoDateAdded.slice(5, 7));
@@ -65,7 +72,7 @@ const canonicalLinksUrl = new URL('links', Astro.site)
</${subHeadingElem}> </${subHeadingElem}>
`} /> `} />
<ul> <ul>
{ LINKS.filter(matchesYear(year)).sort(sortByDateAddedDescending).map(link => ( { links.filter(matchesYear(year)).sort(sortByDateAddedDescending).map(link => (
<li class="h-entry e-content"> <li class="h-entry e-content">
<a class="u-url p-name" href={link.href} set:html={link.title} />. <a class="u-url p-name" href={link.href} set:html={link.title} />.
<Fragment set:html={link.description} /> <Fragment set:html={link.description} />
@@ -74,4 +81,9 @@ const canonicalLinksUrl = new URL('links', Astro.site)
)) } )) }
</ul> </ul>
)) } )) }
{ (maxEntries !== undefined && maxEntries < allLinks.length)
? <p class="full-feed-link"><a href="/links">All links</a></p>
: <></>
}
</section> </section>

View File

@@ -8,6 +8,6 @@ import Page from '../layouts/Page.astro';
<Page title={SITE_TITLE} description={SITE_DESCRIPTION}> <Page title={SITE_TITLE} description={SITE_DESCRIPTION}>
<Me /> <Me />
<BlogFeed hideAuthor /> <BlogFeed hideAuthor maxEntries={3} />
<LinksFeed hideAuthor /> <LinksFeed hideAuthor maxEntries={5} />
</Page> </Page>