Skip to content

Commit

Permalink
feat(blog): archives page
Browse files Browse the repository at this point in the history
  • Loading branch information
noghartt committed Oct 26, 2024
1 parent 45eab67 commit 6bda550
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
42 changes: 42 additions & 0 deletions src/pages/blog/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
import { getCollection } from 'astro:content';
import Layout from "../../layout/Layout.astro";
import Link from '../../components/Link.astro';
import GoBackButton from '../../components/GoBackButton.astro';
const posts = await getCollection('blog', ({ data }) => !data.draft);
const postsSorted = posts.sort((a, b) =>
Math.floor(b.data.pubDate / 1000) -
Math.floor(a.data.pubDate / 1000)
);
---

<Layout>
<GoBackButton />
<section class="posts">
<section>
<h2>Archives</h2>
</section>
<ul>
{postsSorted.slice(0, 4).map(post => (
<li>
<Link href={`/blog/${post.slug}`}>
{post.data.pubDate.toISOString().split('T')[0]}
{' '}
{post.data.title}
</Link>
</li>
))}
</ul>
</section>
</Layout>

<style>
.posts {
display: flex;
flex-direction: column;
gap: var(--spacing-2x);
}
</style>
11 changes: 5 additions & 6 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import Layout from "../layout/Layout.astro";
import Link from "../components/Link.astro";
import { GITHUB_REPO_URL, TWITTER_URL, GITHUB_PROFILE_URL } from '../config';
const [posts, lists] = await Promise.all([
getCollection('blog', ({ data }) => !data.draft),
getCollection('lists', ({ data }) => !data.draft),
]);
const posts = await getCollection('blog', ({ data }) => !data.draft);
const postsSorted = posts.sort((a, b) =>
Math.floor(b.data.pubDate / 1000) -
Math.floor(a.data.pubDate / 1000)
Expand Down Expand Up @@ -38,7 +34,7 @@ const year = new Date().getFullYear() - 2002;
<section class="posts">
<h2>Archive</h2>
<ul>
{postsSorted.map(post => (
{postsSorted.slice(0, 5).map(post => (
<li>
<Link href={`/blog/${post.slug}`}>
{post.data.pubDate.toISOString().split('T')[0]}
Expand All @@ -47,6 +43,9 @@ const year = new Date().getFullYear() - 2002;
</Link>
</li>
))}
<li>
<Link href="/blog">See more posts...</Link>
</li>
</ul>
</section>
</Layout>
Expand Down

0 comments on commit 6bda550

Please sign in to comment.