From 8394d72f480ac22a86b858b182e198a86a92ee9c Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Wed, 13 May 2015 00:44:35 +0100 Subject: [PATCH] Fix for Issue 72: Markdown: 500 for YAML metadata --- middleware/markdown/metadata.go | 13 +++++++++++++ middleware/markdown/metadata_test.go | 10 +++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/middleware/markdown/metadata.go b/middleware/markdown/metadata.go index afab4daa..0f6ef9e8 100644 --- a/middleware/markdown/metadata.go +++ b/middleware/markdown/metadata.go @@ -154,6 +154,19 @@ func (y *YAMLMetadataParser) Parse(b []byte) ([]byte, error) { if err := yaml.Unmarshal(b, &m); err != nil { return markdown, err } + + // convert variables (if present) to map[string]interface{} + // to match expected type + if vars, ok := m["variables"].(map[interface{}]interface{}); ok { + vars1 := make(map[string]interface{}) + for k, v := range vars { + if key, ok := k.(string); ok { + vars1[key] = v + } + } + m["variables"] = vars1 + } + y.metadata.load(m) return markdown, nil } diff --git a/middleware/markdown/metadata_test.go b/middleware/markdown/metadata_test.go index 68659bf4..861e485b 100644 --- a/middleware/markdown/metadata_test.go +++ b/middleware/markdown/metadata_test.go @@ -35,13 +35,13 @@ var YAML = [4]string{` title : A title template : default variables : -- name : value + name : value `, `--- title : A title template : default variables : -- name : value + name : value --- Page content `, @@ -49,7 +49,7 @@ Page content title : A title template : default variables : -- name : value + name : value `, `title : A title template : default variables : name : value`, } @@ -112,7 +112,7 @@ func TestParsers(t *testing.T) { return false } } - return true + return len(m.Variables) == 1 } data := []struct { @@ -135,7 +135,7 @@ func TestParsers(t *testing.T) { md, err := v.parser.Parse([]byte(v.testData[1])) check(t, err) if !compare(v.parser.Metadata()) { - t.Fatalf("Expected %v, found %v for %v", expected, v.parser.Metadata().Variables, v.name) + t.Fatalf("Expected %v, found %v for %v", expected, v.parser.Metadata(), v.name) } if "Page content" != strings.TrimSpace(string(md)) { t.Fatalf("Expected %v, found %v for %v", "Page content", string(md), v.name)