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)