summaryrefslogtreecommitdiff
path: root/src/lib/utils/posts.ts
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-02-09 00:17:47 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-02-09 00:17:47 +0300
commit3fe271f400325d6e792304726cfd984c4bd2176d (patch)
tree27dafec9ff2dedafac47e149de554a3117d9d6df /src/lib/utils/posts.ts
parent416768216b00116a5d37c45279d740de09838d19 (diff)
better optimized builds?
Diffstat (limited to 'src/lib/utils/posts.ts')
-rw-r--r--src/lib/utils/posts.ts63
1 files changed, 26 insertions, 37 deletions
diff --git a/src/lib/utils/posts.ts b/src/lib/utils/posts.ts
index cc05c83..d0eca68 100644
--- a/src/lib/utils/posts.ts
+++ b/src/lib/utils/posts.ts
@@ -1,51 +1,40 @@
import type { Component } from 'svelte';
export interface PostMetadata {
- title: string;
- date: string;
- description: string;
- tags?: string[];
+ title: string;
+ date: string;
+ description: string;
+ tags?: string[];
}
export interface Post extends PostMetadata {
- slug: string;
+ slug: string;
}
export interface PostModule {
- default: Component;
- metadata: PostMetadata;
+ default: Component;
+ metadata: PostMetadata;
}
-// Use Vite's glob import to get all .svx files from posts directory
-const postFiles = import.meta.glob<PostModule>('/src/posts/*.svx', { eager: true });
-
-/**
- * Get all posts, sorted by date (newest first)
- */
-export function getPosts(): Post[] {
- const posts: Post[] = [];
-
- for (const path in postFiles) {
- const file = postFiles[path];
- // Extract slug from path: /src/posts/hello-world.svx -> hello-world
- const slug = path.split('/').pop()?.replace('.svx', '') ?? '';
-
- if (file.metadata) {
- posts.push({
- ...file.metadata,
- slug
- });
- }
- }
-
- // Sort by date, newest first
- return posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
+const postLoaders = import.meta.glob<PostModule>('/src/posts/*.svx');
+
+function getSlugFromPath(path: string): string {
+ return path.split('/').pop()?.replace('.svx', '') ?? '';
+}
+
+export function getPostSlugs(): string[] {
+ return Object.keys(postLoaders)
+ .map((path) => getSlugFromPath(path))
+ .sort();
}
-/**
- * Get a single post by slug
- */
-export function getPost(slug: string): PostModule | undefined {
- const path = `/src/posts/${slug}.svx`;
- return postFiles[path];
+export async function getPost(slug: string): Promise<PostModule | undefined> {
+ const path = `/src/posts/${slug}.svx`;
+ const loader = postLoaders[path];
+
+ if (!loader) {
+ return undefined;
+ }
+
+ return loader();
}