src/utils/post.ts 816 B raw
1
import type { CollectionEntry } from "astro:content";
2
3
export function sortMDByDate(posts: CollectionEntry<"post">[] = []) {
4
	return posts.sort(
5
		(a, b) =>
6
			new Date(b.data.publishDate).valueOf() -
7
			new Date(a.data.publishDate).valueOf(),
8
	);
9
}
10
11
export function getUniqueTags(posts: CollectionEntry<"post">[] = []) {
12
	const uniqueTags = new Set<string>();
13
	posts.forEach((post) => {
14
		post.data.tags.map((tag) => uniqueTags.add(tag));
15
	});
16
	return Array.from(uniqueTags);
17
}
18
19
export function getUniqueTagsWithCount(posts: CollectionEntry<"post">[] = []): {
20
	[key: string]: number;
21
} {
22
	return posts.reduce((prev, post) => {
23
		const runningTags: { [key: string]: number } = { ...prev };
24
		post.data.tags.forEach((tag) => {
25
			runningTags[tag] = (runningTags[tag] || 0) + 1;
26
		});
27
		return runningTags;
28
	}, {});
29
}