feat: 新增置顶文章

This commit is contained in:
KazooTTT
2025-02-14 18:59:28 +08:00
parent c7b012a891
commit 3af73ab320
5 changed files with 40 additions and 2 deletions

View File

@ -27,6 +27,16 @@ const socialLinks: {
link: "https://x.com/kazoottt", link: "https://x.com/kazoottt",
name: "mdi:twitter", name: "mdi:twitter",
}, },
{
friendlyName: "Bilibili",
link: "https://space.bilibili.com/391236013",
name: "mdi:video",
},
{
friendlyName: "YouTube",
link: "https://www.youtube.com/@kazoottt4718",
name: "mdi:youtube",
},
{ {
friendlyName: "Photo", friendlyName: "Photo",
link: "https://unsplash.com/@kazoottt", link: "https://unsplash.com/@kazoottt",
@ -35,7 +45,7 @@ const socialLinks: {
{ {
friendlyName: "汇总", friendlyName: "汇总",
link: "https://bento.me/KazooTTT", link: "https://bento.me/KazooTTT",
name: "mdi:link", name: "mdi:open-in-new",
}, },
]; ];
--- ---

View File

@ -18,6 +18,7 @@ const { as: Tag = "div", post, withDesc = false } = Astro.props;
<Tag> <Tag>
{post.data.draft && <span class="text-red-500">(Draft) </span>} {post.data.draft && <span class="text-red-500">(Draft) </span>}
<a class="cactus-link" data-astro-prefetch href={`/posts/${post.id}/`}> <a class="cactus-link" data-astro-prefetch href={`/posts/${post.id}/`}>
{post.data.fixedToTop && <span class="text-accent-2">*</span>}
{post.data.title} {post.data.title}
</a> </a>
</Tag> </Tag>

View File

@ -21,6 +21,7 @@ const post = defineCollection({
date_modified: z.date().optional(), date_modified: z.date().optional(),
data_created: z.date().optional(), data_created: z.date().optional(),
category: z.string().optional().nullable(), category: z.string().optional().nullable(),
fixedToTop: z.boolean().optional().default(false),
}), }),
}); });

View File

@ -8,6 +8,12 @@ export async function getAllPosts(): Promise<CollectionEntry<"post">[]> {
}); });
} }
export async function getAllFixedToTopPosts(): Promise<CollectionEntry<"post">[]> {
return await getCollection("post", ({ data }) => {
return import.meta.env.PROD ? data.fixedToTop : false;
});
}
export async function getAllCollectionPosts() { export async function getAllCollectionPosts() {
const posts = await getAllPosts(); const posts = await getAllPosts();
const notes = await getCollection("note"); const notes = await getCollection("note");

View File

@ -3,7 +3,7 @@ import { type CollectionEntry, getCollection } from "astro:content";
import SocialList from "@/components/SocialList.astro"; import SocialList from "@/components/SocialList.astro";
import PostPreview from "@/components/blog/PostPreview.astro"; import PostPreview from "@/components/blog/PostPreview.astro";
import Note from "@/components/note/Note.astro"; import Note from "@/components/note/Note.astro";
import { getAllPosts } from "@/data/post"; import { getAllFixedToTopPosts, getAllPosts } from "@/data/post";
import PageLayout from "@/layouts/Base.astro"; import PageLayout from "@/layouts/Base.astro";
import { collectionDateSort } from "@/utils/date"; import { collectionDateSort } from "@/utils/date";
@ -14,6 +14,12 @@ const allPostsByDate = allPosts
.sort(collectionDateSort) .sort(collectionDateSort)
.slice(0, MAX_POSTS) as CollectionEntry<"post">[]; .slice(0, MAX_POSTS) as CollectionEntry<"post">[];
// Fixed to top Posts
const allFixedToTopPosts = await getAllFixedToTopPosts();
const allFixedToTopPostsByDate = allFixedToTopPosts
.sort(collectionDateSort)
.slice(0, MAX_POSTS) as CollectionEntry<"post">[];
// Notes // Notes
const MAX_NOTES = 6; const MAX_NOTES = 6;
const allNotes = await getCollection("note"); const allNotes = await getCollection("note");
@ -26,6 +32,20 @@ const latestNotes = allNotes.sort(collectionDateSort).slice(0, MAX_NOTES);
<p class="mb-4">TODO ...</p> <p class="mb-4">TODO ...</p>
<SocialList /> <SocialList />
</section> </section>
<section class="mt-16">
<h2 class="title text-accent mb-6 text-xl"><a href="/posts/">置顶文章</a></h2>
<ul class="space-y-4" role="list">
{
allFixedToTopPostsByDate.map((p) => (
<li class="grid gap-2 sm:grid-cols-[auto_1fr]">
<PostPreview post={p} />
</li>
))
}
</ul>
</section>
<section class="mt-16"> <section class="mt-16">
<h2 class="title text-accent mb-6 text-xl"><a href="/posts/">Posts</a></h2> <h2 class="title text-accent mb-6 text-xl"><a href="/posts/">Posts</a></h2>
<ul class="space-y-4" role="list"> <ul class="space-y-4" role="list">