Skip to content

Commit 68be5e4

Browse files
committed
feat: validate article metadatas
1 parent d47aaac commit 68be5e4

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ require (
1111
github.com/yuin/goldmark v1.7.8
1212
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
1313
github.com/yuin/goldmark-meta v1.1.0
14+
gopkg.in/yaml.v2 v2.4.0
1415
)
1516

1617
require (
@@ -26,5 +27,4 @@ require (
2627
github.com/rivo/uniseg v0.4.7 // indirect
2728
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
2829
golang.org/x/sys v0.30.0 // indirect
29-
gopkg.in/yaml.v2 v2.4.0 // indirect
3030
)

pkg/routes/blog/article.go

Lines changed: 54 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
highlighting "github.com/yuin/goldmark-highlighting/v2"
1515
meta "github.com/yuin/goldmark-meta"
1616
mdParser "github.com/yuin/goldmark/parser"
17+
"gopkg.in/yaml.v2"
1718
)
1819

1920
type article struct {
@@ -23,7 +24,11 @@ type article struct {
2324
Uri string
2425
}
2526

26-
// TODO: Add metadata validation
27+
type articleMetadata struct {
28+
Title string `yaml:"title"`
29+
Date ArticleDate `yaml:"date"`
30+
Uri string `yaml:"uri"`
31+
}
2732

2833
func NewArticle(file fs.File) (*article, error) {
2934

@@ -33,26 +38,40 @@ func NewArticle(file fs.File) (*article, error) {
3338
return nil, fmt.Errorf("Could not convert to markdown: %s", err)
3439
}
3540

36-
metadata := meta.Get(context)
37-
38-
date, err := time.Parse("2006-01-02", metadata["date"].(string))
41+
metadata, err := getMetadata(context)
3942
if err != nil {
40-
return nil, fmt.Errorf("Could not parse as date : %s", err)
43+
return nil, fmt.Errorf("Could not get article metadata: %s", err)
4144
}
4245

43-
uri, err := makeArticleUri(date, metadata)
46+
uri, err := makeArticleUri(*metadata)
4447
if err != nil {
4548
return nil, fmt.Errorf("Could make article uri: %s", err)
4649
}
4750

4851
return &article{
49-
Title: metadata["title"].(string),
50-
Date: date,
52+
Title: metadata.Title,
53+
Date: metadata.Date.Time,
5154
Content: buf.Bytes(),
52-
Uri: uri,
55+
Uri: uri,
5356
}, nil
5457
}
5558

59+
func getMetadata(context mdParser.Context) (*articleMetadata, error) {
60+
metadata, err := meta.TryGetItems(context)
61+
if err != nil {
62+
return nil, fmt.Errorf("Could not get article metadata: %s", err)
63+
}
64+
65+
out, err := yaml.Marshal(metadata)
66+
if err != nil {
67+
return nil, fmt.Errorf("Could not Marshal metadata: %s", err)
68+
}
69+
var final articleMetadata
70+
err = yaml.Unmarshal(out, &final)
71+
72+
return &final, err
73+
}
74+
5675
func fileToMarkdown(file fs.File, buf *bytes.Buffer) (mdParser.Context, error) {
5776
info, err := file.Stat()
5877
if err != nil {
@@ -81,17 +100,34 @@ func fileToMarkdown(file fs.File, buf *bytes.Buffer) (mdParser.Context, error) {
81100
return context, nil
82101
}
83102

84-
func makeArticleUri(date time.Time, metadata map[string]any) (string, error) {
85-
86-
metaUri := metadata["uri"].(string)
87-
88-
if metaUri == "" {
89-
metaTitle := metadata["title"].(string)
90-
if metaTitle == "" {
103+
func makeArticleUri(metadata articleMetadata) (string, error) {
104+
uri := metadata.Uri
105+
if uri == "" {
106+
if metadata.Title == "" {
91107
return "", fmt.Errorf("Either one of 'uri' or 'title' needs to be set")
92108
}
93-
metaUri = url.QueryEscape(strings.ReplaceAll(strings.ToLower(metaTitle), " ", "-"))
109+
uri = url.QueryEscape(strings.ReplaceAll(strings.ToLower(metadata.Title), " ", "-"))
94110
}
95111

96-
return fmt.Sprintf("%s/%s", date.Format("2006/01"), metaUri), nil
112+
return fmt.Sprintf("%s/%s", metadata.Date.Time.Format("2006/01"), uri), nil
113+
}
114+
115+
type ArticleDate struct {
116+
Time time.Time
117+
}
118+
119+
func (t *ArticleDate) UnmarshalYAML(unmarshal func(any) error) error {
120+
121+
var buf string
122+
err := unmarshal(&buf)
123+
if err != nil {
124+
return fmt.Errorf("Could not unmarshal date: %s", err)
125+
}
126+
127+
tt, err := time.Parse("2006-01-02", strings.TrimSpace(buf))
128+
if err != nil {
129+
return fmt.Errorf("Could not parse date: %s", err)
130+
}
131+
t.Time = tt
132+
return nil
97133
}

pkg/routes/blog/blog.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ func getArticles(filesystem fs.ReadDirFS) []article {
7070
continue
7171
}
7272
a, err := NewArticle(file)
73-
if err == nil || a != nil {
74-
result = append(result, *a)
73+
if err != nil {
74+
log.Fatal("Error building article", "name", entry.Name(), "reason", err)
75+
continue
7576
}
77+
result = append(result, *a)
7678
}
7779
return result
7880
}

0 commit comments

Comments
 (0)