summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib/utils/posts.ts63
-rw-r--r--src/routes/+page.svelte8
-rw-r--r--src/routes/posts/[slug]/+page.ts7
3 files changed, 33 insertions, 45 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();
}
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index e304a68..b2c92b4 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -1,8 +1,8 @@
<script lang="ts">
+ import type { PageData } from "./$types";
import PostCard from "$lib/components/PostCard.svelte";
- import { getPosts } from "$lib/utils/posts";
- const posts = getPosts();
+ let { data }: { data: PageData } = $props();
</script>
<svelte:head>
@@ -14,10 +14,10 @@
</svelte:head>
<section>
- {#if posts.length === 0}
+ {#if data.posts.length === 0}
<p>No posts.</p>
{:else}
- {#each posts as post}
+ {#each data.posts as post}
<PostCard {post} />
{/each}
{/if}
diff --git a/src/routes/posts/[slug]/+page.ts b/src/routes/posts/[slug]/+page.ts
index d0776db..8d173ed 100644
--- a/src/routes/posts/[slug]/+page.ts
+++ b/src/routes/posts/[slug]/+page.ts
@@ -1,4 +1,4 @@
-import { getPost, getPosts } from '$lib/utils/posts';
+import { getPost, getPostSlugs } from '$lib/utils/posts';
import { error } from '@sveltejs/kit';
import type { PageLoad } from './$types';
@@ -6,13 +6,12 @@ export const prerender = true;
// Generate all post routes at build time
export function entries() {
- const posts = getPosts();
- return posts.map((post) => ({ slug: post.slug }));
+ return getPostSlugs().map((slug) => ({ slug }));
}
export const load: PageLoad = async ({ params }) => {
const slug = params.slug;
- const post = getPost(slug);
+ const post = await getPost(slug);
if (!post) {
error(404, `Post not found: ${slug}`);