From 360c9d5c1a35020fae7731e84f2a65d30e003617 Mon Sep 17 00:00:00 2001 From: Yufan Sheng Date: Wed, 16 Oct 2024 02:17:58 +0800 Subject: [PATCH] feat: dynamic feature posts. --- options.ts | 3 +- src/components/page/post/FeaturePosts.astro | 59 ++++++++++++++------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/options.ts b/options.ts index da1346c..d6ac9ad 100644 --- a/options.ts +++ b/options.ts @@ -46,7 +46,7 @@ const Options = z .readonly(), post: z.object({ sort: z.enum(['asc', 'desc']), - feature: z.array(z.string()).optional(), + feature: z.array(z.string()).min(3).optional(), category: z.array(z.string()).optional(), }), pagination: z.object({ @@ -163,7 +163,6 @@ const options: z.input = { assetPrefix: 'https://cat.yufan.me', post: { sort: 'desc', - feature: ['we-are-stranger', 'secret-of-boys-mind', 'my-darling'], category: ['article', 'think', 'gossip', 'coding'], }, pagination: { diff --git a/src/components/page/post/FeaturePosts.astro b/src/components/page/post/FeaturePosts.astro index 9e6c4ea..5d2f3c8 100644 --- a/src/components/page/post/FeaturePosts.astro +++ b/src/components/page/post/FeaturePosts.astro @@ -8,29 +8,52 @@ interface Props { } const { posts } = Astro.props; +const historicalPosts = (): Post[] => { + if (posts.length <= options.settings.pagination.posts * 2) { + // No need to reviews the historial posts. + return []; + } + + const now = new Date(); + const radical = now.getMonth() + now.getDate(); + const offset = Math.floor(posts.length / radical); + + if (offset >= 3) { + return [posts[radical * (offset - 2) - 1], posts[radical * (offset - 1) - 1], posts[radical * offset - 1]]; + } + return []; +}; const featurePosts = options.settings.post.feature ?? []; -const metas: Post[] = featurePosts - .map((slug) => posts.find((post) => post.slug === slug)) - .flatMap((post) => (post == null ? [] : [post])) - .slice(0, 3); + +const metas: Post[] = + featurePosts.length < 3 + ? historicalPosts() + : featurePosts + .map((slug) => posts.find((post) => post.slug === slug)) + .flatMap((post) => (post == null ? [] : [post])) + .slice(0, 3); --- -
-
-
-
- -
-
-
-
- +{ + metas.length === 3 && ( +
+
+
+
+
-
- +
+
+
+ +
+
+ +
+
-
-
+ ) +}