Skip to content

Commit

Permalink
Merge pull request #124 from shandianchengzi/master
Browse files Browse the repository at this point in the history
fix(pages): 将已发布文章的时间排序代码挪到分页之前,解决分页后未排序的问题。
  • Loading branch information
mudongliang authored Jul 23, 2024
2 parents 74e9ce4 + d463151 commit 7133a92
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
8 changes: 1 addition & 7 deletions pages/src/layouts/Posts.astro
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,7 @@ const countData = getStatusCount(await getCollection("posts"));
}
<ul>
{
// 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 }) => (
<Card
id={id}
href={`/posts/${slug}/`}
Expand Down
14 changes: 12 additions & 2 deletions pages/src/utils/getSortedPosts.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { CollectionEntry } from "astro:content";

const getSortedPosts = (posts: CollectionEntry<"posts">[]) => {
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(
Expand All @@ -14,6 +22,8 @@ const getSortedPosts = (posts: CollectionEntry<"posts">[]) => {
// new Date(a.data.modDatetime ?? a.data.pubDatetime).getTime() / 1000
// )
// );

return posts;
};

export default getSortedPosts;

0 comments on commit 7133a92

Please sign in to comment.