Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/utils/postFilter.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Loading