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 { 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());
---

View File

@@ -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 },

View File

@@ -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(),
);

View File

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