Skip to content

Commit dd5ef41

Browse files
authored
Merge pull request #88 from JingkaiTang/fix/draft-excluded-from-prev-next
Fix: 草稿文章不应出现在上一篇/下一篇导航中
2 parents 2ac1f27 + 4686406 commit dd5ef41

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

src/pages/writing/[slug].astro

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,32 @@ export async function getStaticPaths() {
99
// Drafts are still hidden from feeds/lists elsewhere.
1010
const posts = await getCollection('writing');
1111
12+
// Filter out drafts for prev/next navigation
13+
// Drafts should not appear in navigation and should not be linked from published posts
14+
const publishedPosts = posts.filter(p => !p.data.draft);
15+
1216
// Sort posts by pubDate (newest first) for prev/next navigation
13-
const sortedPosts = posts.sort((a, b) => b.data.pubDate.getTime() - a.data.pubDate.getTime());
17+
const sortedPosts = publishedPosts.sort((a, b) => b.data.pubDate.getTime() - a.data.pubDate.getTime());
18+
19+
// Build a map: postId -> index in sortedPosts (O(1) lookup)
20+
const postIndexMap = new Map(
21+
sortedPosts.map((post, index) => [post.id, index])
22+
);
1423
15-
return posts.map((p, index) => ({
16-
params: { slug: p.slug.replace(/\/index$/, '') },
17-
props: {
18-
post: p,
19-
prevPost: sortedPosts[index - 1] || null, // Newer post
20-
nextPost: sortedPosts[index + 1] || null, // Older post
21-
},
22-
}));
24+
return posts.map((p) => {
25+
// Find the index of this post in the sorted published posts
26+
const publishedIndex = postIndexMap.get(p.id);
27+
28+
return {
29+
params: { slug: p.slug.replace(/\/index$/, '') },
30+
props: {
31+
post: p,
32+
// Only show prev/next for published posts
33+
prevPost: publishedIndex !== undefined ? (sortedPosts[publishedIndex - 1] || null) : null, // Newer post
34+
nextPost: publishedIndex !== undefined ? (sortedPosts[publishedIndex + 1] || null) : null, // Older post
35+
},
36+
};
37+
});
2338
}
2439
2540
const { post, prevPost, nextPost } = Astro.props;

0 commit comments

Comments
 (0)