fix: Cataloging problem on /post #23

Merged
aniva merged 6 commits from post/page into main 2024-10-18 11:17:32 -07:00
10 changed files with 114 additions and 97 deletions

View File

@ -1,7 +1,6 @@
import type { Reroute } from '@sveltejs/kit'; import type { Reroute } from '@sveltejs/kit';
const translated: Record<string, string> = { const translated: Record<string, string> = {
'/post': '/page/1',
}; };
export const reroute: Reroute = ({ url }) => { export const reroute: Reroute = ({ url }) => {

View File

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import type { Post } from '$lib/types' import type { Post } from '$lib/types'
import DividerVertical from "svelte-radix/DividerVertical.svelte"; import DividerVertical from "svelte-radix/DividerVertical.svelte"
export let post: Post; export let post: Post;
export let inactive: bool = false; export let inactive: bool = false;

View File

@ -0,0 +1,69 @@
<script lang="ts">
import { DoubleArrowLeft, DoubleArrowRight, ChevronLeft, ChevronRight } from 'svelte-radix';
import type { Post } from '$lib/types'
import PostHeader from './PostHeader.svelte';
export let posts: Post[];
// The page navigation bar will not show if these numbers are not provided.
export let maxPageN: number = 0;
export let pageN: number = 0;
const disableLinkPrev = pageN == 1;
const disableLinkNext = pageN == maxPageN;
const firstPageLink = "/post";
const prevLink = pageN <= 2 ? firstPageLink : `/page/${pageN - 1}`;
</script>
<ul id="catalog" class="content">
{#each posts as post}
<li>
<PostHeader {post}/>
</li>
{/each}
</ul>
<!-- Navigation bar -->
{#if maxPageN}
<hr class="separator" />
<div id="page-navigator">
<a
aria-label="First Page"
class="nav-link icon"
class:disabled-link={disableLinkPrev}
href={firstPageLink}><DoubleArrowLeft /></a>
<a
aria-label="Prev Page"
class="nav-link icon"
class:disabled-link={disableLinkPrev}
href={prevLink}><ChevronLeft /></a>
<div id="page-map">
<p id="page-num">{pageN}/{maxPageN}</p>
</div>
<a
aria-label="Next Page"
class="nav-link icon"
class:disabled-link={disableLinkNext}
href="/page/{Math.min(maxPageN, pageN+1)}"><ChevronRight /></a>
<a
aria-label="Last Page"
class="nav-link icon"
class:disabled-link={disableLinkNext}
href="/page/{maxPageN}"><DoubleArrowRight /></a>
</div>
{/if}
<style>
#page-navigator {
display: flex;
flex-direction: horizontal;
justify-content: space-between;
margin: 1em auto 1em auto;
width: 80%;
align-items: center;
}
#page-map {
display: inline-block;
}
#page-num {
color: rgb(128,128,128);
}
</style>

View File

@ -1,14 +1,10 @@
<script lang="ts"> <script lang="ts">
import type { PageData } from './$types'; import siteMetadata from '$content/metadata.json'
export let data: PageData; import PostList from '$lib/components/PostList.svelte'
const { posts, pageN, maxPageN } = data;
import siteMetadata from '$content/metadata.json';
import { DoubleArrowLeft, DoubleArrowRight, ChevronLeft, ChevronRight } from 'svelte-radix'; import type { PageData } from './$types'
import PostHeader from '$lib/components/PostHeader.svelte'; export let data: PageData
const { posts, pageN, maxPageN } = data
const disableLinkPrev = pageN == 1;
const disableLinkNext = pageN == maxPageN;
</script> </script>
<svelte:head> <svelte:head>
@ -16,49 +12,7 @@
</svelte:head> </svelte:head>
<hr class="separator" /> <hr class="separator" />
<ul id="catalog" class="content"> <PostList {posts} {pageN} {maxPageN}/>
{#each posts as post}
<li>
<PostHeader post={post}/>
</li>
{/each}
</ul>
<hr class="separator" />
<div id="page-navigator">
<a
class="nav-link icon"
class:disabled-link={disableLinkPrev}
href="/page/1"><DoubleArrowLeft /></a>
<a
class="nav-link icon"
class:disabled-link={disableLinkPrev}
href="/page/{Math.max(1, pageN-1)}"><ChevronLeft /></a>
<div id="page-map">
<p id="page-num">{pageN}/{maxPageN}</p>
</div>
<a
class="nav-link icon"
class:disabled-link={disableLinkNext}
href="/page/{Math.min(maxPageN, pageN+1)}"><ChevronRight /></a>
<a
class="nav-link icon"
class:disabled-link={disableLinkNext}
href="/page/{maxPageN}"><DoubleArrowRight /></a>
</div>
<style> <style>
#page-navigator {
display: flex;
flex-direction: horizontal;
justify-content: space-between;
margin: 1em auto 1em auto;
width: 80%;
align-items: center;
}
#page-map {
display: inline-block;
}
#page-num {
color: rgb(128,128,128);
}
</style> </style>

