From 4762be11e37bcf155c17b188e9afd483be72aa43 Mon Sep 17 00:00:00 2001 From: shandianchengzi Date: Thu, 11 Jul 2024 20:26:39 +0800 Subject: [PATCH] =?UTF-8?q?posts:=20=E4=B8=BB=E9=A1=B5=E7=9A=84=E6=96=87?= =?UTF-8?q?=E7=AB=A0=E6=8E=92=E5=BA=8F=EF=BC=8C=E5=B9=B6=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=8F=91=E5=B8=83=E6=97=A5=E6=9C=9F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/src/components/Card.tsx | 11 +++++++++-- pages/src/layouts/Posts.astro | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pages/src/components/Card.tsx b/pages/src/components/Card.tsx index 5c42792..2c6ed1b 100644 --- a/pages/src/components/Card.tsx +++ b/pages/src/components/Card.tsx @@ -29,6 +29,11 @@ const parseBody = (body: string, href: string) => { return marked_content + end; } +// 定义一个函数,解析20240428格式为2024-04-28 +const parseDate = (date: string) => { + return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8); +} + export interface Props { id?: string; href?: string; @@ -42,6 +47,7 @@ export default function Card({ id, href, frontmatter, secHeading = true, body, p const { title, status, + published_date } = frontmatter; const headerProps = { @@ -76,6 +82,7 @@ export default function Card({ id, href, frontmatter, secHeading = true, body, p ) )} +

{parseDate(published_date.toString())}

{ status === 'collected' &&
{body && body.split(/\s/g).length} 词 @@ -87,8 +94,8 @@ export default function Card({ id, href, frontmatter, secHeading = true, body, p {/* */}
{publishCard && - // 去掉段后间隔 -
+ // 去掉段后间隔,设置max-width为100% +
{/* 注意需要my-0否则继承 */}

diff --git a/pages/src/layouts/Posts.astro b/pages/src/layouts/Posts.astro index b6c1220..ce318b6 100644 --- a/pages/src/layouts/Posts.astro +++ b/pages/src/layouts/Posts.astro @@ -61,7 +61,13 @@ const countData = getStatusCount(await getCollection("posts")); }

    { - paginatedPosts.map(({ id, data, slug, body }) => ( + // 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 }) => (