feat: SEO backend data (1/4)
This commit is contained in:
1 parent
cb1410f0b0
commit
0eb46b0d74
90 files changed
+6094
-350
No files matched your search
@@ -18,6 +18,8 @@ var (
|
||||
ErrInvalidLocale = errors.New("invalid locale")
|
||||
ErrUseSetDefault = errors.New("the default flag is changed through the default route")
|
||||
ErrInvalidReassign = errors.New("invalid reassign destination")
|
||||
|
||||
ErrInvalidTitleTemplate = errors.New("the title template must contain %title%")
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -30,8 +32,14 @@ const (
|
||||
MinNameRunes = 1
|
||||
MaxNameRunes = 100
|
||||
|
||||
MaxLocaleRunes = 35
|
||||
MaxBaseURLRunes = 255
|
||||
|
||||
MinKeyRunes = 2
|
||||
MaxKeyRunes = 32
|
||||
|
||||
TitlePlaceholder = "%title%"
|
||||
DefaultTitleTemplate = "%title% | %site%"
|
||||
)
|
||||
|
||||
var keyPattern = regexp.MustCompile(`^[a-z0-9]+(-[a-z0-9]+)*$`)
|
||||
@@ -49,6 +57,9 @@ type Site struct {
|
||||
Locale *string `json:"locale"`
|
||||
IsDefault bool `json:"is_default"`
|
||||
ArticleCount int `json:"article_count"`
|
||||
SiteName string `json:"site_name"`
|
||||
TitleTemplate string `json:"title_template"`
|
||||
DefaultDescription string `json:"default_description"`
|
||||
}
|
||||
|
||||
type Nullable[T any] struct {
|
||||
@@ -64,6 +75,9 @@ type Patch struct {
|
||||
Locale Nullable[string]
|
||||
DefaultSocialImageID Nullable[uint32]
|
||||
IsDefault *bool
|
||||
SiteName *string
|
||||
TitleTemplate *string
|
||||
DefaultDescription *string
|
||||
}
|
||||
|
||||
func IsValidKey(key string) bool {
|
||||
@@ -76,11 +90,18 @@ func IsValidName(name string) bool {
|
||||
return length >= MinNameRunes && length <= MaxNameRunes
|
||||
}
|
||||
|
||||
func ValidateTitleTemplate(template string) error {
|
||||
if !strings.Contains(template, TitlePlaceholder) {
|
||||
return ErrInvalidTitleTemplate
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NormalizeLocale(locale *string) (*string, error) {
|
||||
if locale == nil || *locale == "" {
|
||||
return nil, nil
|
||||
}
|
||||
if !localePattern.MatchString(*locale) {
|
||||
if utf8.RuneCountInString(*locale) > MaxLocaleRunes || !localePattern.MatchString(*locale) {
|
||||
return nil, ErrInvalidLocale
|
||||
}
|
||||
return locale, nil
|
||||
@@ -126,7 +147,11 @@ func NormalizeBaseURL(raw string) (string, error) {
|
||||
if found.scheme == "http" && !isLoopback(found.hostname) {
|
||||
return "", ErrInvalidSiteURL
|
||||
}
|
||||
return found.scheme + "://" + found.host, nil
|
||||
normalized := found.scheme + "://" + found.host
|
||||
if utf8.RuneCountInString(normalized) > MaxBaseURLRunes {
|
||||
return "", ErrInvalidSiteURL
|
||||
}
|
||||
return normalized, nil
|
||||
}
|
||||
|
||||
func SeedBaseURL(publicURL string) (string, error) {
|
||||
|
||||
Reference in new issue
Block a user