@@ -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
1920type 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
2833func 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+
5675func 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}
0 commit comments