From 85ded530355b2f8d576c5a20899ff343162698a4 Mon Sep 17 00:00:00 2001 From: thislight Date: Sat, 26 Apr 2025 14:21:07 +0800 Subject: [PATCH] add VISIBLE_DRAFT for showing drafts in the list --- astro.config.mjs | 51 +++++++++++++++++++++++++++++----------------- package.json | 2 +- src/utils/posts.ts | 11 ++++++++-- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/astro.config.mjs b/astro.config.mjs index f777689..bcf224e 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -1,23 +1,36 @@ -import { defineConfig } from "astro/config"; +import { defineConfig, envField } from "astro/config"; import solidJs from "@astrojs/solid-js"; import mdx from "@astrojs/mdx"; import icon from "astro-icon"; -import paths from "node:path" +import paths from "node:path"; import rehypeBuckTable from "~/support/rehype-buck-tables"; // https://astro.build/config export default defineConfig({ - integrations: [solidJs(), mdx(), icon(), { - name: "inject-cloudflare-headers", - "hooks": { - "astro:config:setup": ({injectRoute}) => { - injectRoute({ - pattern: "/_headers", - entrypoint: "./src/pages/_headers.ts", - }) - } - } - } + env: { + schema: { + VISIBLE_DRAFT: envField.boolean({ + context: "server", + access: "public", + default: false, + }), + }, + }, + integrations: [ + solidJs(), + mdx(), + icon(), + { + name: "inject-cloudflare-headers", + hooks: { + "astro:config:setup": ({ injectRoute }) => { + injectRoute({ + pattern: "/_headers", + entrypoint: "./src/pages/_headers.ts", + }); + }, + }, + }, ], markdown: { rehypePlugins: [rehypeBuckTable], @@ -25,14 +38,14 @@ export default defineConfig({ themes: { light: "github-light", dark: "github-dark", - } - } + }, + }, }, vite: { resolve: { alias: { - "~": paths.resolve(import.meta.dir, "src") - } - } - } + "~": paths.resolve(import.meta.dir, "src"), + }, + }, + }, }); diff --git a/package.json b/package.json index 48ece13..4bcaadd 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "type": "module", "private": true, "scripts": { - "dev": "bunx --bun astro dev", + "dev": "VISIBLE_DRAFT=true bunx --bun astro dev", "dist": "bunx --bun astro build", "preview": "bunx --bun astro preview", "posts": "bun run scripts/postman.ts" diff --git a/src/utils/posts.ts b/src/utils/posts.ts index 7fcf42a..ca7dde4 100644 --- a/src/utils/posts.ts +++ b/src/utils/posts.ts @@ -1,4 +1,5 @@ import type { CollectionEntry } from "astro:content"; +import { VISIBLE_DRAFT } from "astro:env/server"; function toPadLeft(n: number) { if (n < 10) { @@ -24,7 +25,9 @@ export function postParamlink(post: CollectionEntry<"posts">) { } export function getAllTags(posts: CollectionEntry<"posts">[]) { - const tags = new Set(posts.filter(shouldBeVisible).flatMap((post) => post.data.tags || [])); + const tags = new Set( + posts.filter(shouldBeVisible).flatMap((post) => post.data.tags || []), + ); return tags.values().toArray().sort(); } @@ -75,5 +78,9 @@ export function getHotTags(posts: CollectionEntry<"posts">[], tags: string[]) { } export function shouldBeVisible(post: CollectionEntry<"posts">) { - return !post.data.visibility || post.data.visibility === "visible" + return ( + !post.data.visibility || + post.data.visibility === "visible" || + (VISIBLE_DRAFT && post.data.visibility === "draft") + ); }