diff --git a/src/routes/archive/+page.server.ts b/src/routes/archive/+page.server.ts new file mode 100644 index 0000000..056ac0c --- /dev/null +++ b/src/routes/archive/+page.server.ts @@ -0,0 +1,44 @@ +import type { PageServerLoad } from './$types'; +import { error } from '@sveltejs/kit'; + +export const load: PageServerLoad = async ({ platform }) => { + const db = platform?.env?.DB; + if (!db) { + throw error(500, 'Database connection not found'); + } + + try { + // Fetch all posts, ordered by published_at descending + const result = await db.prepare(` + SELECT slug, title, published_at + FROM posts + ORDER BY published_at DESC + `).all(); + + const posts = result.results as Array<{ slug: string; title: string; published_at: string }>; + + // Group posts by year + const archives: Record = {}; + + posts.forEach(post => { + const date = new Date(post.published_at); + const year = date.getFullYear().toString(); + + if (!archives[year]) { + archives[year] = []; + } + archives[year].push(post); + }); + + // Sort years descending + const years = Object.keys(archives).sort((a, b) => Number(b) - Number(a)); + + return { + archives, + years + }; + } catch (err: any) { + console.error('Error fetching archives:', err); + throw error(500, 'Failed to fetch archives'); + } +}; diff --git a/src/routes/archive/+page.svelte b/src/routes/archive/+page.svelte new file mode 100644 index 0000000..ed8c51e --- /dev/null +++ b/src/routes/archive/+page.svelte @@ -0,0 +1,115 @@ + + + + {$page.data.aliases?.post || '文章'}归档 - {$page.data.site?.title || 'BlogFlare'} + + +
+
+
+ + +
+ {#each data.years as year} +
+

{year}

+
    + {#each data.archives[year] as post} +
  • + + {post.title} +
  • + {/each} +
+
+ {/each} +
+
+
+
+ +