From da2a2ebc04d2531194c4d5e0ac8e398ad15bba34 Mon Sep 17 00:00:00 2001 From: Angad Behl <77907286+slashtechno@users.noreply.github.com> Date: Tue, 25 Jun 2024 15:08:09 +0000 Subject: [PATCH] fix: If a frontmatter field is explicitly set to empty, don't add it --- internal/platforms/frontmatter.go | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/internal/platforms/frontmatter.go b/internal/platforms/frontmatter.go index 6cee7a7..4f3cc69 100644 --- a/internal/platforms/frontmatter.go +++ b/internal/platforms/frontmatter.go @@ -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{}