Add slug-to-numeric resolution for blog URLs

- Modified blog handler to resolve slugs to numeric files when slug files don't exist
- This allows slug URLs to work without creating duplicate slug files
- Fixes sorting issues while maintaining slug URL compatibility
This commit is contained in:
Tomas Dvorak
2026-03-14 11:27:08 +01:00
parent f8a6abf391
commit 4f3164956a
+26
View File
@@ -1424,6 +1424,32 @@ func main() {
http.ServeFile(w, r, slugPath) http.ServeFile(w, r, slugPath)
return return
} }
// If slug file doesn't exist, try to resolve slug to numeric file
blogDir := filepath.Join(sp, "blog")
entries, err := os.ReadDir(blogDir)
if err == nil {
for _, entry := range entries {
name := entry.Name()
if !regexp.MustCompile(`^\d{4}\.html$`).MatchString(name) {
continue
}
numericPath := filepath.Join(blogDir, name)
// Check if this numeric file has the matching slug
b, err := os.ReadFile(numericPath)
if err != nil {
continue
}
s := string(b)
re := regexp.MustCompile(`(?is)<meta name="slug" content="([^"]+)"`)
m := re.FindStringSubmatch(s)
if len(m) >= 2 && m[1] == path {
http.ServeFile(w, r, numericPath)
return
}
}
}
} }
// If not found, serve 404 // If not found, serve 404