diff --git a/src/routes/post/Heading.svelte b/src/lib/components/PostHeader.svelte similarity index 100% rename from src/routes/post/Heading.svelte rename to src/lib/components/PostHeader.svelte diff --git a/src/lib/posts.ts b/src/lib/posts.ts new file mode 100644 index 0000000..855c1e3 --- /dev/null +++ b/src/lib/posts.ts @@ -0,0 +1,26 @@ +export async function getPosts() { + const allPostFiles = import.meta.glob('$content/post/*.md'); + const iterablePostFiles = Object.entries(allPostFiles); + + const allPosts = await Promise.all( + iterablePostFiles.map(async ([pathMarkdown, resolver]) => { + const { metadata } = await resolver(); + const pathPost = pathMarkdown.slice(pathMarkdown.lastIndexOf("/") + 1, -".md".length); + + return { + meta: { + ...metadata, + date: new Date(metadata.date), + }, + path: pathPost + }; + }) + ); + + allPosts.sort((post1, post2) => { + const date1: Date = post1.meta.date; + const date2: Date = post2.meta.date; + return date2.getTime() - date1.getTime(); + }); + return allPosts; +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 45c2981..fa257e3 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -31,12 +31,12 @@ {/if} diff --git a/src/routes/page/[slug]/+page.ts b/src/routes/page/[slug]/+page.ts new file mode 100644 index 0000000..dd9e8c7 --- /dev/null +++ b/src/routes/page/[slug]/+page.ts @@ -0,0 +1,22 @@ +import { error } from '@sveltejs/kit'; +import type { PageLoad } from './$types'; +import { getPosts } from '$lib/posts'; +import siteMetadata from '$content/metadata.json'; + +export const load: PageLoad = async ({ params }) => { + const pageN: number = +params.slug; + if (!pageN) throw error(404); + + const posts = await getPosts(); + + const pageSize = siteMetadata?.pageSize || 3; + + const maxPageN = Math.ceil(posts.length / pageSize); + if (pageN < 0 || pageN > maxPageN) throw error(404); + + return { + pageN, + posts: posts.slice((pageN - 1) * pageSize, pageN * pageSize), + maxPageN, + }; +} diff --git a/src/routes/post/+page.server.ts b/src/routes/post/+page.server.ts index 368c4b9..fc78cd1 100644 --- a/src/routes/post/+page.server.ts +++ b/src/routes/post/+page.server.ts @@ -1,30 +1,7 @@ import type { PageLoad } from './$types'; +import { getPosts } from '$lib/posts'; export const load: PageLoad = async (_) => { - const allPostFiles = import.meta.glob('$content/post/*.md'); - const iterablePostFiles = Object.entries(allPostFiles); - - const allPosts = await Promise.all( - iterablePostFiles.map(async ([pathMarkdown, resolver]) => { - const { metadata } = await resolver(); - const pathPost = pathMarkdown.slice(pathMarkdown.lastIndexOf("/") + 1, -".md".length); - - return { - meta: { - ...metadata, - date: new Date(metadata.date), - }, - path: pathPost - }; - }) - ); - - allPosts.sort((post1, post2) => { - const date1: Date = post1.meta.date; - const date2: Date = post2.meta.date; - return date2.getTime() - date1.getTime(); - }); - return { - allPosts - }; + const allPosts = await getPosts(); + return { allPosts }; }; diff --git a/src/routes/post/+page.svelte b/src/routes/post/+page.svelte index f5925fd..5298603 100644 --- a/src/routes/post/+page.svelte +++ b/src/routes/post/+page.svelte @@ -3,7 +3,7 @@ export let data: PageData; const { allPosts } = data; import siteMetadata from '$content/metadata.json'; - import Heading from './Heading.svelte'; + import PostHeader from '$lib/components/PostHeader.svelte'; @@ -14,7 +14,7 @@