Merge pull request 'feat: Series' (#31) from post/series into main

Reviewed-on: #31
This commit is contained in:
Leni Aniva 2025-01-25 15:38:01 -08:00
commit 7daa6eb70d
5 changed files with 94 additions and 7 deletions

View File

@ -15,7 +15,8 @@ export function transformPostMeta(metadata: PostMetadata, slug: string) : Post {
};
}
export async function getPosts(tag: string | null = null) : Promise<Post[]> {
// Generates a list of all posts filtering by tags and series, reverse chronologically ordered.
export async function getPosts(filter: { tag?: string, series?: string }) : Promise<Post[]> {
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<Post[]> {
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;

View File

@ -1,7 +1,8 @@
<script lang="ts">
import { TrackPrevious, TrackNext } from 'svelte-radix';
import type { PageData } from './$types';
export let data: PageData;
const { post, Content } = data;
const { post, Content, seriesNeighbours } = data;
import siteMetadata from '$content/metadata.json';
import PostHeader from '$lib/components/PostHeader.svelte';
</script>
@ -17,3 +18,39 @@
<Content />
</article>
<hr />
<ul id="series">
{#each seriesNeighbours as { name, prevSlug, prevTitle, nextSlug, nextTitle }}
<li class="series-neighbour">
{#if prevTitle}
<a href="/post/{prevSlug}">{prevTitle}</a>
<p class="dummy"><TrackPrevious /></p>
{/if}
<p class="series-tag">{name}</p>
{#if nextTitle}
<p class="dummy"><TrackNext /></p>
<a href="/post/{nextSlug}">{nextTitle}</a>
{/if}
</li>
{/each}
</ul>
<style>
#series {
text-align: center;
}
.series-tag {
color: var(--series);
display: inline-block;
}
.dummy {
display: inline-block;
color: rgba(128,128,128, .6);
vertical-align: middle;
padding-left: 1em;
padding-right: 1em;
}
.series-neighbour {
padding-left: 2em;
padding-right: 2em;
}
</style>

View File

@ -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 {

View File

@ -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,

View File

@ -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/");
});