Skip to content

Commit

Permalink
feat: sort posts by pubDate
Browse files Browse the repository at this point in the history
  • Loading branch information
renanleonel committed Mar 6, 2024
1 parent c31ef6e commit fbd65e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
14 changes: 10 additions & 4 deletions src/components/Post.astro
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
---
import Badge from "./Badge.astro";
interface Props {
title: string;
description: string;
url: string;
index: number;
}
const { title, description, url } = Astro.props;
const { title, description, url, index } = Astro.props;
---

<div class="hover:bg-neutral-100 dark:hover:bg-[#242424] px-2 py-4 rounded-md">
<a href={url}>
<h2 class="text-lg font-semibold">
{title}
</h2>
<div class="flex items-center gap-4">
<h2 class="text-lg font-semibold">
{title}
</h2>
{index === 0 && <Badge>new!</Badge>}
</div>
<p>{description}</p>
</a>
</div>
13 changes: 10 additions & 3 deletions src/pages/posts/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@
import "@/styles/globals.css";
import Post from "@/components/Post.astro";
import Layout from "@/layouts/Layout.astro";
const posts = await getCollection("posts");
import { getCollection } from "astro:content";
let posts = await getCollection("posts");
posts = posts.sort(
(a, b) =>
Date.parse(b.data.pubDate.toISOString()) -
Date.parse(a.data.pubDate.toISOString()),
);
---

<Layout title="posts | renan leonel">
<main class="space-y-4">
<h1 class="text-xl font-bold">Posts</h1>
<div class="space-y-4">
{
posts.map((post) => (
posts.map((post, i) => (
<Post
title={post.data.title}
description={post.data.description}
url={`/posts/${post.slug}/`}
index={i}
/>
))
}
Expand Down

0 comments on commit fbd65e4

Please sign in to comment.