🗃️ Parse and import BaseURL property in Site FS repository

This commit is contained in:
Maxim Lebedev 2023-11-09 07:07:55 +06:00
parent e3362fcd9f
commit d677254e32
Signed by: toby3d
GPG Key ID: 1F14E25B7C119FC5
1 changed files with 30 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/fs"
"net/url"
"time"
"github.com/adrg/frontmatter"
@ -18,6 +19,7 @@ type (
Site struct {
Title string `yaml:"title"`
TimeZone TimeZone `yaml:"timeZone"`
BaseURL URL `yaml:"baseUrl"`
Params map[string]any `yaml:",inline"`
Content []byte `yaml:"-"`
}
@ -26,6 +28,10 @@ type (
*time.Location `yaml:"-"`
}
URL struct {
*url.URL `yaml:"-"`
}
fileSystemSiteRepository struct {
dir fs.FS
rootPath string
@ -64,6 +70,7 @@ func (repo *fileSystemSiteRepository) Get(ctx context.Context, lang language.Tag
return &domain.Site{
Language: lang,
Title: data.Title,
BaseURL: data.BaseURL.URL,
TimeZone: data.TimeZone.Location,
Params: data.Params,
}, nil
@ -94,6 +101,29 @@ func (tz TimeZone) MarshalYAML() (any, error) {
return tz.Location.String(), nil
}
func (u *URL) UnmarshalYAML(value *yaml.Node) error {
if value.IsZero() {
return nil
}
parsed, err := url.Parse(value.Value)
if err != nil {
return fmt.Errorf("cannot parse URL value '%v': %w", value, err)
}
u.URL = parsed
return nil
}
func (u URL) MarshalYAML() (any, error) {
if u.URL == nil {
return nil, nil
}
return u.URL.String(), nil
}
func NewSite() *Site {
return &Site{
Params: make(map[string]any),