diff --git a/src/utils/postFilter.ts b/src/utils/postFilter.ts index a2e4682cbb..d4c75c8b18 100644 --- a/src/utils/postFilter.ts +++ b/src/utils/postFilter.ts @@ -1,16 +1,18 @@ import type { CollectionEntry } from "astro:content"; +import dayjs from "dayjs"; +import utc from "dayjs/plugin/utc"; +import timezone from "dayjs/plugin/timezone"; import config from "@/config"; -/** - * Determines whether a post is eligible to be listed/rendered. - * - * - Excludes drafts always - * - In production, excludes scheduled posts until `pubDatetime` minus the configured margin - * - In dev, always shows non-draft posts to make authoring easier - */ +dayjs.extend(utc); +dayjs.extend(timezone); + export function postFilter({ data }: CollectionEntry<"posts">) { + const tz = data.timezone ?? config.site.timezone; + // Interpret the date in the site's timezone instead of UTC, so that + // pubDatetime: 2026-06-28 means "June 28 in Shanghai" not "June 28 UTC". + const publishTime = dayjs.utc(data.pubDatetime).tz(tz, true); const isPublishTimePassed = - Date.now() > - new Date(data.pubDatetime).getTime() - config.posts.scheduledPostMargin; + Date.now() > publishTime.valueOf() - config.posts.scheduledPostMargin; return !data.draft && (import.meta.env.DEV || isPublishTimePassed); }