diff options
Diffstat (limited to 'src/lib/utils/posts.server.ts')
| -rw-r--r-- | src/lib/utils/posts.server.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lib/utils/posts.server.ts b/src/lib/utils/posts.server.ts new file mode 100644 index 0000000..a9b7bb9 --- /dev/null +++ b/src/lib/utils/posts.server.ts @@ -0,0 +1,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()); +} |
