SanYeCao-blog/src/components/Posts/PostList.astro

78 lines
1.6 KiB
Text
Raw Normal View History

2026-03-24 17:33:01 +00:00
---
2026-03-24 18:01:45 +00:00
import { getCollection } from "astro:content";
2026-03-24 17:33:01 +00:00
import PostItem from "./PostItem.astro";
2026-03-25 08:43:02 +00:00
import { getLangFromUrl } from "@/i18n";
2026-03-24 17:33:01 +00:00
2026-03-24 22:50:33 +00:00
const lang = getLangFromUrl(Astro.url);
2026-03-24 18:01:45 +00:00
const allPosts = await getCollection("blog");
2026-03-25 08:43:02 +00:00
2026-03-25 10:38:01 +00:00
const sortedPosts = [...allPosts].sort(
(a, b) =>
new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime(),
);
2026-03-29 19:23:23 +00:00
const filteredPosts = sortedPosts.filter((post: any) => {
const postLang = post.id.split("/")[0];
return postLang === lang;
});
const latestPosts = filteredPosts.slice(0, 5);
2026-03-24 17:33:01 +00:00
---
<ul>
2026-03-24 18:01:45 +00:00
{
2026-03-25 10:38:01 +00:00
latestPosts.map((post: any) => {
2026-03-24 18:01:45 +00:00
const formattedDate = new Date(post.data.pubDate)
.toISOString()
.split("T")[0];
2026-03-24 17:33:01 +00:00
2026-03-29 19:23:23 +00:00
const [postLang, ...slugParts] = post.id.split("/");
const slug = slugParts.join("/");
2026-03-24 18:01:45 +00:00
return (
<PostItem
2026-03-29 19:23:23 +00:00
url={`/${postLang}/posts/${slug}/`}
postPath={`/posts/${slug}/`}
2026-03-24 18:01:45 +00:00
title={post.data.title}
description={post.data.description}
2026-03-24 18:01:45 +00:00
date={formattedDate}
2026-03-24 23:40:19 +00:00
img={post.data.image.url}
2026-03-24 18:01:45 +00:00
/>
);
})
}
2026-03-24 17:33:01 +00:00
</ul>
2026-03-24 23:40:19 +00:00
2026-03-25 10:38:01 +00:00
<div class="more-link-wrap">
<a href={`/${lang}/timeline`} class="more-link">
{lang === "zh" ? ">>>更多" : ">>>More"}
</a>
</div>
2026-03-24 23:40:19 +00:00
<style>
ul {
margin: 0;
padding-left: 0;
}
2026-03-25 10:38:01 +00:00
.more-link-wrap {
margin-top: 1rem;
text-align: right;
}
.more-link {
color: #6f8090;
font-weight: 700;
text-decoration: none;
font-size: 1.14rem;
}
.more-link:hover {
text-decoration: underline;
}
:global(.dark) .more-link {
color: #aab7c4;
}
2026-03-24 23:40:19 +00:00
</style>