Fix tags with slash in name breaking routes#475
Conversation
Tags containing "/" (e.g. "c/c++") broke because Gin's :name parameter
stops capturing at the first slash. Switch to *name wildcard parameter
which captures the full path including slashes, and trim the leading "/"
that wildcard params include. Also update templates to use .Permalink()
instead of hardcoding /tag/{{.Name}}.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #475 +/- ##
=======================================
Coverage 41.92% 41.92%
=======================================
Files 6 6
Lines 1016 1016
=======================================
Hits 426 426
Misses 561 561
Partials 29 29 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Fixes tag pages and tag links for tag names containing / by switching Gin’s tag route to a wildcard capture and normalizing the captured value; updates templates to rely on tag permalinks rather than hardcoded tag URLs.
Changes:
- Change tag route from
/tag/:nameto/tag/*nameto support slashes in tag names. - Trim the leading
/included by Gin wildcard params before querying tags. - Update post templates to use
Tag.Permalink()(via{{ .Permalink }}) for tag links.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
templates/post.html |
Uses tag permalink for tag links so names with / generate valid URLs. |
templates/post-admin.html |
Same tag-link update for the admin post view. |
goblog.go |
Switches the main server’s tag route to a wildcard parameter. |
blog/blog_test.go |
Updates test router to register the wildcard tag route. |
blog/blog.go |
Normalizes wildcard-captured tag names by trimming the leading /. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| router.GET("/posts/:yyyy/:mm/:dd/:slug", b.Post) | ||
| router.GET("/tag/:name", b.Tag) | ||
| router.GET("/tag/*name", b.Tag) | ||
| router.GET("/posts", b.Posts) |
There was a problem hiding this comment.
The updated route is intended to fix tags containing '/'. The test suite currently only exercises /tag/test and doesn’t include a regression test for a tag name with a slash (e.g. /tag/c/c++). Add a test that creates a tag with a slash in its name and asserts the handler returns 200 for that URL, so this bug can’t reappear unnoticed.
| func (b *Blog) Tag(c *gin.Context) { | ||
| tag := c.Param("name") | ||
| tag := strings.TrimPrefix(c.Param("name"), "/") | ||
| posts, err := b.getPostsByTag(c) | ||
| if err != nil { |
There was a problem hiding this comment.
Tag() now normalizes the wildcard param into tag, but getPostsByTag(c) re-reads c.Param("name") and repeats the normalization. To avoid drift (and double work), consider extracting the normalized tag name once (e.g., helper tagNameFromContext or pass tag into getPostsByTag) and use that value consistently.
Fixes #55
Summary
/(e.g.c/c++) broke because Gin's:nameroute parameter stops capturing at the first/, so/tag/c/c++only matchedc*namewildcard parameter which captures the full path including slashes/that wildcard params include from the captured valuepost.htmlandpost-admin.htmltemplates to use.Permalink()instead of hardcoding/tag/{{.Name}}Test plan
go test ./...passes/in the name (e.g.c/c++) — should load correctly/tagsindex page links still work🤖 Generated with Claude Code