mirror of
https://github.com/saicaca/fuwari.git
synced 2026-01-11 23:02:53 +01:00
feat: initial commit
(cherry picked from commit 44c4d7b9521fe449e61edc614446195861932f8c)
This commit is contained in:
32
src/utils/content-utils.ts
Normal file
32
src/utils/content-utils.ts
Normal 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]}));
|
||||
}
|
||||
Reference in New Issue
Block a user