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