Fixes metadata fallback strategy

This commit is contained in:
Eduardo Cuducos
2021-08-05 17:26:02 -04:00
committed by Maas Lalani
parent 1968b71eee
commit 45ea575822

View File

@@ -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