46 lines
1 KiB
Go
46 lines
1 KiB
Go
|
|
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, "-")
|
||
|
|
}
|