Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown: 500 for YAML metadata #72

Closed
mschoebel opened this issue May 12, 2015 · 5 comments
Closed

Markdown: 500 for YAML metadata #72

mschoebel opened this issue May 12, 2015 · 5 comments
Labels
bug 🐞 Something isn't working

Comments

@mschoebel
Copy link

I played around with the markdown middleware:

Template (default.html):

<html>
  <head>
    <title>{{ .title }}</title>
  </head>
  <body>
    {{ .markdown }}
  </body>
</html>

Caddyfile:

0.0.0.0:8000

markdown / {
    ext .md
    template default default.html
}

errors error.log
log access.log

Test markdown file test_yaml.md:

  ---
  template: default
  variables:
  - title: Test
  ---

  # Headline
  Lorem ipsum dolor sit amet ...

Result when accessing http://localhost:8000/test_yaml.md is 500 Internal Server Error. There is an entry in error.log saying: [PANIC /test_yaml.md] src/github.com/mholt/caddy/middleware/markdown/process.go:66 - assignment to entry in nil map.

The other formats (JSON, TOML) work as intended.

@mholt
Copy link
Member

mholt commented May 12, 2015

(Known issue - the line number in the panic message is +2 the actual, so line 64 is actual source of panic. Still not sure why the line number is off.)

I thought I fixed this with commit 8ea98f8 - have you pulled the latest recently? (go get -u github.com/mholt/caddy should do it)

@mschoebel
Copy link
Author

Unfortunately, I get the described effect with the latest HEAD ...

I added debug statements to the load method (metadata.go) ...

func (m *Metadata) load(parsedMap map[string]interface{}) {
    fmt.Printf("-- %v\n", parsedMap)
    if template, ok := parsedMap["title"]; ok {
        m.Title, _ = template.(string)
    }
    if template, ok := parsedMap["template"]; ok {
        m.Template, _ = template.(string)
    }
    if variables, ok := parsedMap["variables"]; ok {
        fmt.Printf("-- %v\n", reflect.TypeOf(variables))
        vars, ok := variables.(map[string]interface{})
        m.Variables = vars
        fmt.Printf("-- %v - %v\n", vars, ok)
    }
}

... and get the following output:

(YAML)

-- map[template:default variables:[map[title:Test]]]
-- []interface {}
-- map[] - false

(TOML)

-- map[template:default variables:map[title:Test]]
-- map[string]interface {}
-- map[title:Test] - true

yaml.Unmarshal (in Parse of YAMLMetadataParser) seems to return something unexpected.

I might have some more time tomorrow for more debugging.

@abiosoft
Copy link

Thanks for testing this out.

I have seen the issue, the fix should be out soon.

Firstly, the YAML syntax used for variables - is for a list and not an object. Only indentation is the requirement for a nested objected. (I am also guilty of this).

Should be

 ---
  template: default
  variables:
    title: Test
  ---

Secondly, the yaml package decodes nested maps into a map[interface{}]interface{} and I was assuming a map[string]interface{}.

Thanks.

@abiosoft
Copy link

@mschoebel The fix has been merged. Let us know if it works now.

Thanks.

@mschoebel
Copy link
Author

Works. 👍

YAML is weird.

@mholt mholt added the bug 🐞 Something isn't working label May 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants