Skip to content

Commit 323cbc9

Browse files
committed
fix: post/s plural in tags
1 parent b963e6e commit 323cbc9

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/content/config.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import { defineCollection, getCollection, z, type CollectionEntry } from 'astro:content';
22
// import { glob } from 'astro/loaders'; // not available with legacy API
33

4+
const schema = z.object({
5+
title: z.string(),
6+
date: z.coerce.date(),
7+
description: z.string().optional(),
8+
draft: z.boolean().optional(),
9+
tags: z.array(z.string()).optional()
10+
});
11+
412
const quirks = defineCollection({
513
type: 'content',
614
// loader: glob({ pattern: '**\/[^_]*.md', base: './src/quirks' }),
7-
schema: z.object({
8-
title: z.string(),
9-
date: z.coerce.date(),
10-
description: z.string().optional(),
11-
draft: z.boolean().optional(),
12-
tags: z.array(z.string()).optional()
13-
})
15+
schema,
1416
});
1517
export const collections = { quirks };
1618

19+
type Post = CollectionEntry<"quirks">;
20+
1721
export async function getAllPosts() {
18-
const posts = await getCollection("quirks", ({ data }) => {
22+
const posts: Array<Post> = await getCollection("quirks", ({ data }: { data: z.infer<typeof schema> }) => {
1923
if (import.meta.env.PROD) { // production, so hide drafts and scheduled posts
2024
if (data.draft === true) {
2125
console.log(' > skipping draft article, as env is production (title: %s)', data.title);
@@ -40,11 +44,11 @@ export async function getAllPosts() {
4044
})
4145
}
4246

43-
export async function getPostsGroupedByTags() {
47+
export async function getPostsGroupedByTags(): Promise<Map<string, Array<Post>>> {
4448
const posts = await getAllPosts();
4549
return posts.reduce(
46-
(allTags: Map<string, Array<CollectionEntry<"quirks">>>, post) => {
47-
const postTags = post.data.tags || [];
50+
(allTags: Map<string, Array<Post>>, post: Post) => {
51+
const postTags: Array<string> = post.data.tags || [];
4852
postTags.forEach((tag) => {
4953
if (!allTags.has(tag)) {
5054
allTags.set(tag, []);

src/pages/tags/index.astro

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const tags = await getPostsGroupedByTags();
1111
{
1212
Array.from(tags).map(([tag, posts]) => (
1313
<li>
14-
&mdash; <a href={`/tags/${tag}`}>{tag}</a> ({posts.length} posts)
14+
&mdash; <a href={`/tags/${tag}`}>{tag}</a> ({posts.length} post{posts.length > 1 ? 's' : ''})
1515
</li>
1616
))
1717
}

0 commit comments

Comments
 (0)