Skip to content

Commit d29f8d2

Browse files
committed
feat: Add preferred canonical URL for blog posts
Introduces a `PreferredCanonical` function to prioritize the English canonical URL if available, falling back to the default canonical URL. This ensures that view counts and like buttons use the most appropriate canonical URL for the blog post.
1 parent 4c1e899 commit d29f8d2

2 files changed

Lines changed: 34 additions & 10 deletions

File tree

view/component_gosuda_blog_post.templ

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import (
55
"time"
66
)
77

8+
func PreferredCanonical(meta *types.Metadata) string {
9+
if meta == nil {
10+
return ""
11+
}
12+
if meta.LangCanonical != nil {
13+
if v, ok := meta.LangCanonical["en"]; ok && v != "" {
14+
return v
15+
}
16+
}
17+
return meta.Canonical
18+
}
19+
820
templ GosudaBlogPost(m *Metadata, doc *types.Document, post *types.Post) {
921
<div class="max-w-6xl mx-auto p-4 min-h-screen flex flex-col">
1022
@BlogHeader(m)
@@ -16,9 +28,9 @@ templ GosudaBlogPost(m *Metadata, doc *types.Document, post *types.Post) {
1628
<time datetime={ doc.Metadata.Date.Format(time.RFC3339) } class="inline-block text-gray-600 italic">{ doc.Metadata.Date.Format("January 2, 2006") }</time>
1729
<div class="ml-4 flex items-center text-sm text-gray-500 gap-4">
1830
<!-- Placeholder: will hydrate on client to actual view count -->
19-
<span data-view-count data-url={ templ.SafeURL(doc.Metadata.Canonical) } aria-label="View count">views ....</span>
31+
<span data-view-count data-url={ templ.SafeURL(PreferredCanonical(&doc.Metadata)) } aria-label="View count">views ....</span>
2032
<!-- Placeholder like button: inner span will hydrate with like count -->
21-
<button type="button" data-like-button data-url={ templ.SafeURL(doc.Metadata.Canonical) } class="text-sm text-gray-500" aria-label="Like this post">
33+
<button type="button" data-like-button data-url={ templ.SafeURL(PreferredCanonical(&doc.Metadata)) } class="text-sm text-gray-500" aria-label="Like this post">
2234
<span data-like-count>like ...</span>
2335
</button>
2436
</div>

view/component_gosuda_blog_post_templ.go

Lines changed: 20 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)