feat(blog): 重构文章和笔记展示,优化分类和标签

This commit is contained in:
KazooTTT
2025-03-05 16:14:05 +08:00
parent 013ede32a4
commit e6a88b73af
20 changed files with 263 additions and 196 deletions

View File

@ -1,22 +1,61 @@
import type { AllItem, NoteItem, PostItem } from "@/types";
import { collectionDateSort } from "@/utils/date";
import { type CollectionEntry, getCollection } from "astro:content";
/**
* @description: 添加分类作为前缀
* @param {CollectionEntry} posts
*/
const addCategoryAsPrefix = (posts: CollectionEntry<"post" | "note">[]) => {
return posts.map((post) => {
return {
...post,
data: {
...post.data,
title: post.data?.category
? `[${post.data?.category}] ${post.data.title}`
: post.data.title,
},
};
});
};
export const getDateSortByCreateTime = (post: CollectionEntry<"post" | "note">) => {
return post.data.date ?? post.data.data_created ?? post.data.date_modified ?? new Date();
};
export const getDateSortByUpdateTime = (post: CollectionEntry<"post" | "note">) => {
return post.data.date_modified ?? post.data.date ?? post.data.data_created ?? new Date();
};
/** filter out draft posts based on the environment */
export async function getAllPosts(): Promise<CollectionEntry<"post">[]> {
return await getCollection("post", ({ data }) => {
export async function getAllPosts(): Promise<PostItem[]> {
const posts = await getCollection("post", ({ data }) => {
return import.meta.env.PROD ? !data.draft : true;
});
return (addCategoryAsPrefix(posts) as CollectionEntry<"post">[]).map((item) => {
return {
...item,
dateToCmp: getDateSortByCreateTime(item),
};
});
}
export async function getAllFixedToTopPosts(): Promise<CollectionEntry<"post">[]> {
return await getCollection("post", ({ data }) => {
return import.meta.env.PROD ? data.fixedToTop : false;
export async function getAllFixedToTopPosts() {
const posts = await getAllPosts();
return posts.filter((post) => post.data.fixedToTop);
}
export async function getAllNotes(): Promise<NoteItem[]> {
const notes = await getCollection("note");
return (addCategoryAsPrefix(notes) as NoteItem[]).map((item) => {
return { ...item, dateToCmp: getDateSortByCreateTime(item) };
});
}
export async function getAllCollectionPosts() {
const posts = await getAllPosts();
const notes = await getCollection("note");
const notes = await getAllNotes();
const allPosts = [...posts, ...notes];
const allPostsSortedByDate = allPosts.sort(collectionDateSort);
return allPostsSortedByDate;
@ -25,9 +64,9 @@ export async function getAllCollectionPosts() {
/** groups posts by year (based on option siteConfig.sortPostsByUpdatedDate), using the year as the key
* Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so.
*/
export function groupPostsByYear(posts: CollectionEntry<"post">[]) {
return posts.reduce<Record<string, CollectionEntry<"post">[]>>((acc, post) => {
const year = post.data.date.getFullYear();
export function groupPostsByYear(posts: AllItem[]) {
return posts.reduce<Record<string, AllItem[]>>((acc, post) => {
const year = getDateSortByCreateTime(post).getFullYear();
if (!acc[year]) {
acc[year] = [];
}
@ -39,21 +78,21 @@ export function groupPostsByYear(posts: CollectionEntry<"post">[]) {
/** returns all tags created from posts (inc duplicate tags)
* Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so.
* */
export function getAllTags(posts: CollectionEntry<"post">[]) {
export function getAllTags(posts: AllItem[]) {
return posts.flatMap((post) => [...post.data.tags]);
}
/** returns all unique tags created from posts
* Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so.
* */
export function getUniqueTags(posts: CollectionEntry<"post">[]) {
export function getUniqueTags(posts: AllItem[]) {
return [...new Set(getAllTags(posts))];
}
/** returns a count of each unique tag - [[tagName, count], ...]
* Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so.
* */
export function getUniqueTagsWithCount(posts: CollectionEntry<"post">[]): [string, number][] {
export function getUniqueTagsWithCount(posts: AllItem[]): [string, number][] {
return [
...getAllTags(posts).reduce(
(acc, t) => acc.set(t, (acc.get(t) ?? 0) + 1),
@ -63,19 +102,17 @@ export function getUniqueTagsWithCount(posts: CollectionEntry<"post">[]): [strin
}
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
export function getAllCategories(posts: Array<CollectionEntry<"post">>): string[] {
export function getAllCategories(posts: AllItem[]): string[] {
return posts.map((post) => post.data.category ?? "未分类");
}
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
export function getUniqueCategories(posts: Array<CollectionEntry<"post">>): string[] {
export function getUniqueCategories(posts: AllItem[]): string[] {
return [...new Set(getAllCategories(posts))];
}
/** Note: This function doesn't filter draft posts, pass it the result of getAllPosts above to do so. */
export function getUniqueCategoriesWithCount(
posts: Array<CollectionEntry<"post">>,
): Array<[string, number]> {
export function getUniqueCategoriesWithCount(posts: AllItem[]): Array<[string, number]> {
return [
...getAllCategories(posts).reduce(
(acc, t) => acc.set(t, (acc.get(t) || 0) + 1),