arimelody-web/model/blog.go

63 lines
1.6 KiB
Go

package model
import (
"fmt"
"regexp"
"strings"
"time"
)
type (
BlueskyRecord struct {
ActorDID string `json:"actor"`
RecordID string `json:"record"`
}
FediverseActivity struct {
AccountID string `json:"account"`
StatusID string `json:"status"`
}
BlogAuthor struct {
ID string `json:"id"`
DisplayName string `json:"display_name"`
AvatarURL string `json:"avatar_url"`
}
BlogPost struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Visible bool `json:"visible"`
PublishDate time.Time `json:"publish_date"`
Author BlogAuthor `json:"author"`
Markdown string `json:"markdown"`
Bluesky *BlueskyRecord `json:"bluesky"`
Fediverse *FediverseActivity `json:"fediverse"`
}
)
func (b *BlogPost) TitleNormalised() string {
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.PublishDate.Month()))
}
func (b *BlogPost) PrintDate() string {
return b.PublishDate.Format("02 January 2006, 15:04")
}
func (b *BlogPost) PrintShortDate() string {
return b.PublishDate.Format("2 Jan 2006")
}
func (b *BlogPost) TextPublishDate() string {
return b.PublishDate.Format("2006-01-02T15:04")
}