feat: drafts frontmatter & exclude drafts from production

This commit is contained in:
草师傅 2025-08-18 18:26:04 +08:00
parent bfd3bef223
commit ca28bd2d36
Signed by: gb
GPG key ID: 43330A030E2D6478
12 changed files with 34 additions and 11 deletions

View file

@ -4,6 +4,7 @@ import { z, reference } from 'astro:content';
export const posts = ({ image }) => z.object({ export const posts = ({ image }) => z.object({
title: z.string(), title: z.string(),
description: z.string().optional(), description: z.string().optional(),
draft: z.boolean().optional().default(false),
summary: z.string().optional(), summary: z.string().optional(),
date: z.coerce.date(), date: z.coerce.date(),
categories: z.union([z.array(z.string()), z.string()]).transform(val => Array.isArray(val) ? val : [val]).default(['uncategorized']), categories: z.union([z.array(z.string()), z.string()]).transform(val => Array.isArray(val) ? val : [val]).default(['uncategorized']),

View file

@ -9,7 +9,9 @@ import { getLangFromUrl, useTranslations, useTranslatedPath } from '../i18n/util
const lang = getLangFromUrl(Astro.url); const lang = getLangFromUrl(Astro.url);
const t = useTranslations(lang); const t = useTranslations(lang);
const translatePath = useTranslatedPath(lang); const translatePath = useTranslatedPath(lang);
const posts = await getCollection('posts'); const posts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
posts.sort((a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime()); posts.sort((a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime());
--- ---

View file

@ -17,7 +17,9 @@ const t = useTranslations(lang);
const translatePath = useTranslatedPath(lang); const translatePath = useTranslatedPath(lang);
export async function getStaticPaths() { export async function getStaticPaths() {
const blogEntries = await getCollection('posts'); const blogEntries = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
return blogEntries.map(entry => ({ return blogEntries.map(entry => ({
params: { slug: entry.slug }, props: { entry }, params: { slug: entry.slug }, props: { entry },
})); }));

View file

@ -2,7 +2,9 @@ import { getCollection } from 'astro:content';
export const prerender = true; export const prerender = true;
export async function getStaticPaths() { export async function getStaticPaths() {
const blogEntries = await getCollection('posts'); const blogEntries = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
return blogEntries.map(entry => ({ return blogEntries.map(entry => ({
params: { slug: entry.slug }, props: { entry }, params: { slug: entry.slug }, props: { entry },
})); }));

View file

@ -62,7 +62,9 @@ const fileCache = {
}; };
export async function getStaticPaths() { export async function getStaticPaths() {
const blogEntries = await getCollection('posts'); const blogEntries = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
return blogEntries.map(post => ({ return blogEntries.map(post => ({
params: { slug: post.slug }, props: { post }, params: { slug: post.slug }, props: { post },
})); }));

View file

@ -1,7 +1,9 @@
--- ---
import {getCollection} from "astro:content"; import {getCollection} from "astro:content";
import Layout from "../layouts/Layout.astro"; import Layout from "../layouts/Layout.astro";
const allPosts = await getCollection('posts'); const allPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
const uniqueCategories = [...new Set(allPosts.map((post: any) => post.data.categories ? post.data.categories : []).flat())]; const uniqueCategories = [...new Set(allPosts.map((post: any) => post.data.categories ? post.data.categories : []).flat())];
--- ---
<Layout title="Categoies" description="List all categories used in the blog posts."> <Layout title="Categoies" description="List all categories used in the blog posts.">

View file

@ -4,7 +4,9 @@ import {getCollection} from "astro:content";
import ArticleList from "../../components/ArticleList.astro"; import ArticleList from "../../components/ArticleList.astro";
export async function getStaticPaths() { export async function getStaticPaths() {
const allPosts = await getCollection('posts'); const allPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
// Get all categories, ensuring the default 'uncategorized' is used when categories is undefined // Get all categories, ensuring the default 'uncategorized' is used when categories is undefined
const uniqueCategories = [...new Set(allPosts.map((post: any) => const uniqueCategories = [...new Set(allPosts.map((post: any) =>
post.data.categories || ['uncategorized']).flat())]; post.data.categories || ['uncategorized']).flat())];

View file

@ -1,7 +1,9 @@
import { getCollection } from 'astro:content'; import { getCollection } from 'astro:content';
export async function GET() { export async function GET() {
const posts = await getCollection('posts'); const posts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
const searchIndex = posts.map(post => ({ const searchIndex = posts.map(post => ({
title: post.data.title, title: post.data.title,
description: post.data.description || '', description: post.data.description || '',

View file

@ -1,7 +1,9 @@
--- ---
import {getCollection} from "astro:content"; import {getCollection} from "astro:content";
import Layout from "../layouts/Layout.astro"; import Layout from "../layouts/Layout.astro";
const allPosts = await getCollection('posts'); const allPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
const uniqueTags = [...new Set(allPosts.map((post: any) => post.data.tags ? post.data.tags : []).flat())]; const uniqueTags = [...new Set(allPosts.map((post: any) => post.data.tags ? post.data.tags : []).flat())];
--- ---
<Layout title="Tags" description="List all tags used in the blog posts."> <Layout title="Tags" description="List all tags used in the blog posts.">

View file

@ -4,7 +4,9 @@ import {getCollection} from "astro:content";
import ArticleList from "../../components/ArticleList.astro"; import ArticleList from "../../components/ArticleList.astro";
export async function getStaticPaths() { export async function getStaticPaths() {
const allPosts = await getCollection('posts'); const allPosts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
const uniqueTags = [...new Set(allPosts.map((post: any) => post.data.tags ? post.data.tags : []).flat())]; const uniqueTags = [...new Set(allPosts.map((post: any) => post.data.tags ? post.data.tags : []).flat())];
return uniqueTags.map((tag) => { return uniqueTags.map((tag) => {
const filteredPosts = allPosts.filter((post: any) => post.data.tags?.includes(tag)); const filteredPosts = allPosts.filter((post: any) => post.data.tags?.includes(tag));

View file

@ -1,7 +1,9 @@
import { getCollection } from 'astro:content'; import { getCollection } from 'astro:content';
export async function GET(context) { export async function GET(context) {
const posts = await getCollection('posts'); const posts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
// Sort posts by date (newest first) // Sort posts by date (newest first)
posts.sort((a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime()); posts.sort((a, b) => new Date(b.data.date).getTime() - new Date(a.data.date).getTime());

View file

@ -1,7 +1,9 @@
import { getCollection, render } from 'astro:content'; import { getCollection, render } from 'astro:content';
async function fetchPostsData() { async function fetchPostsData() {
const posts = await getCollection('posts'); const posts = await getCollection('posts', ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
const entriesData = {}; const entriesData = {};
for (const post of posts) { for (const post of posts) {