diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts new file mode 100644 index 0000000..cf12d86 --- /dev/null +++ b/src/lib/helpers.ts @@ -0,0 +1,5 @@ +import { type CollectionEntry } from "astro:content"; + +export function shouldShowPost(post: CollectionEntry<"blog">) { + return import.meta.env.DEV || !post.data.draft; +} \ No newline at end of file diff --git a/src/pages/index.astro b/src/pages/index.astro index 21f75aa..271f7c8 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -1,8 +1,9 @@ --- import { getCollection } from "astro:content"; +import { shouldShowPost } from "@lib/helpers"; import General from "@layouts/General.astro"; import Summary from "@components/Summary.astro"; -const posts = (await getCollection("blog")).filter(entry => import.meta.env.DEV || !entry.data.draft); +const posts = (await getCollection("blog")).filter(shouldShowPost); posts.sort((a, b) => new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime()); --- diff --git a/src/pages/posts/[...slug]/index.astro b/src/pages/posts/[...slug]/index.astro index dcc7ada..de9cc6a 100644 --- a/src/pages/posts/[...slug]/index.astro +++ b/src/pages/posts/[...slug]/index.astro @@ -1,11 +1,12 @@ --- import Post from "@layouts/Post.astro"; +import { shouldShowPost } from "@lib/helpers"; import { getCollection } from "astro:content"; export async function getStaticPaths() { const posts = await getCollection("blog"); return posts - .filter((entry) => import.meta.env.DEV || !entry.data.draft) + .filter(shouldShowPost) .map((entry) => { return { params: { slug: entry.slug }, diff --git a/src/pages/tags/[...slug].astro b/src/pages/tags/[...slug].astro index a3e5d83..63fec51 100644 --- a/src/pages/tags/[...slug].astro +++ b/src/pages/tags/[...slug].astro @@ -1,12 +1,13 @@ --- import General from "@layouts/General.astro"; +import { shouldShowPost } from "@lib/helpers"; import { getCollection } from "astro:content"; export async function getStaticPaths() { const blogEntries = await getCollection("blog"); const tags = new Set( blogEntries - .filter((entry) => import.meta.env.DEV || !entry.data.draft) + .filter(shouldShowPost) .map((entry) => entry.data.tags) .flat(), ); diff --git a/tsconfig.json b/tsconfig.json index bacec95..1e4d16e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,9 @@ "compilerOptions": { "baseUrl": ".", "paths": { + "@lib/*": [ + "src/lib/*" + ], "@styles/*": [ "src/styles/*" ],