This commit is contained in:
prototypa
2023-06-14 17:58:31 -04:00
3 changed files with 58 additions and 18 deletions

View File

@ -7,28 +7,35 @@ import type { Post } from '~/types';
export interface Props { export interface Props {
tags: Post['tags']; tags: Post['tags'];
class?: string; class?: string;
title?: string | undefined;
isCategory?: boolean;
} }
const { tags, class: className = 'text-sm' } = Astro.props; const { tags, class: className = 'text-sm', title = undefined, isCategory = false } = Astro.props;
--- ---
{ {
tags && Array.isArray(tags) && ( tags && Array.isArray(tags) && (
<ul class={className}> <>
{tags.map((tag) => ( <>
<li class="bg-gray-100 dark:bg-slate-700 inline-block mr-2 mb-2 py-0.5 px-2 lowercase font-medium"> {title !== undefined && <span class="align-super font-normal underline underline-offset-4 decoration-2 dark:text-slate-400">{title}</span>}
{BLOG?.tag?.disabled ? ( </>
tag <ul class={className}>
) : ( {tags.map((tag) => (
<a <li class="bg-gray-100 dark:bg-slate-700 inline-block mr-2 mb-2 py-0.5 px-2 lowercase font-medium">
href={getPermalink(tag, 'tag')} {BLOG?.tag?.disabled ? (
class="text-muted dark:text-slate-300 hover:text-primary dark:hover:text-gray-200" tag
> ) : (
{tag} <a
</a> href={getPermalink(tag, (isCategory ? 'category' : 'tag'))}
)} class="text-muted dark:text-slate-300 hover:text-primary dark:hover:text-gray-200"
</li> >
))} {tag}
</ul> </a>
)}
</li>
))}
</ul>
</>
) )
} }

View File

@ -5,8 +5,10 @@ import Layout from '~/layouts/PageLayout.astro';
import BlogList from '~/components/blog/List.astro'; import BlogList from '~/components/blog/List.astro';
import Headline from '~/components/blog/Headline.astro'; import Headline from '~/components/blog/Headline.astro';
import Pagination from '~/components/blog/Pagination.astro'; import Pagination from '~/components/blog/Pagination.astro';
// import PostTags from "~/components/blog/Tags.astro";
import { fetchPosts } from '~/utils/blog'; import { fetchPosts } from '~/utils/blog';
// import { findTags, findCategories } from '~/utils/blog';
import { BLOG_BASE } from '~/utils/permalinks'; import { BLOG_BASE } from '~/utils/permalinks';
export async function getStaticPaths({ paginate }) { export async function getStaticPaths({ paginate }) {
@ -20,6 +22,9 @@ export async function getStaticPaths({ paginate }) {
const { page } = Astro.props; const { page } = Astro.props;
const currentPage = page.currentPage ?? 1; const currentPage = page.currentPage ?? 1;
// const allCategories = await findCategories();
// const allTags = await findTags();
const meta = { const meta = {
title: `Blog${currentPage > 1 ? ` — Page ${currentPage}` : ''}`, title: `Blog${currentPage > 1 ? ` — Page ${currentPage}` : ''}`,
description: SITE.description, description: SITE.description,
@ -37,5 +42,9 @@ const meta = {
</Headline> </Headline>
<BlogList posts={page.data} /> <BlogList posts={page.data} />
<Pagination prevUrl={page.url.prev} nextUrl={page.url.next} /> <Pagination prevUrl={page.url.prev} nextUrl={page.url.next} />
<!--
<PostTags tags={allCategories} class="mb-2" header="Search by Categories:" isCategory />
<PostTags tags={allTags} header="Search by Tags:" />
-->
</section> </section>
</Layout> </Layout>

View File

@ -122,3 +122,27 @@ export const findLatestPosts = async ({ count }: { count?: number }): Promise<Ar
return posts ? posts.slice(0, _count) : []; return posts ? posts.slice(0, _count) : [];
}; };
/** */
export const findTags = async (): Promise<Array<string>> => {
const posts = await fetchPosts();
const tags = posts.reduce((acc, post: Post) => {
if (post.tags && Array.isArray(post.tags)) {
return [...acc, ...post.tags];
}
return acc;
}, []);
return [...new Set(tags)];
};
/** */
export const findCategories = async (): Promise<Array<string>> => {
const posts = await fetchPosts();
const categories = posts.reduce((acc, post: Post) => {
if (post.category) {
return [...acc, post.category];
}
return acc;
}, []);
return [...new Set(categories)];
};