Repos / s4g / e853b2bdba
commit e853b2bdba6b21fbe560d7be55f9960286364047
Author: Nhân <hi@imnhan.com>
Date:   Mon Jul 10 16:54:32 2023 +0700

    escape Article.WebPath, also cache it

diff --git a/main.go b/main.go
index f034930..38472a3 100644
--- a/main.go
+++ b/main.go
@@ -8,6 +8,7 @@
 	"io/fs"
 	"log"
 	"net/http"
+	"net/url"
 	"path/filepath"
 	"sort"
 	"strings"
@@ -172,6 +173,7 @@ type Article struct {
 	Fs         writablefs.FS
 	Path       string
 	OutputPath string
+	webPath    string
 	DjotBody   string
 	ArticleMetadata
 }
@@ -186,11 +188,22 @@ type ArticleMetadata struct {
 }
 
 func (a *Article) WebPath() string {
-	p := a.OutputPath
-	if strings.HasSuffix(p, "/index.html") {
-		p = strings.TrimSuffix(p, "index.html")
+	if a.webPath != "" {
+		return a.webPath
 	}
-	return p
+	path := a.OutputPath
+	if strings.HasSuffix(path, "/index.html") {
+		path = strings.TrimSuffix(path, "index.html")
+	}
+
+	parts := strings.Split(path, "/")
+	escaped := make([]string, len(parts))
+	for i := 0; i < len(parts); i++ {
+		escaped[i] = url.PathEscape(parts[i])
+	}
+
+	a.webPath = strings.Join(escaped, "/")
+	return a.webPath
 }
 
 func (a *Article) WriteHtmlFile(