From 9618162bae1d108f67a714b1dce7e558f42a489a Mon Sep 17 00:00:00 2001 From: Cassidy Williams Date: Thu, 1 Sep 2022 16:02:14 -0500 Subject: [PATCH] Add tags pages --- src/components/Post.astro | 2 +- src/pages/tag/[tag].astro | 86 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/pages/tag/[tag].astro diff --git a/src/components/Post.astro b/src/components/Post.astro index 1278cfd..20c19e7 100644 --- a/src/components/Post.astro +++ b/src/components/Post.astro @@ -17,7 +17,7 @@ const { slug, title, tags, description, date } = Astro.props; diff --git a/src/pages/tag/[tag].astro b/src/pages/tag/[tag].astro new file mode 100644 index 0000000..49e6b47 --- /dev/null +++ b/src/pages/tag/[tag].astro @@ -0,0 +1,86 @@ +--- +import BaseHead from '../../components/BaseHead.astro'; +import Header from '../../components/Header.astro'; +import Footer from '../../components/Footer.astro'; +import Post from '../../components/Post.astro'; + +import { SITE_TITLE, SITE_DESCRIPTION } from '../../config'; + +// @rachsmithcodes wrote this +function postTags(posts) { + return posts.reduce((allTags, post) => { + const postTags = post.frontmatter.tags; + if (postTags) { + postTags.forEach((tag) => { + if (!allTags[tag]) { + allTags[tag] = []; + } + allTags[tag].push(post); + }); + } + return allTags; + }, {}); +} + +// Credit to @rachsmithcodes for helping out my lil ol brain +export async function getStaticPaths() { + let posts = await Astro.glob(`../../posts/*.md`); + const tags = posts.reduce((allTags, post) => { + const postTags = post.frontmatter.tags; + if (postTags) { + postTags.forEach((tag) => { + if (!allTags[tag]) { + allTags[tag] = []; + } + allTags[tag].push(post); + }); + } + return allTags; + }, {}); + + console.log(tags) + + return Object.keys(tags).map((t) => { + return ({ + params: { tag: t }, + props: { tag: tags[t] }, + }) + }); +} + +const { tag } = Astro.props; + +--- + + + + + + + + +
+
+ +
    + {tag.map( + ({url, frontmatter: { description, slug, title, tags, added: date }}) => ( + + ))} +
+
+
+ +