Skip to content

Commit

Permalink
refactor: better tags listing
Browse files Browse the repository at this point in the history
So that htm, css and js can be always first in the list
  • Loading branch information
codenomnom committed Oct 11, 2024
1 parent 12239ad commit 7ec6be9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions src/content/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const quirks = defineCollection({
// loader: glob({ pattern: '**\/[^_]*.md', base: './src/quirks' }),
schema: z.object({
title: z.string(),
date: z.coerce.date().optional(),
date: z.coerce.date(),
description: z.string().optional(),
draft: z.boolean().optional(),
tags: z.array(z.string()).optional()
Expand Down Expand Up @@ -43,21 +43,25 @@ export async function getAllPosts() {
export async function getPostsGroupedByTags() {
const posts = await getAllPosts();
return posts.reduce(
(allTags: Record<string, Array<CollectionEntry<"quirks">>>, post) => {
(allTags: Map<string, Array<CollectionEntry<"quirks">>>, post) => {
const postTags = post.data.tags || [];
postTags.forEach((tag) => {
if (!allTags[tag]) {
allTags[tag] = [];
if (!allTags.has(tag)) {
allTags.set(tag, []);
}
allTags[tag].push(post);
allTags.get(tag)!.push(post);
});
return allTags;
},
{},
new Map([
['html', []],
['css', []],
['js', []],
]),
);
}

export async function getAllTags() {
const posts = await getPostsGroupedByTags();
return Object.keys(posts);
return Array.from(posts.keys());
}
2 changes: 1 addition & 1 deletion src/pages/tags/[tag].astro
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function getStaticPaths() {
const { tag } = Astro.props;
const allPosts = await getPostsGroupedByTags();
const posts = allPosts[tag] || [];
const posts = allPosts.get(tag) || [];
---

<MainLayout title=`tagged: ${tag}`>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/tags/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const tags = await getPostsGroupedByTags();
<h1>all tags</h1>
<ul>
{
Object.entries(tags).map(([tag, posts]) => (
tags.entries().map(([tag, posts]) => (
<li>
&mdash; <a href={`/tags/${tag}`}>{tag}</a> ({posts.length} posts)
</li>
Expand Down

0 comments on commit 7ec6be9

Please sign in to comment.