feat: initial commit

(cherry picked from commit 44c4d7b9521fe449e61edc614446195861932f8c)
This commit is contained in:
saicaca
2023-09-26 14:27:38 +08:00
parent 02b0a65314
commit 124843848f
58 changed files with 13083 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
import {getCollection} from "astro:content";
export async function getSortedPosts() {
const allBlogPosts = await getCollection("posts");
return allBlogPosts.sort((a, b) => {
const dateA = new Date(a.data.pubDate);
const dateB = new Date(b.data.pubDate);
return dateA > dateB ? -1 : 1;
});
}
export function getPostUrlBySlug(slug: string): string {
return `/posts/${slug}`;
}
export async function getTagList(): Promise<{ name: string; count: number }[]> {
const allBlogPosts = await getCollection("posts");
const countMap: { [key: string]: number } = {};
allBlogPosts.map((post) => {
post.data.tags.map((tag: string) => {
if (!countMap[tag]) countMap[tag] = 0;
countMap[tag]++;
})
});
// 获取对象的所有键并按字典顺序排序
const keys: string[] = Object.keys(countMap).sort();
// 使用排序后的键构建包含 key 和 value 的数组
return keys.map((key) => ({name: key, count: countMap[key]}));
}