add prev/next post

This commit is contained in:
Akis 2023-01-03 19:58:24 +02:00
parent b812dabed5
commit ddba1fc23e
Signed by untrusted user: akis
GPG Key ID: 267BF5C6677944ED
3 changed files with 38 additions and 4 deletions

View File

@ -7,6 +7,6 @@
<slot /> <slot />
{#if url} {#if url}
<a href={url}>View on Ghost</a> <a href={url} class="text-center">View on Ghost</a>
{/if} {/if}
</div> </div>

View File

@ -4,7 +4,10 @@ import fetchApi from "$lib/ghost";
export const load = (async ({ params }) => { export const load = (async ({ params }) => {
const data = await fetchApi("posts/slug/" + params.title); const data = await fetchApi("posts/slug/" + params.title);
const allPosts = await fetchApi("posts");
return { return {
post: data.posts[0] post: data.posts[0],
allPosts: allPosts
}; };
}) satisfies PageServerLoad; }) satisfies PageServerLoad;

View File

@ -1,8 +1,12 @@
<script lang="ts"> <script lang="ts">
import { PostOuter, Title, Meta, PostContent, ReadMore, PostsContainer } from "$lib/BlogCard";
import type { PageData } from "./$types"; import type { PageData } from "./$types";
export let data: PageData; export let data: PageData;
import { PostOuter, Title, Meta, PostContent } from "$lib/BlogCard"; $: index = data.allPosts.posts.findIndex((post: { slug: string; }) => post.slug === data.post.slug);
$: next = data.allPosts.posts[index - 1];
$: previous = data.allPosts.posts[index + 1];
</script> </script>
<svelte:head> <svelte:head>
@ -16,3 +20,30 @@
</div> </div>
<PostContent {data} /> <PostContent {data} />
</PostOuter> </PostOuter>
<div class="flex flex-row flex-wrap justify-center my-4">
<PostsContainer>
{#if previous}
<PostOuter>
<h1 class="more-posts">Previous post</h1>
<Title post={previous} />
<Meta post={previous} />
<ReadMore post={previous} />
</PostOuter>
{/if}
{#if next}
<PostOuter>
<h1 class="more-posts">Next post</h1>
<Title post={next} />
<Meta post={next} />
<ReadMore post={next} />
</PostOuter>
{/if}
</PostsContainer>
</div>
<style>
.more-posts {
@apply border-b-solid border-b-grey border-b-1 m-0;
}
</style>