Skip to content

Commit 4c47702

Browse files
committed
better tags page + better post meta header + css cleanup
1 parent e462029 commit 4c47702

File tree

5 files changed

+269
-133
lines changed

5 files changed

+269
-133
lines changed

src/content.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const blog = defineCollection({
3535
title: z.string(),
3636
description: z.string(),
3737
date: z.coerce.date(),
38+
updated: z.coerce.date().optional(),
3839
tags: z.array(z.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/)).default(["others"])
3940
}),
4041
});
@@ -55,6 +56,7 @@ const notes = defineCollection({
5556
z.object({
5657
title: z.string(),
5758
date: z.coerce.date(),
59+
updated: z.coerce.date().optional(),
5860
tags: z.array(z.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/)).default(["others"])
5961
}),
6062
});

src/layouts/Post.astro

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type BlogProps = CollectionEntry<"blog">["data"];
88
type NotesProps = CollectionEntry<"notes">["data"];
99
type Props = BlogProps | NotesProps;
1010
11-
const { title, date, tags } = Astro.props;
11+
const { title, date, updated, tags } = Astro.props;
1212
1313
// Type guard to check if this is a blog post (has description)
1414
const isBlogPost = (props: Props): props is BlogProps => {
@@ -18,20 +18,38 @@ const isBlogPost = (props: Props): props is BlogProps => {
1818
const description = isBlogPost(Astro.props) ? Astro.props.description : SITE_DESCRIPTION;
1919
const image = isBlogPost(Astro.props) ? `${Astro.url.pathname}og.png` : undefined;
2020
21-
const isoDate = date.toISOString().split('T')[0]
21+
const isoDate = date.toISOString().split('T')[0];
22+
const isoUpdated = updated?.toISOString().split('T')[0];
2223
---
2324

2425
<BaseLayout title={title} description={description} image={image} type="article">
2526
<article>
2627
<h1>{ title }</h1>
2728

28-
<ul class="post-metadata">
29-
<li><time datetime={isoDate}>{isoDate}</time></li>
30-
31-
{ tags.map((tag, i) => (
32-
<li><a href={`/tags/${tag}`} class="post-tag">{tag}</a>{ (i < tags.length - 1) ? ', ' : ''}</li>
33-
)) }
34-
</ul>
29+
<div class="post-metadata">
30+
<div class="post-date">
31+
<span class="meta-label">Published at:</span>
32+
<time datetime={isoDate}>{isoDate}</time>
33+
</div>
34+
35+
{ updated && (
36+
<div class="post-updated">
37+
<span class="meta-label">Updated at:</span>
38+
<time datetime={isoUpdated}>{isoUpdated}</time>
39+
</div>
40+
) }
41+
42+
{ tags.length > 0 && (
43+
<div class="post-tags">
44+
<span class="meta-label">Tags:</span>
45+
<div class="tag-list">
46+
{ tags.map((tag) => (
47+
<a href={`/tags/${tag}`} class="post-tag">#{tag}</a>
48+
)) }
49+
</div>
50+
</div>
51+
) }
52+
</div>
3553

3654
<slot />
3755

src/pages/tags/[...slug].astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const { tag, posts, notes } = Astro.props;
2929
---
3030

3131
<BaseLayout title={SITE_TITLE} description={SITE_DESCRIPTION} type="website">
32-
<h1>Tag <code>{tag}</code></h1>
32+
<h1>Tag <span class="tag-display">#{tag}</span></h1>
3333

3434
<h2>Posts</h2>
3535
<PostsList posts={posts} />

src/pages/tags/index.astro

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,40 @@ const allTags = Array.from(new Set(
1212
.flat()
1313
.filter((e) => e),
1414
));
15+
16+
// Calculate counts for each tag
17+
const tagCounts = allTags.map(tag => {
18+
const postCount = posts.filter(p => p.data.tags.includes(tag)).length;
19+
const noteCount = notes.filter(n => n.data.tags.includes(tag)).length;
20+
const totalCount = postCount + noteCount;
21+
22+
return {
23+
tag,
24+
postCount,
25+
noteCount,
26+
totalCount
27+
};
28+
}).sort((a, b) => b.totalCount - a.totalCount); // Sort by total count descending
1529
---
1630

1731
<BaseLayout title={SITE_TITLE} description={SITE_DESCRIPTION} type="website">
1832
<h1>Tags</h1>
1933

20-
<ul>
21-
{ allTags.map(tag => (
22-
<li><a href={`/tags/${tag}`}>{tag}</a></li>
34+
<ul class="tag-list-page">
35+
{ tagCounts.map(({ tag, postCount, noteCount, totalCount }) => (
36+
<li class="tag-item">
37+
<a href={`/tags/${tag}`} class="tag-display">#{tag}</a>
38+
<span class="tag-count">
39+
<span class="total-count">{totalCount} total</span>
40+
(
41+
<span class="count-breakdown">
42+
{postCount > 0 && <span class="post-count">{postCount} post{postCount !== 1 ? 's' : ''}</span>}
43+
{postCount > 0 && noteCount > 0 && <span class="separator">+</span>}
44+
{noteCount > 0 && <span class="note-count">{noteCount} note{noteCount !== 1 ? 's' : ''}</span>}
45+
</span>
46+
)
47+
</span>
48+
</li>
2349
)) }
2450
</ul>
2551
</BaseLayout>

0 commit comments

Comments
 (0)