From 82d0403c8839db9687f91f574962320b8dc05956 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 9 Mar 2024 14:16:58 +0100 Subject: [PATCH] Add Page Filter --- src/components/TagComponent.astro | 15 ++++++++---- src/pages/ankuendigungen/index.astro | 28 ++++++++++++++++++++++- src/pages/ankuendigungen/tags/[tag].astro | 10 ++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/components/TagComponent.astro b/src/components/TagComponent.astro index c263ac9..d2f448e 100644 --- a/src/components/TagComponent.astro +++ b/src/components/TagComponent.astro @@ -5,11 +5,18 @@ import {capitalize} from "./admin/util"; interface Props { tag: string; + noLink?: boolean; } -const {tag} = Astro.props; +const {tag, noLink} = Astro.props; --- - - {capitalize(tag)} - +{noLink + ? ( + {capitalize(tag)} + ) + : ( + + {capitalize(tag)} + + )} diff --git a/src/pages/ankuendigungen/index.astro b/src/pages/ankuendigungen/index.astro index b437ce0..cd4851e 100644 --- a/src/pages/ankuendigungen/index.astro +++ b/src/pages/ankuendigungen/index.astro @@ -4,11 +4,12 @@ import PageLayout from "../../layouts/PageLayout.astro"; import {astroI18n, t} from "astro-i18n"; import PostComponent from "../../components/PostComponent.astro"; import dayjs from "dayjs"; +import TagComponent from "../../components/TagComponent.astro"; async function getPosts() { const posts = await getCollection("announcements", entry => entry.id.split("/")[0] === astroI18n.locale); - const germanPosts = await getCollection("announcements", entry => entry.id.split("/")[0] === "de"); + const germanPosts = await getCollection("announcements", entry => entry.id.split("/")[0] === astroI18n.fallbackLocale); germanPosts.forEach(value => { if (posts.find(post => post.data.key === value.data.key)) { @@ -21,10 +22,35 @@ async function getPosts() { return posts.sort((a, b) => dayjs(b.data.created).unix() - dayjs(a.data.created).unix()).filter((value, index) => index < 20); } +async function getTags() { + const posts = await getCollection("announcements"); + const tags = new Map(); + posts.forEach(post => { + post.data.tags.forEach(tag => { + if (tags.has(tag)) { + tags.set(tag, tags.get(tag) + 1); + } else { + tags.set(tag, 1); + } + }); + }); + return Array.from(tags).sort((a, b) => b[1] - a[1]).map(value => value[0]); +} + const posts = await getPosts(); +const tags = await getTags(); --- +
+

Filter

+
+ {tags.map(tag => ( + + ))} +
+
+
{posts.map((post, index) => (
diff --git a/src/pages/ankuendigungen/tags/[tag].astro b/src/pages/ankuendigungen/tags/[tag].astro index 84ad7fb..01c541c 100644 --- a/src/pages/ankuendigungen/tags/[tag].astro +++ b/src/pages/ankuendigungen/tags/[tag].astro @@ -6,6 +6,9 @@ import PageLayout from "../../../layouts/PageLayout.astro"; import {capitalize} from "../../../components/admin/util"; import PostComponent from "../../../components/PostComponent.astro"; import dayjs from "dayjs"; +import { ArrowLeftOutline } from "flowbite-svelte-icons"; +import {l} from "../../../util/util"; +import TagComponent from "../../../components/TagComponent.astro"; export const getStaticPaths = createGetStaticPaths(async () => { let posts = (await getCollection("announcements", entry => entry.id.split("/")[0] === astroI18n.locale)); @@ -54,6 +57,13 @@ const {posts, tag} = Astro.props; --- + +
{posts.map((post, index) => (