Diffstat (limited to 'internal/blog/index.go')
| -rw-r--r-- | internal/blog/index.go | 83 |
1 files changed, 6 insertions, 77 deletions
diff --git a/internal/blog/index.go b/internal/blog/index.go index a09b59e..861a90e 100644 --- a/internal/blog/index.go +++ b/internal/blog/index.go @@ -1,17 +1,10 @@ package blog -import ( - "encoding/json" - "fmt" - "time" - - "github.com/SayaAndy/saya-today-web/internal/frontmatter" -) +import "time" const IndexFileName = "index.json" -const MedleysIndexFileName = "medleys.json" -const IndexSchemaVersion = 2 +const IndexSchemaVersion = 1 type IndexEntry struct { Link string `json:"link"` @@ -27,77 +20,13 @@ type IndexEntry struct { MedleyPart int `json:"medleyPart,omitempty"` } -func (e IndexEntry) Metadata() *frontmatter.Metadata { - return &frontmatter.Metadata{ - Title: e.Title, - ShortDescription: e.ShortDescription, - ActionDate: e.ActionDate, - PublishedTime: e.PublishedTime, - Thumbnail: e.Thumbnail, - Tags: e.Tags, - Geolocation: e.Geolocation, - Medley: e.Medley, - MedleyPart: e.MedleyPart, - } -} - -type IndexV2Category struct { - GeneratedAt time.Time `json:"generatedAt"` - Pages map[string]IndexEntry `json:"pages"` -} - -type IndexV1Category struct { +type IndexCategory struct { GeneratedAt time.Time `json:"generatedAt"` Pages []IndexEntry `json:"pages"` } type Index struct { - SchemaVersion int `json:"schemaVersion"` - GeneratedAt time.Time `json:"generatedAt"` - Categories any `json:"categories"` -} - -func (idx *Index) UnmarshalJSON(data []byte) error { - var tmp struct { - SchemaVersion int `json:"schemaVersion"` - GeneratedAt time.Time `json:"generatedAt"` - Categories json.RawMessage `json:"categories"` - } - - if err := json.Unmarshal(data, &tmp); err != nil { - return err - } - - idx.SchemaVersion = tmp.SchemaVersion - idx.GeneratedAt = tmp.GeneratedAt - - switch tmp.SchemaVersion { - case 1: - var categories map[string]*IndexV1Category - if err := json.Unmarshal(tmp.Categories, &categories); err != nil { - return fmt.Errorf("unmarshal map[string]*IndexV1Category: %w", err) - } - idx.Categories = &categories - case 2: - var categories map[string]*IndexV2Category - if err := json.Unmarshal(tmp.Categories, &categories); err != nil { - return fmt.Errorf("unmarshal map[string]*IndexV2Category: %w", err) - } - idx.Categories = &categories - default: - return fmt.Errorf("unsupported index version: %d", tmp.SchemaVersion) - } - - return nil -} - -type MedleyEntry struct { - Codename string `json:"codename"` - Localnames map[string]string `json:"localnames"` - Content []string `json:"content"` -} - -type MedleyPageEntry struct { - Codename string `json:"codename"` - Position int `json:"position"` + SchemaVersion int `json:"schemaVersion"` + GeneratedAt time.Time `json:"generatedAt"` + Categories map[string]IndexCategory `json:"categories"` } |