diff --git a/src/lib/posts.ts b/src/lib/posts.ts index 9984e6d..ecfc429 100644 --- a/src/lib/posts.ts +++ b/src/lib/posts.ts @@ -15,7 +15,7 @@ export function transformPostMeta(metadata: PostMetadata, slug: string) : Post { }; } -// Generates a list of all posts filtering by tags and series +// Generates a list of all posts filtering by tags and series, reverse chronologically ordered. export async function getPosts(filter: { tag?: string, series?: string }) : Promise { const allPostFiles = import.meta.glob('$content/post/*.md'); const iterablePostFiles = Object.entries(allPostFiles); diff --git a/src/routes/post/[slug]/+page.svelte b/src/routes/post/[slug]/+page.svelte index 22fcab7..f7609be 100644 --- a/src/routes/post/[slug]/+page.svelte +++ b/src/routes/post/[slug]/+page.svelte @@ -1,7 +1,8 @@ @@ -17,3 +18,39 @@
+ + + diff --git a/src/routes/post/[slug]/+page.ts b/src/routes/post/[slug]/+page.ts index 974b02e..a974f60 100644 --- a/src/routes/post/[slug]/+page.ts +++ b/src/routes/post/[slug]/+page.ts @@ -1,16 +1,54 @@ import { error } from '@sveltejs/kit'; import type { PageLoad } from './$types'; -import { transformPostMeta } from '$lib/posts'; +import type { Post } from '$lib/types' +import { getPosts, transformPostMeta } from '$lib/posts'; export const load: PageLoad = async ({ params }) => { try { const post = await import(`$content/post/${params.slug}.md`); if (!post) throw error(404); + const metadata: Post = transformPostMeta(post.metadata, params.slug); const Content = post.default; + const seriesNeighbours : { + name: string, + prevSlug?: string, + prevTitle?: string, + nextSlug?: string, + nextTitle?: string, + }[] = await Promise.all(post.metadata.series.map(async (series: string) => { + const postInSeries: Post[] = await getPosts({ series }); + + let prevSlug = null; + let prevTitle = null; + let nextSlug = null; + let nextTitle = null; + + for (const p of postInSeries) { + if (p.date > metadata.date) { + nextSlug = p.slug; + nextTitle = p.title; + } + if (p.date < metadata.date) { + prevSlug = p.slug; + prevTitle = p.title; + break; + } + } + + return { + name: series, + prevSlug, + prevTitle, + nextSlug, + nextTitle, + }; + })); + return { - post: transformPostMeta(post.metadata, params.slug), + post: metadata, Content, + seriesNeighbours, }; } catch {