1

In a Yaml data file aaa.yml I have:

yyy: |
     [foo](http://example.com) bar.

I want to pull this data from a Haml file (zzz.html.haml).

1. If I do zzz.html.haml:

    %p
      = data.aaa.yyy
  • Middleman renders zzz.html:

    <p> [foo](http://example.com) bar</p>

2. If I do:

    %p
      :markdown
        = data.aaa.yyy
  • Middleman renders:

    <p>= data.aaa.yyy</p>

How can I make it render:

<p><a href="http://example.com">foo</a> bar</p>

?

Virtua Creative
  • 2,025
  • 1
  • 13
  • 18

2 Answers2

1

You have a string that contains markdown, and you want to render that markdown and include the result in your output.

One way would be to use the Haml :markdown filter. You can’t use normal Haml syntax inside a filter (like you’ve tried), but you can use interpolation inside #{...}, so you could do

:markdown
  #{data.aaa.yyy}

A better way, if the string is a standalone chunk, might be to create a helper method that renders markdown, and call that.

Wherever you have your helpers add something like:

def markdown s
  # I'm using kramdown here, but you can use whatever processor you prefer.
  Kramdown::Document.new(s).to_html
end

Then in your Haml:

= markdown(data.aaa.yyy)
matt
  • 78,533
  • 8
  • 163
  • 197
0

Yay! Turns out there's this helper, which was already in my config.rb but didn't work by itself:

def markdown(text)
  Tilt['markdown'].new { text }.render
end

When I installed this helper (from @matt's answer) with the first one:

def markdown s
  Kramdown::Document.new(s).to_html
end

Then = markdown(foo.bar) worked, which made my day!! :D

Community
  • 1
  • 1
Virtua Creative
  • 2,025
  • 1
  • 13
  • 18
  • This second helper doesn't reference first in any way. I don't know why the first didn't work (it does for me), but the reason why adding second one works is that it overwrites definition of `markdown` and the first one isn't used at all. You can remove first helper and nothing will change. – Rafal Aug 10 '17 at 08:54
  • And if anyone wants to use the first helper, you need to add app context in order for it to work fully in all cases. See [this answer](https://stackoverflow.com/a/44494649/1226389). – Rafal Aug 10 '17 at 08:55