Skip to content

Commit

Permalink
Hide posts when PubDateTime is not reached
Browse files Browse the repository at this point in the history
  • Loading branch information
davlgd committed Jan 14, 2024
1 parent 742314e commit 532fe41
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const SITE: Site = {
ogImage: "astropaper-og.jpg",
lightAndDarkMode: true,
postPerPage: 3,
scheduledPostMargin: 15 * 60 * 1000, // 15 minutes
};

export const LOCALE = {
Expand Down
6 changes: 4 additions & 2 deletions src/pages/search.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import Main from "@layouts/Main.astro";
import Header from "@components/Header.astro";
import Footer from "@components/Footer.astro";
import SearchBar from "@components/Search";
import getSortedPosts from "@utils/getSortedPosts";
// Retrieve all articles
// Retrieve all published articles
const posts = await getCollection("blog", ({ data }) => !data.draft);
const sortedPosts = getSortedPosts(posts);
// List of items to search in
const searchList = posts.map(({ data, slug }) => ({
const searchList = sortedPosts.map(({ data, slug }) => ({
title: data.title,
description: data.description,
data,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Site = {
ogImage?: string;
lightAndDarkMode: boolean;
postPerPage: number;
scheduledPostMargin: number;
};

export type SocialObjects = {
Expand Down
8 changes: 7 additions & 1 deletion src/utils/getSortedPosts.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { SITE } from "@config";
import type { CollectionEntry } from "astro:content";

const getSortedPosts = (posts: CollectionEntry<"blog">[]) => {
return posts
.filter(({ data }) => !data.draft)
.filter(({ data }) => {
const alreadyPublished =
new Date(data.pubDatetime).getTime() >
Date.now() + SITE.scheduledPostMargin && !import.meta.env.DEV;
return !data.draft && !alreadyPublished;
})
.sort(
(a, b) =>
Math.floor(
Expand Down
8 changes: 7 additions & 1 deletion src/utils/getUniqueTags.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SITE } from "@config";
import { slugifyStr } from "./slugify";
import type { CollectionEntry } from "astro:content";

Expand All @@ -7,7 +8,12 @@ interface Tag {
}

const getUniqueTags = (posts: CollectionEntry<"blog">[]) => {
const filteredPosts = posts.filter(({ data }) => !data.draft);
const filteredPosts = posts.filter(({ data }) => {
const alreadyPublished =
new Date(data.pubDatetime).getTime() >
Date.now() + SITE.scheduledPostMargin && !import.meta.env.DEV;
return !data.draft && !alreadyPublished;
});
const tags: Tag[] = filteredPosts
.flatMap(post => post.data.tags)
.map(tag => ({ tag: slugifyStr(tag), tagName: tag }))
Expand Down

0 comments on commit 532fe41

Please sign in to comment.