Skip to content

Commit

Permalink
fix: If a frontmatter field is explicitly set to empty, don't add it
Browse files Browse the repository at this point in the history
  • Loading branch information
slashtechno authored Jun 25, 2024
1 parent 87a5ecf commit da2a2eb
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions internal/platforms/frontmatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,27 @@ func FrontmatterMappingFromInterface(m interface{}) (*FrontmatterMapping, error)
}
// Assert that each key is a string. If any are not, return an error
for k, v := range frontmatterMapping {
strValue, ok := v.(string)
_, ok := v.(string)
if !ok {
return nil, fmt.Errorf("frontmatter mapping key '%s' is not a string", k)
}
// Use the GetMappingOrDefault function to ensure a default value is used if necessary
frontmatterMapping[k] = GetMappingOrDefault(k, strValue)
}
// Add any missing keys from the default FrontMatterMappings
for k, v := range FrontMatterMappings {
if _, ok := frontmatterMapping[k]; !ok {
frontmatterMapping[k] = v
}
}

return &FrontmatterMapping{
Title: GetMappingOrDefault("title", "defaultTitle"),
Date: GetMappingOrDefault("date", "defaultDate"),
LastUpdated: GetMappingOrDefault("date_updated", "defaultLastUpdated"),
Description: GetMappingOrDefault("description", "defaultDescription"),
CanonicalURL: GetMappingOrDefault("canonical_url", "defaultCanonicalURL"),
Title: frontmatterMapping["title"].(string),
Date: frontmatterMapping["date"].(string),
LastUpdated: frontmatterMapping["date_updated"].(string),
CanonicalURL: frontmatterMapping["canonical_url"].(string),
Description: frontmatterMapping["description"].(string),
}, nil
}

// GetMappingOrDefault returns the value for a given key from FrontMatterMappings.
// If the key does not exist, it returns a default value.
func GetMappingOrDefault(key, defaultValue string) string {
if value, exists := FrontMatterMappings[key]; exists {
return value
}
return defaultValue
}

// Take a map and return a Frontmatter struct, taking FrontmatterMapping into account
func FrontmatterFromMap(m map[string]interface{}, frontmatterMapping FrontmatterMapping) *Frontmatter {
frontmatterObjet := &Frontmatter{}
Expand Down

0 comments on commit da2a2eb

Please sign in to comment.