summaryrefslogtreecommitdiff
path: root/src/utils/micro.ts
blob: 7f06b4119693c2d25cefdd134f071ca110929a4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import type { CollectionEntry } from "astro:content";

export type MicroEntry = CollectionEntry<"micro">;

export function sortMicroEntries(entries: MicroEntry[]): MicroEntry[] {
	return entries.sort((a, b) => b.data.publishDate.getTime() - a.data.publishDate.getTime());
}

export async function getAllMicroPosts(): Promise<MicroEntry[]> {
	const { getCollection } = await import("astro:content");

	// Get only Pleroma micro posts
	try {
		const microPosts = await getCollection("micro");
		return sortMicroEntries(microPosts);
	} catch (error) {
		console.warn("Micro collection not available:", error);
		return [];
	}
}

/** Extract all tags from micro posts */
export function getAllMicroTags(entries: MicroEntry[]): string[] {
	return entries.flatMap((entry) => entry.data.tags ?? []);
}

/** Get unique tags from micro posts */
export function getUniqueMicroTags(entries: MicroEntry[]): string[] {
	return [...new Set(getAllMicroTags(entries))];
}

/** Get unique tags with counts from micro posts */
export function getUniqueMicroTagsWithCount(entries: MicroEntry[]): [string, number][] {
	return [
		...getAllMicroTags(entries).reduce(
			(acc, t) => acc.set(t, (acc.get(t) ?? 0) + 1),
			new Map<string, number>(),
		),
	].sort((a, b) => b[1] - a[1]);
}