From 7755bf50431dacbff913dbf87e055f791ba4b97a Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Sun, 22 Mar 2026 17:58:37 +1100 Subject: [PATCH] feat(pages): add Page model and slug generator Co-Authored-By: Claude Sonnet 4.6 --- models/pages.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ models/pages_test.go | 26 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 models/pages.go create mode 100644 models/pages_test.go diff --git a/models/pages.go b/models/pages.go new file mode 100644 index 0000000..1022120 --- /dev/null +++ b/models/pages.go @@ -0,0 +1,45 @@ +package models + +import ( + "strings" + "time" + "unicode" +) + +const ( + PageTypeNormal = 0 +) + +type Page struct { + ID int64 `json:"id"` + SiteID int64 `json:"site_id"` + GUID string `json:"guid"` + Title string `json:"title"` + Slug string `json:"slug"` + Body string `json:"body"` + PageType int `json:"page_type"` + ShowInNav bool `json:"show_in_nav"` + SortOrder int `json:"sort_order"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + +// GeneratePageSlug creates a URL-safe slug from a page title. +// e.g. "About Me" -> "about-me" +func GeneratePageSlug(title string) string { + var sb strings.Builder + prevDash := false + for _, c := range strings.TrimSpace(title) { + if unicode.IsLetter(c) || unicode.IsNumber(c) { + sb.WriteRune(unicode.ToLower(c)) + prevDash = false + } else if unicode.IsSpace(c) || c == '-' || c == '_' { + if !prevDash && sb.Len() > 0 { + sb.WriteRune('-') + prevDash = true + } + } + } + result := sb.String() + return strings.TrimRight(result, "-") +} diff --git a/models/pages_test.go b/models/pages_test.go new file mode 100644 index 0000000..831b31f --- /dev/null +++ b/models/pages_test.go @@ -0,0 +1,26 @@ +package models_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "lmika.dev/lmika/weiro/models" +) + +func TestGeneratePageSlug(t *testing.T) { + tests := []struct { + title string + want string + }{ + {"About Me", "about-me"}, + {" Contact Us ", "contact-us"}, + {"Hello---World", "hello-world"}, + {"FAQ", "faq"}, + {"", ""}, + } + for _, tt := range tests { + t.Run(tt.title, func(t *testing.T) { + assert.Equal(t, tt.want, models.GeneratePageSlug(tt.title)) + }) + } +}