arimelody.me/model/blog.go

52 lines
1.2 KiB
Go
Raw Permalink Normal View History

2025-04-02 23:04:09 +01:00
package model
import (
"database/sql"
2025-04-02 23:04:09 +01:00
"fmt"
"html/template"
"regexp"
"strings"
"time"
)
type (
BlogPost struct {
ID string `db:"id"`
Title string `db:"title"`
Description string `db:"description"`
Visible bool `db:"visible"`
CreatedAt time.Time `db:"created_at"`
ModifiedAt sql.NullTime `db:"modified_at"`
AuthorID string `db:"author"`
Markdown string `db:"markdown"`
HTML template.HTML `db:"html"`
BlueskyActorID *string `db:"bluesky_actor"`
BlueskyPostID *string `db:"bluesky_post"`
2025-04-02 23:04:09 +01:00
}
)
func (b *BlogPost) TitleNormalised() string {
2025-04-02 23:04:09 +01:00
rgx := regexp.MustCompile(`[^a-z0-9\-]`)
return rgx.ReplaceAllString(
strings.ReplaceAll(
strings.ToLower(b.Title), " ", "-",
),
"",
)
}
func (b *BlogPost) GetMonth() string {
return fmt.Sprintf("%02d", int(b.CreatedAt.Month()))
2025-04-02 23:04:09 +01:00
}
func (b *BlogPost) PrintDate() string {
return b.CreatedAt.Format("2 January 2006, 03:04")
}
func (b *BlogPost) PrintModifiedDate() string {
if !b.ModifiedAt.Valid {
return ""
}
return b.ModifiedAt.Time.Format("2 January 2006, 03:04")
2025-04-02 23:04:09 +01:00
}