feat(pages): add Page model and slug generator
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
f17597e4b8
commit
7755bf5043
45
models/pages.go
Normal file
45
models/pages.go
Normal file
|
|
@ -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, "-")
|
||||||
|
}
|
||||||
26
models/pages_test.go
Normal file
26
models/pages_test.go
Normal file
|
|
@ -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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue