From 11a39a53cbb0dc838992c34337f026c22bcd2e3e Mon Sep 17 00:00:00 2001 From: Caleb Gardner Date: Sun, 26 Jan 2025 06:52:45 -0600 Subject: [PATCH] Remove punctuation from blog IDs --- editor.go | 3 +-- internal/blog/blog.go | 7 +++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/editor.go b/editor.go index 9862585..b87b3b9 100644 --- a/editor.go +++ b/editor.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "net/http" - "strings" "text/template" "time" @@ -220,7 +219,7 @@ func editorPost(w http.ResponseWriter, r *http.Request) { return } if newBlog.ID == "" { - newBlog.ID = strings.TrimSpace(strings.ToLower(strings.ReplaceAll(newBlog.Title, " ", "-"))) + newBlog.ID = newBlog.IDFromTitle() if blogApp.Contains(r.Context(), newBlog.ID) { sendContent(w, r, "

Title is not unique!

", "", "") return diff --git a/internal/blog/blog.go b/internal/blog/blog.go index c1f74c3..88980d3 100644 --- a/internal/blog/blog.go +++ b/internal/blog/blog.go @@ -6,7 +6,9 @@ import ( "fmt" "log" "net/http" + "regexp" "strconv" + "strings" "time" "github.com/CalebQ42/darkstorm-server/internal/backend" @@ -59,6 +61,11 @@ func (b *Blog) HTMX(blogApp *BlogApp, ctx context.Context) string { return out } +func (b Blog) IDFromTitle() string { + id := strings.Join(regexp.MustCompile("([A-z]| |[0-9])*").FindAllString(b.Title, -1), "") + return strings.ReplaceAll(id, " ", "-") +} + func (b *BlogApp) ConvertBlog(blog *Blog) { if !blog.StaticPage { blog.HTMLBlog = b.conv.HTMLConvert(blog.RawBlog)