Last active
March 25, 2017 18:43
-
-
Save hashborgir/99d85e30f10ff617270c894f50110ea7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func createMetadata(archetype parser.Page, name string) (map[string]interface{}, error) { | |
archMetadata, err := archetype.Metadata() | |
if err != nil { | |
return nil, err | |
} | |
metadata, err := cast.ToStringMapE(archMetadata) | |
if err != nil { | |
return nil, err | |
} | |
var date time.Time | |
for k, v := range metadata { | |
if v == "" { | |
continue | |
} | |
lk := strings.ToLower(k) | |
switch lk { | |
case "date": | |
date, err = cast.ToTimeE(v) | |
if err != nil { | |
return nil, err | |
} | |
case "title": | |
// Use the archetype title as is | |
metadata[lk] = v | |
} | |
} | |
if metadata == nil { | |
metadata = make(map[string]interface{}) | |
} | |
if date.IsZero() { | |
date = time.Now() | |
} | |
if _, ok := metadata["title"]; !ok { | |
metadata["title"] = helpers.MakeTitle(helpers.Filename(name)) | |
} | |
metadata["date"] = date.Format(time.RFC3339) | |
keys := make([]string, 0, len(metadata)) | |
for key := range metadata { | |
keys = append(keys, key) | |
} | |
sort.Strings(keys) | |
return keys, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment