summaryrefslogtreecommitdiff
path: root/src/lib/utils/posts.server.ts
blob: a9b7bb9cd7346002d037c316dc92c96e84b8c36c (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
import type { Post, PostMetadata } from './posts';

const postMetadataFiles = import.meta.glob<PostMetadata>('/src/posts/*.svx', {
    eager: true,
    import: 'metadata'
});

function getSlugFromPath(path: string): string {
    return path.split('/').pop()?.replace('.svx', '') ?? '';
}

export function getPosts(): Post[] {
    const posts: Post[] = [];

    for (const path in postMetadataFiles) {
        const metadata = postMetadataFiles[path];
        const slug = getSlugFromPath(path);

        posts.push({
            ...metadata,
            slug
        });
    }

    return posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
}