--- import { getCollection } from "astro:content"; import type { CollectionEntry } from "astro:content"; import type { Lang } from "@/i18n"; interface Props { post: CollectionEntry<"blog">; lang: Lang; } const { post, lang } = Astro.props; const allPosts = await getCollection("blog"); // 只保留当前语言 const sameLangPosts = allPosts.filter((p) => { const [postLang] = p.id.split("/"); return postLang === lang; }); // 按日期倒序:越新越靠前 const sortedPosts = [...sameLangPosts].sort( (a, b) => new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime(), ); // 找当前文章位置 const currentIndex = sortedPosts.findIndex((p) => p.id === post.id); // 更旧的一篇 const prevPost = currentIndex < sortedPosts.length - 1 ? sortedPosts[currentIndex + 1] : null; // 更新的一篇 const nextPost = currentIndex > 0 ? sortedPosts[currentIndex - 1] : null; const getSlugFromId = (id: string) => id.split("/").slice(1).join("/"); const prevSlug = prevPost ? getSlugFromId(prevPost.id) : null; const nextSlug = nextPost ? getSlugFromId(nextPost.id) : null; --- { nextPost && nextSlug && ( {lang === "zh" ? "上一篇" : "Previous"} {nextPost.data.title} ) } { prevPost && prevSlug && ( {lang === "zh" ? "下一篇" : "Next"} {prevPost.data.title} ) }