From e82092019ab1050d688bbce835c568ccbedd9b29 Mon Sep 17 00:00:00 2001 From: Roman Kashitsyn Date: Wed, 17 Jan 2024 22:31:38 +0100 Subject: [PATCH] fix(seo): use absolute URLs as canonical links --- blogware/layout.go | 28 ++++++++++++++++------------ blogware/main.go | 3 ++- blogware/render.go | 21 +++++++++++---------- post.tmpl | 2 +- 4 files changed, 30 insertions(+), 24 deletions(-) diff --git a/blogware/layout.go b/blogware/layout.go index 22e55cd..b044ef3 100644 --- a/blogware/layout.go +++ b/blogware/layout.go @@ -50,14 +50,14 @@ var SiteLayout = []LayoutEntry{ {Path: "/index.html", Type: IndexPage}, } -func handleAtomRender(w http.ResponseWriter, r *http.Request) { +func handleAtomRender(w http.ResponseWriter, _ *http.Request) { articles, err := AllArticles() if err != nil { w.WriteHeader(500) fmt.Fprintf(w, "Failed to list posts: %v", err) return } - feed, err := RenderAtomFeed("https://mmapped.blog", articles) + feed, err := RenderAtomFeed(rootURL, articles) if err != nil { w.WriteHeader(500) fmt.Fprintf(w, "Failed to render atom feed: %v", err) @@ -125,15 +125,16 @@ func renderPostAt(i int, articles []Article) (contents []byte, err error) { nextPost = &articles[i+1] } ctx := PostRenderContext{ - Title: article.Title, - CreatedAt: article.CreatedAt, - ModifiedAt: article.ModifiedAt, - Keywords: article.Keywords, - URL: article.URL, - Toc: toc, - Body: body, - PrevPost: prevPost, - NextPost: nextPost, + AbsoluteURL: rootURL + article.URL, + Title: article.Title, + CreatedAt: article.CreatedAt, + ModifiedAt: article.ModifiedAt, + Keywords: article.Keywords, + URL: article.URL, + Toc: toc, + Body: body, + PrevPost: prevPost, + NextPost: nextPost, } tmpl, err := getTemplate("post") if err != nil { @@ -333,6 +334,9 @@ func copyFile(src, dst string) error { func copyRecursively(src, dst string) error { return filepath.Walk(src, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } if p == src { return nil } @@ -422,7 +426,7 @@ func RenderSite() error { } case AtomXMLFeed: log.Printf("Rendering %s", e.Path) - feed, err := RenderAtomFeed("https://mmapped.blog", articles) + feed, err := RenderAtomFeed(rootURL, articles) if err != nil { return fmt.Errorf("failed to render the atom feed: %w", err) } diff --git a/blogware/main.go b/blogware/main.go index 5883181..2c4ec73 100644 --- a/blogware/main.go +++ b/blogware/main.go @@ -17,6 +17,7 @@ var ( inputDir string outputDir string renderFile string + rootURL string ) func getTemplate(name string) (tmpl *template.Template, err error) { @@ -36,7 +37,6 @@ func getTemplate(name string) (tmpl *template.Template, err error) { func parseArticle(path string) (article Article, err error) { stream, err := StreamFromFile(path) - if err != nil { err = fmt.Errorf("failed to read input file %s: %w", path, err) return @@ -96,6 +96,7 @@ func main() { flag.StringVar(&inputDir, "input", ".", "the path to the source root") flag.StringVar(&outputDir, "output", "", "the destination directory for static pages") flag.StringVar(&renderFile, "f", "", "render the specified file to stdout and exit") + flag.StringVar(&rootURL, "root", "https://mmapped.blog", "the root URL of the blog") flag.Parse() diff --git a/blogware/render.go b/blogware/render.go index dabc084..a8ca1e7 100644 --- a/blogware/render.go +++ b/blogware/render.go @@ -45,16 +45,17 @@ type PageRenderContext struct { } type PostRenderContext struct { - Title string - Subtitle string - CreatedAt time.Time - ModifiedAt time.Time - Keywords []string - URL string - Toc []TocSection - Body template.HTML - PrevPost *Article - NextPost *Article + AbsoluteURL string + Title string + Subtitle string + CreatedAt time.Time + ModifiedAt time.Time + Keywords []string + URL string + Toc []TocSection + Body template.HTML + PrevPost *Article + NextPost *Article } func (a *Article) Toc() (sections []TocSection, err error) { diff --git a/post.tmpl b/post.tmpl index 0835b23..6772453 100644 --- a/post.tmpl +++ b/post.tmpl @@ -14,7 +14,7 @@ - +