From 39b17ddd3064cd777ddbcca4063455a2ee2bc31b Mon Sep 17 00:00:00 2001 From: "fogtype.com" Date: Thu, 17 Apr 2025 15:28:17 +0000 Subject: [PATCH] Fix atom feed link construction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This pull request introduces a helper function `newAtomLink` to correctly construct Atom feed links using subdomains and slugs. Previously, the feed link was generated using `filepath.Join`, which is intended for file paths and caused malformed URLs like: ``` https://example.comfoo/bar ``` Now, the link is properly constructed in the format: ``` https://./ ``` Example: ```go newAtomLink("https://example.com", "blog", "some-post") // → ``` This change improves the correctness and clarity of Atom feed generation by replacing inappropriate path logic with URL-aware composition via url.Parse. --- atom/feed.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/atom/feed.go b/atom/feed.go index 73e8470..85a6506 100644 --- a/atom/feed.go +++ b/atom/feed.go @@ -3,6 +3,7 @@ package atom import ( "encoding/xml" "fmt" + "net/url" "path/filepath" "time" @@ -80,8 +81,7 @@ func NewAtomFeed(srcDir string, posts []types.Post) ([]byte, error) { dateStr, filepath.Join(srcDir, p.Meta["slug"].(string)), ), - // filepath.Join strips the second / in http:// - Link: &AtomLink{Href: config.Config.URL + filepath.Join(srcDir, p.Meta["slug"].(string))}, + Link: newAtomLink(config.Config.URL, srcDir, p.Meta["slug"].(string)), Summary: &AtomSummary{ Content: summaryContent, Type: "html", @@ -113,3 +113,21 @@ func NewAtomFeed(srcDir string, posts []types.Post) ([]byte, error) { // Add the header. return []byte(xml.Header + string(feedXML)), nil } + +// Creates a new Atom link. +// +// Example: +// +// newAtomLink("https://example.com", "blog", "some-post") +// // → +func newAtomLink(base string, subdomain string, slug string) *AtomLink { + baseURL, err := url.Parse(base) + if err != nil { + return nil + } + + baseURL.Host = subdomain + "." + baseURL.Host + baseURL.Path = slug + + return &AtomLink{Href: baseURL.String()} +} -- 2.51.2