diff --git a/src/lib/posts.ts b/src/lib/posts.ts index d0fe91f..ecfc429 100644 --- a/src/lib/posts.ts +++ b/src/lib/posts.ts @@ -15,7 +15,8 @@ export function transformPostMeta(metadata: PostMetadata, slug: string) : Post { }; } -export async function getPosts(tag: string | null = null) : Promise { +// 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); @@ -27,8 +28,10 @@ export async function getPosts(tag: string | null = null) : Promise { return transformPostMeta(metadata, slug); }) ); - if (tag) - posts = posts.filter(post => post.tags.includes(tag)) + if (filter?.tag) + posts = posts.filter(post => post.tags.includes(filter.tag!)) + if (filter?.series) + posts = posts.filter(post => post.series?.includes(filter.series!) ?? false) posts.sort((post1, post2) => { const date1: Date = post1.date; 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 @@
+
    + {#each seriesNeighbours as { name, prevSlug, prevTitle, nextSlug, nextTitle }} +
  • + {#if prevTitle} + {prevTitle} +

    + {/if} +

    {name}

    + {#if nextTitle} +

    + {nextTitle} + {/if} +
  • + {/each} +
+ + diff --git a/src/routes/post/[slug]/+page.ts b/src/routes/post/[slug]/+page.ts index 974b02e..151f390 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((metadata.series || []).map(async (series: string) => { + const postInSeries: Post[] = await getPosts({ series }); + + let prevSlug = undefined; + let prevTitle = undefined; + let nextSlug = undefined; + let nextTitle = undefined; + + 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 { diff --git a/src/routes/tag/[slug]/+page.ts b/src/routes/tag/[slug]/+page.ts index f3a0fe0..6d2fafd 100644 --- a/src/routes/tag/[slug]/+page.ts +++ b/src/routes/tag/[slug]/+page.ts @@ -2,7 +2,7 @@ import type { PageLoad } from './$types'; import { getPosts } from '$lib/posts'; export const load: PageLoad = async ({ params }) => { - const posts = await getPosts(params.slug); + const posts = await getPosts({ tag: params.slug }); return { name: params.slug, posts, diff --git a/tests/series-tags.spec.ts b/tests/series-tags.spec.ts new file mode 100644 index 0000000..b2dc6eb --- /dev/null +++ b/tests/series-tags.spec.ts @@ -0,0 +1,9 @@ +import { expect, test } from '@playwright/test'; + +test('Navigate in series', async ({ page }) => { + await page.goto('/post'); + await page.getByText('Placeholder 2').click(); + await expect(page).toHaveURL("/post/placeholder2/"); + await page.getByText('Placeholder 1').click(); + await expect(page).toHaveURL("/post/placeholder1/"); +});