View File

@ -1,7 +1,7 @@
import { error } from '@sveltejs/kit'; import { error } from '@sveltejs/kit'
import type { PageLoad } from './$types'; import type { PageLoad } from './$types'
import { getPosts } from '$lib/posts'; import { getPosts } from '$lib/posts'
import siteMetadata from '$content/metadata.json'; import siteMetadata from '$content/metadata.json'
export const load: PageLoad = async ({ params }) => { export const load: PageLoad = async ({ params }) => {
const pageN: number = +params.slug; const pageN: number = +params.slug;

View File

@ -1,7 +1,14 @@
import type { PageLoad } from './$types'; import type { PageLoad } from './$types'
import { getPosts } from '$lib/posts'; import { getPosts } from '$lib/posts'
import siteMetadata from '$content/metadata.json'
export const load: PageLoad = async (_) => { export const load: PageLoad = async () => {
const allPosts = await getPosts(); const posts = await getPosts();
return { allPosts }; const pageSize = siteMetadata?.pageSize || 3;
}; const maxPageN = Math.ceil(posts.length / pageSize);
return {
maxPageN,
posts: posts.slice(0, pageSize),
};
}

View File

@ -1,9 +1,10 @@
<script lang="ts"> <script lang="ts">
import siteMetadata from '$content/metadata.json';
import PostList from '$lib/components/PostList.svelte'
import type { PageData } from './$types'; import type { PageData } from './$types';
export let data: PageData; export let data: PageData;
const { allPosts } = data; const { posts, maxPageN } = data;
import siteMetadata from '$content/metadata.json';
import PostHeader from '$lib/components/PostHeader.svelte';
</script> </script>
<svelte:head> <svelte:head>
@ -11,17 +12,4 @@
</svelte:head> </svelte:head>
<hr class="separator" /> <hr class="separator" />
<ul id="catalog" class="content"> <PostList {posts} pageN=1 {maxPageN}/>
{#each allPosts as post}
<li>
<PostHeader post={post}/>
</li>
{/each}
</ul>
<hr class="separator" />
<style>
#catalog li {
margin-bottom: 10px;
}
</style>

View File

@ -1,9 +1,9 @@
<script lang="ts"> <script lang="ts">
import type { PageData } from './$types'; import type { PageData } from './$types';
import PostList from '$lib/components/PostList.svelte'
import siteMetadata from '$content/metadata.json';
export let data: PageData; export let data: PageData;
const { name, posts } = data; const { name, posts } = data;
import siteMetadata from '$content/metadata.json';
import PostHeader from '$lib/components/PostHeader.svelte';
</script> </script>
<svelte:head> <svelte:head>
@ -11,19 +11,9 @@
</svelte:head> </svelte:head>
<h1>{name} ({posts.length})</h1> <h1>{name} ({posts.length})</h1>
<hr class="separator" /> <hr class="separator" />
<ul id="catalog" class="content"> <PostList {posts}/>
{#each posts as post}
<li>
<PostHeader post={post}/>
</li>
{/each}
</ul>
<hr class="separator" /> <hr class="separator" />
<style> <style>
#catalog li {
margin-bottom: 10px;
}
</style> </style>

View File

@ -91,9 +91,7 @@ const config = {
}), }),
prerender: { prerender: {
crawl: true, crawl: true,
entries: [ entries: ['*'],
"/page/1/",
],
}, },
alias: { alias: {
$content: contentDir, $content: contentDir,

View File

@ -5,3 +5,15 @@ test('Navigate to blog post', async ({ page }) => {
await page.getByText('The Perfect Math Class').click(); await page.getByText('The Perfect Math Class').click();
await expect(page).toHaveURL("/post/the-perfect-math-class/"); await expect(page).toHaveURL("/post/the-perfect-math-class/");
}); });
test('Navigate to page 2', async ({ page }) => {
await page.goto('/post');
await page.getByRole('link', { name: 'Next Page' }).click();
await expect(page).toHaveURL("/page/2/");
await page.getByRole('link', { name: 'Prev Page' }).click();
await expect(page).toHaveURL("/post/");
await page.getByRole('link', { name: 'Last Page' }).click();
await expect(page).toHaveURL("/page/2/");
await page.getByRole('link', { name: 'First Page' }).click();
await expect(page).toHaveURL("/post/");
});