eratic / sites / fav / src / lib / entries.ts
172 words
 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
41
42
43
44
45
46
47
import { getCollection, type CollectionEntry } from 'astro:content';
import { KIND_PLURAL, type Kind } from '../content.config';

export type Fav = CollectionEntry<'favs'>;

function isPublished(data: { draft: boolean; pubDate: Date }): boolean {
	if (import.meta.env.DEV) return true;
	return !data.draft && data.pubDate.getTime() <= Date.now();
}

/** Everything, newest first. */
export async function allFavs(): Promise<Fav[]> {
	const favs = await getCollection('favs');
	return favs
		.filter((entry) => isPublished(entry.data))
		.sort((a, b) => b.data.pubDate.getTime() - a.data.pubDate.getTime());
}

export function favPath(entry: { id: string }): string {
	return `/${entry.id}/`;
}

/** "Book · Hermann Hesse · 1943" — whichever parts exist. */
export function favKicker(entry: Fav): string {
	const kind = entry.data.kind;
	const label = kind.charAt(0).toUpperCase() + kind.slice(1);
	return [label, entry.data.creator, entry.data.year?.toString()]
		.filter((part): part is string => Boolean(part))
		.join(' · ');
}

export function kindPath(kind: Kind): string {
	return `/${KIND_PLURAL[kind]}/`;
}

export async function feedItems() {
	const favs = await allFavs();
	return favs.map((entry) => ({
		path: favPath(entry),
		title: entry.data.title,
		summary: entry.data.summary,
		pubDate: entry.data.pubDate,
		updatedDate: entry.data.updatedDate,
		tags: [...entry.data.tags, entry.data.kind],
		content: (entry as { rendered?: { html?: string } }).rendered?.html,
	}));
}

static mirror of HEAD · about · clone: git clone https://git.makefuture.ro/eratic.git