summaryrefslogtreecommitdiff
path: root/src/lib/utils/posts.ts
diff options
context:
space:
mode:
authorBerke Güzel <wenekar1@gmail.com>2026-01-29 23:56:21 +0300
committerBerke Güzel <wenekar1@gmail.com>2026-01-29 23:56:21 +0300
commit35556dca71eafdac4eb5d2fe781ba39687d0b058 (patch)
treed85489336dce549b08d385c45cec6ae0678b211b /src/lib/utils/posts.ts
parent292d9dd4241ace94bfaf50827dcedfbd40de8032 (diff)
initial commit
Diffstat (limited to 'src/lib/utils/posts.ts')
-rw-r--r--src/lib/utils/posts.ts51
1 files changed, 51 insertions, 0 deletions
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<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());
+}
+
+/**
+ * Get a single post by slug
+ */
+export function getPost(slug: string): PostModule | undefined {
+ const path = `/src/posts/${slug}.svx`;
+ return postFiles[path];
+}