From d46315140c42d07db495dc203b20f0e3d655f40b Mon Sep 17 00:00:00 2001
From: shandianchengzi <1252402849@qq.com>
Date: Tue, 23 Jul 2024 22:35:01 +0800
Subject: [PATCH] =?UTF-8?q?fix(pages):=20=E5=B0=86=E5=B7=B2=E5=8F=91?=
=?UTF-8?q?=E5=B8=83=E6=96=87=E7=AB=A0=E7=9A=84=E6=97=B6=E9=97=B4=E6=8E=92?=
=?UTF-8?q?=E5=BA=8F=E4=BB=A3=E7=A0=81=E6=8C=AA=E5=88=B0=E5=88=86=E9=A1=B5?=
=?UTF-8?q?=E4=B9=8B=E5=89=8D=EF=BC=8C=E8=A7=A3=E5=86=B3=E5=88=86=E9=A1=B5?=
=?UTF-8?q?=E5=90=8E=E6=9C=AA=E6=8E=92=E5=BA=8F=E7=9A=84=E9=97=AE=E9=A2=98?=
=?UTF-8?q?=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pages/src/layouts/Posts.astro | 8 +-------
pages/src/utils/getSortedPosts.ts | 14 ++++++++++++--
2 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/pages/src/layouts/Posts.astro b/pages/src/layouts/Posts.astro
index ce318b6..b6c1220 100644
--- a/pages/src/layouts/Posts.astro
+++ b/pages/src/layouts/Posts.astro
@@ -61,13 +61,7 @@ const countData = getStatusCount(await getCollection("posts"));
}
{
- // sort by date
- paginatedPosts.sort((a, b) => {
- // 假设 published_date 是字符串形式如 '20240428'
- const dateA = parseInt(a.data.published_date.toString());
- const dateB = parseInt(b.data.published_date.toString());
- return dateB - dateA; // 降序排列,若需升序改为 dateA - dateB
- }).map(({ id, data, slug, body }) => (
+ paginatedPosts.map(({ id, data, slug, body }) => (
[]) => {
- return posts;
-
// TODO: implement sorting
+ // split all the posts by its status
+ const publishedPosts = posts.filter((post) => post.data.status === "published");
+ const otherPosts = posts.filter((post) => post.data.status !== "published");
+ // sort the published posts by its published date
+ publishedPosts.sort((a: CollectionEntry<"posts">, b: CollectionEntry<"posts">) => {
+ return new Date(b.data.published_date).getTime() - new Date(a.data.published_date).getTime();
+ });
+ // combine the sorted published posts with the other posts
+ posts = publishedPosts.concat(otherPosts);
+
// .sort(
// (a, b) =>
// Math.floor(
@@ -14,6 +22,8 @@ const getSortedPosts = (posts: CollectionEntry<"posts">[]) => {
// new Date(a.data.modDatetime ?? a.data.pubDatetime).getTime() / 1000
// )
// );
+
+ return posts;
};
export default getSortedPosts;