refactor: renamed md to posts, and plaintext to files

This commit is contained in:
Lewis Wynne 2026-03-27 19:11:57 +00:00
parent 20811f107b
commit e4052fc145
7 changed files with 13 additions and 13 deletions

42
www/src/lib/posts.ts Normal file
View file

@ -0,0 +1,42 @@
import type { CollectionEntry } from 'astro:content';
import { DEFAULT_CATEGORY } from './consts';
import { sortEntries } from './format';
export type Post = CollectionEntry<'posts'> & { body?: string };
export function getSlug(postId: string): string {
const parts = postId.split('/');
return parts[parts.length - 1];
}
export function resolveRelatedPosts<T extends { id: string }>(
slugs: string[],
allPosts: T[],
): T[] {
const bySlug = new Map(allPosts.map(p => [getSlug(p.id), p]));
return slugs.flatMap(s => bySlug.get(s) ?? []);
}
export function organizePostsByCategory(posts: Post[]): {
grouped: Record<string, Post[]>;
categories: string[];
} {
const grouped = posts.reduce((acc, post) => {
const category = post.data.category ?? DEFAULT_CATEGORY;
if (!acc[category]) acc[category] = [];
acc[category].push(post);
return acc;
}, {} as Record<string, Post[]>);
const categories = Object.keys(grouped).sort((a, b) => {
if (a === DEFAULT_CATEGORY) return -1;
if (b === DEFAULT_CATEGORY) return 1;
return a.localeCompare(b);
});
for (const category of categories) {
grouped[category] = sortEntries(grouped[category], p => p.data);
}
return { grouped, categories };
}