From 45ea575822f0fabc1016d50a08a8b770901401bf Mon Sep 17 00:00:00 2001 From: Eduardo Cuducos Date: Thu, 5 Aug 2021 17:26:02 -0400 Subject: [PATCH] Fixes metadata fallback strategy --- internal/meta/meta.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/internal/meta/meta.go b/internal/meta/meta.go index de9971f..77744b4 100644 --- a/internal/meta/meta.go +++ b/internal/meta/meta.go @@ -6,10 +6,19 @@ import ( "gopkg.in/yaml.v2" ) +const defaultTheme = "default" + +// Temporary structure to differentiate values not present in the YAML header +// from values set to empty strings in the YAML header. We replace values not +// set by defaults values when parsing a header. +type parsedMeta struct { + Theme *string `yaml:"theme"` +} + // Meta contains all of the data to be parsed // out of a markdown file's header section type Meta struct { - Theme string `yaml:"theme"` + Theme string } // New creates a new instance of the @@ -24,20 +33,18 @@ func New() *Meta { // If no front matter is provided, it will fallback to the default theme and // return false to acknowledge that there is no front matter in this slide func (m *Meta) Parse(header string) (*Meta, bool) { - fallback := &Meta{ - Theme: "default", - } + fallback := &Meta{Theme: defaultTheme} - err := yaml.Unmarshal([]byte(header), &m) + var tmp parsedMeta + err := yaml.Unmarshal([]byte(header), &tmp) if err != nil { return fallback, false } - // This fixes a bug where the first slide of a presentation won't show up if - // the first slide is valid YAML (i.e. "# Header") - // FIXME: This only works because we currently only have one option (theme), - if m.Theme == "" { - return fallback, false + if tmp.Theme != nil { + m.Theme = *tmp.Theme + } else { + m.Theme = fallback.Theme } return m, true