Skip to content

Commit c0422bf

Browse files
committed
Related content fine-tune
1 parent a6c7c73 commit c0422bf

File tree

4 files changed

+33
-139
lines changed

4 files changed

+33
-139
lines changed

src/components/ReadNext.astro

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
---
22
import PostCard from './PostCard.astro';
3-
import ReadNextCard from './ReadNextCard.astro';
43
import type { CollectionEntry } from 'astro:content';
54
65
interface Props {
76
currentSlug: string;
8-
tags: string[];
97
relatedPosts: CollectionEntry<'posts'>[];
108
prevPost?: CollectionEntry<'posts'> | null;
119
nextPost?: CollectionEntry<'posts'> | null;
1210
}
1311
14-
const { currentSlug, tags, relatedPosts, prevPost, nextPost } = Astro.props;
15-
const showRelatedPosts = relatedPosts.length > 1;
16-
const hasContent = showRelatedPosts || prevPost || nextPost;
12+
const { currentSlug, relatedPosts, prevPost, nextPost } = Astro.props;
13+
14+
const MAX_CARDS = 3;
15+
const seen = new Set<string>([currentSlug]);
16+
const cards: CollectionEntry<'posts'>[] = [];
17+
18+
for (const post of relatedPosts) {
19+
if (cards.length >= MAX_CARDS) break;
20+
if (!seen.has(post.id)) {
21+
seen.add(post.id);
22+
cards.push(post);
23+
}
24+
}
25+
26+
for (const post of [prevPost, nextPost]) {
27+
if (cards.length >= MAX_CARDS) break;
28+
if (post && !seen.has(post.id)) {
29+
seen.add(post.id);
30+
cards.push(post);
31+
}
32+
}
1733
---
1834

1935
{
20-
hasContent && (
36+
cards.length > 0 && (
2137
<aside class="read-next">
2238
<div class="read-next-inner">
2339
<h3 class="read-next-heading">Read Next</h3>
2440
<div class="read-next-feed">
25-
{showRelatedPosts && (
26-
<ReadNextCard currentSlug={currentSlug} tags={tags} relatedPosts={relatedPosts} />
27-
)}
28-
{prevPost && <PostCard post={prevPost} />}
29-
{nextPost && <PostCard post={nextPost} />}
41+
{cards.map(post => <PostCard post={post} />)}
3042
</div>
3143
</div>
3244
</aside>

src/components/ReadNextCard.astro

Lines changed: 0 additions & 124 deletions
This file was deleted.

src/layouts/PostLayout.astro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ const primaryTag = tags?.[0];
8989

9090
<ReadNext
9191
currentSlug={post.id}
92-
tags={tags || []}
9392
relatedPosts={relatedPosts}
9493
prevPost={prevPost}
9594
nextPost={nextPost}

src/pages/posts/[...slug].astro

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ export async function getStaticPaths() {
1313
return sorted.map((post, index) => {
1414
const prev = index > 0 ? sorted[index - 1] : null;
1515
const next = index < sorted.length - 1 ? sorted[index + 1] : null;
16-
const primaryTag = post.data.tags?.[0];
17-
const relatedPosts = primaryTag
18-
? sorted.filter(p => p.data.tags?.includes(primaryTag) && p.id !== post.id)
16+
const currentTags = post.data.tags ?? [];
17+
const relatedPosts = currentTags.length > 0
18+
? sorted
19+
.filter(p => p.id !== post.id && p.data.tags?.some(t => currentTags.includes(t)))
20+
.map(p => ({
21+
post: p,
22+
overlap: p.data.tags!.filter(t => currentTags.includes(t)).length,
23+
}))
24+
.sort((a, b) => b.overlap - a.overlap || b.post.data.date.valueOf() - a.post.data.date.valueOf())
25+
.map(r => r.post)
1926
: [];
2027
const authorData = post.data.author
2128
.map(authorId => authors.find(a => a.data.id === authorId)?.data)

0 commit comments

Comments
 (0)