From 35556dca71eafdac4eb5d2fe781ba39687d0b058 Mon Sep 17 00:00:00 2001 From: Berke Güzel Date: Thu, 29 Jan 2026 23:56:21 +0300 Subject: initial commit --- src/lib/utils/posts.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/lib/utils/posts.ts (limited to 'src/lib/utils') diff --git a/src/lib/utils/posts.ts b/src/lib/utils/posts.ts new file mode 100644 index 0000000..cc05c83 --- /dev/null +++ b/src/lib/utils/posts.ts @@ -0,0 +1,51 @@ +import type { Component } from 'svelte'; + +export interface PostMetadata { + title: string; + date: string; + description: string; + tags?: string[]; +} + +export interface Post extends PostMetadata { + slug: string; +} + +export interface PostModule { + default: Component; + metadata: PostMetadata; +} + +// Use Vite's glob import to get all .svx files from posts directory +const postFiles = import.meta.glob('/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()); +} + +/** + * Get a single post by slug + */ +export function getPost(slug: string): PostModule | undefined { + const path = `/src/posts/${slug}.svx`; + return postFiles[path]; +} -- cgit v1.2.3