Remove punctuation from blog IDs

This commit is contained in:
Caleb Gardner
2025-01-26 06:52:45 -06:00
parent cd871c7217
commit 11a39a53cb
2 changed files with 8 additions and 2 deletions
+1 -2
View File
@@ -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, "<p>Title is not unique!</p>", "", "")
return
+7
View File
@@ -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)