From 4a0bd18b1b7963dfa78131bc059d5d185ac8dc4f Mon Sep 17 00:00:00 2001 From: satnaing Date: Sun, 29 Jan 2023 21:17:12 +0630 Subject: [PATCH] refactor: remove unnessary types and refactor excluding draft Exclude types of postResult and pagePaths which can be inferred. Filtered draft posts in getCollection function. --- src/pages/posts/[slug].astro | 41 +++++++----------------------------- 1 file changed, 8 insertions(+), 33 deletions(-) diff --git a/src/pages/posts/[slug].astro b/src/pages/posts/[slug].astro index e663b0e..17e1d2b 100644 --- a/src/pages/posts/[slug].astro +++ b/src/pages/posts/[slug].astro @@ -11,42 +11,17 @@ export interface Props { post: CollectionEntry<"blog">; } -type PostResult = { - params: { - slug: string | number; - }; - props?: { - post: CollectionEntry<"blog">; - }; -}[]; - -type PagePaths = { - params: { - slug: string; - }; -}[]; - export async function getStaticPaths() { - const posts = await getCollection("blog"); + const posts = await getCollection("blog", ({ data }) => !data.draft); - const filteredPosts = posts.filter(({ data }) => !data.draft); + const postResult = posts.map(post => ({ + params: { slug: slugify(post.data) }, + props: { post }, + })); - let postResult: PostResult = filteredPosts.map(post => { - return { - params: { - slug: slugify(post.data), - }, - props: { - post, - }, - }; - }); - - const pagePaths: PagePaths = getPageNumbers(filteredPosts.length).map( - pageNum => ({ - params: { slug: String(pageNum) }, - }) - ); + const pagePaths = getPageNumbers(posts.length).map(pageNum => ({ + params: { slug: String(pageNum) }, + })); return [...postResult, ...pagePaths]; }