move / -> /index.html convention to storage layer
Alan Pearce alan@alanpearce.eu
Sun, 26 Jan 2025 14:34:58 +0100
2 files changed, 17 insertions(+), 9 deletions(-)
M internal/builder/builder.go → internal/builder/builder.go
@@ -77,7 +77,7 @@ log.Debug("rendering tags list") if err := storage.RenderToFile( templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"), - path.Join("tags", "index.html"), + "tags/", ); err != nil { return nil, err } @@ -94,7 +94,7 @@ log.Debug("rendering tags page", "tag", tag) url := "/tags/" + tag if err := storage.RenderToFile( templates.TagPage(config, tag, matchingPosts, url), - path.Join("tags", tag, "index.html"), + path.Join("tags", tag)+"/", ); err != nil { return nil, err } @@ -118,7 +118,7 @@ log.Debug("rendering list page") if err := storage.RenderToFile( templates.ListPage(config, posts, "/post"), - path.Join("post", "index.html"), + "post/", ); err != nil { return nil, err } @@ -160,14 +160,14 @@ content, err := content.RenderMarkdown(text) if err != nil { return nil, err } - if err := storage.RenderToFile(templates.Homepage(config, posts, content), "index.html"); err != nil { + if err := storage.RenderToFile(templates.Homepage(config, posts, content), "/"); err != nil { return nil, err } // it would be nice to set LastMod here, but using the latest post // date would be wrong as the homepage has its own content file // without a date, which could be newer sitemap.AddPath("/", time.Time{}) - f, err := storage.Open("index.html") + f, err := storage.Open("/") if err != nil { return nil, err }
M internal/storage/files/writer.go → internal/storage/files/writer.go
@@ -7,6 +7,7 @@ "io" "io/fs" "os" "path/filepath" + "strings" "go.alanpearce.eu/x/log" @@ -70,11 +71,11 @@ }) } func (f *Files) Open(filename string) (io.ReadCloser, error) { - return os.Open(filepath.Join(f.outputDirectory, filename)) + return os.Open(f.join(filename)) } func (f *Files) OutputToFile(output io.Reader, filename string) error { - fn := filepath.Join(f.outputDirectory, filename) + fn := f.join(filename) if err := f.mkdirp(filepath.Dir(filename)); err != nil { return err } @@ -93,7 +94,7 @@ return nil } func (f *Files) RenderToFile(component templ.Component, filename string) error { - fn := filepath.Join(f.outputDirectory, filename) + fn := f.join(filename) if err := f.mkdirp(filepath.Dir(filename)); err != nil { return err } @@ -112,7 +113,7 @@ return nil } func (f *Files) WriterToFile(writer io.WriterTo, filename string) error { - fn := filepath.Join(f.outputDirectory, filename) + fn := f.join(filename) if err := f.mkdirp(filepath.Dir(filename)); err != nil { return err } @@ -209,3 +210,10 @@ err := os.MkdirAll(filepath.Join(f.outputDirectory, dir), 0755) return errors.WithMessage(err, "could not create directory") } + +func (f *Files) join(filename string) string { + if strings.HasSuffix(filename, "/") { + filename = filename + "index.html" + } + return filepath.Join(f.outputDirectory, filename) +}