Factor filter predicate into helpers.ts, add @lib alias dir

This commit is contained in:
2023-12-01 21:35:02 -06:00
parent b1e4f1a583
commit 7e461a18a9
5 changed files with 14 additions and 3 deletions

5
src/lib/helpers.ts Normal file
View File

@@ -0,0 +1,5 @@
import { type CollectionEntry } from "astro:content";
export function shouldShowPost(post: CollectionEntry<"blog">) {
return import.meta.env.DEV || !post.data.draft;
}

View File

@@ -1,8 +1,9 @@
--- ---
import { getCollection } from "astro:content"; import { getCollection } from "astro:content";
import { shouldShowPost } from "@lib/helpers";
import General from "@layouts/General.astro"; import General from "@layouts/General.astro";
import Summary from "@components/Summary.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()); posts.sort((a, b) => new Date(b.data.pubDate).getTime() - new Date(a.data.pubDate).getTime());
--- ---

View File

@@ -1,11 +1,12 @@
--- ---
import Post from "@layouts/Post.astro"; import Post from "@layouts/Post.astro";
import { shouldShowPost } from "@lib/helpers";
import { getCollection } from "astro:content"; import { getCollection } from "astro:content";
export async function getStaticPaths() { export async function getStaticPaths() {
const posts = await getCollection("blog"); const posts = await getCollection("blog");
return posts return posts
.filter((entry) => import.meta.env.DEV || !entry.data.draft) .filter(shouldShowPost)
.map((entry) => { .map((entry) => {
return { return {
params: { slug: entry.slug }, params: { slug: entry.slug },

View File

@@ -1,12 +1,13 @@
--- ---
import General from "@layouts/General.astro"; import General from "@layouts/General.astro";
import { shouldShowPost } from "@lib/helpers";
import { getCollection } from "astro:content"; import { getCollection } from "astro:content";
export async function getStaticPaths() { export async function getStaticPaths() {
const blogEntries = await getCollection("blog"); const blogEntries = await getCollection("blog");
const tags = new Set( const tags = new Set(
blogEntries blogEntries
.filter((entry) => import.meta.env.DEV || !entry.data.draft) .filter(shouldShowPost)
.map((entry) => entry.data.tags) .map((entry) => entry.data.tags)
.flat(), .flat(),
); );

View File

@@ -3,6 +3,9 @@
"compilerOptions": { "compilerOptions": {
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"@lib/*": [
"src/lib/*"
],
"@styles/*": [ "@styles/*": [
"src/styles/*" "src/styles/*"
], ],