refactor: remove unnessary types and refactor excluding draft

Exclude types of postResult and pagePaths  which can be inferred. Filtered draft posts in
getCollection function.
This commit is contained in:
satnaing
2023-01-29 21:17:12 +06:30
parent a61fd45559
commit 4a0bd18b1b
+8 -33
View File
@@ -11,42 +11,17 @@ export interface Props {
post: CollectionEntry<"blog">; post: CollectionEntry<"blog">;
} }
type PostResult = {
params: {
slug: string | number;
};
props?: {
post: CollectionEntry<"blog">;
};
}[];
type PagePaths = {
params: {
slug: string;
};
}[];
export async function getStaticPaths() { 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 => { const pagePaths = getPageNumbers(posts.length).map(pageNum => ({
return { params: { slug: String(pageNum) },
params: { }));
slug: slugify(post.data),
},
props: {
post,
},
};
});
const pagePaths: PagePaths = getPageNumbers(filteredPosts.length).map(
pageNum => ({
params: { slug: String(pageNum) },
})
);
return [...postResult, ...pagePaths]; return [...postResult, ...pagePaths];
} }