From 1b9cbfa0a9373cd87192e1cf5115439ed0e6ef34 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Mon, 22 Jul 2024 21:11:46 +0200 Subject: [PATCH] bring back tags and use collections on blog index --- src/pages/blog/[slug].astro | 10 ++++------ src/pages/blog/index.astro | 20 ++++++++------------ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/pages/blog/[slug].astro b/src/pages/blog/[slug].astro index 01eae47..283f233 100644 --- a/src/pages/blog/[slug].astro +++ b/src/pages/blog/[slug].astro @@ -8,8 +8,6 @@ import { marked } from 'marked'; export async function getStaticPaths() { const posts = await getCollection('posts'); - - return posts.map(p => ({ params: { slug: p.slug @@ -18,7 +16,7 @@ export async function getStaticPaths() { title: p.data.title, publishDate: p.data.publishDate, slug: p.slug, - tags: "test", + tags: p.data.tags, content: marked.parse(p.body), readingTime: readingTime(p.body).text, } @@ -35,9 +33,9 @@ const permalink = `${Astro.site.href}blog/${slug}`;

{publishDate} ~ {readingTime}

{title}

- - - + {tags.map(item => ( + {item} + ))}

diff --git a/src/pages/blog/index.astro b/src/pages/blog/index.astro index da5199e..0a5e8f0 100644 --- a/src/pages/blog/index.astro +++ b/src/pages/blog/index.astro @@ -1,17 +1,13 @@ --- import BaseLayout from '../../layouts/BaseLayout.astro'; +import {getCollection} from "astro:content"; const title = 'Blog'; const description = 'Something that\'s supposed to be thoughts'; const permalink = `${Astro.site.href}blog`; -let allPosts = []; -try { - allPosts = await Astro.glob('../../content/posts/*.md'); -} catch (error) { - console.error('No blog posts found'); -} -allPosts = allPosts.sort((a, b) => new Date(b.frontmatter.publishDate).valueOf() - new Date(a.frontmatter.publishDate).valueOf()); +const posts= await getCollection('posts'); +const allPosts= posts.sort((a, b) => new Date(b.data.publishDate).valueOf() - new Date(a.data.publishDate).valueOf()); --- @@ -19,22 +15,22 @@ allPosts = allPosts.sort((a, b) => new Date(b.frontmatter.publishDate).valueOf()

Blog

{allPosts.length === 0 &&

No posts as of yet, hop back later!

} {allPosts.map((post, index) => { - const href = `/blog/${post.file.split('/').pop().split('.').shift()}`; + const href = `/blog/${post.slug}`; return (
{ index !== 0 &&
}

- {post.frontmatter.title} + {post.data.title}

- {post.frontmatter.tags.map(item => ( + {post.data.tags.map(item => ( {item} ))}
-

{post.frontmatter.description}

+

{post.data.description}

- — {post.frontmatter.publishDate} + — {post.data.publishDate}