问题
I am using Markdown written in data file to render HTML in HAML template with Middleman static page generator.
My data file, data/faq.yaml
looks like this:
dog:
question: I want to know more about a dog.
answerA: (HTML in data) Just <a href="https://google.com">Google</a> it!
answerB: (Markdown link in data) Just [Google](https://google.com) it!
To convert this markdown to HTML, I am using custom helper defined in config.rb
:
helpers do
def markdown(text)
Tilt['markdown'].new { text }.render
end
end
Which then I use in my HAML template like this:
= markdown(data.faq.dog.answerA)
Everything works perfectly until I try to render markdown link written in data file.
Below is my faq.haml
template with:
- Markdown link written directly in HAML, which renders through HAML's markdown filter
- Reference to
answerA
from data with HTML link written directly - Reference to
answerB
from data with same link written with markdown syntax
faq.haml
:
.answer
:markdown
(HAML) Just [Google](https://google.com) it!
= markdown(data.faq.dog.answerA)
= markdown(data.faq.dog.answerB)
First 2 links render propely, but markdown link in answerB
throws following error:
undefined method `link_to' for #<Object:0x000000044f9e18>
Traceback:
/home/myself/.rvm/gems/ruby-2.4.0/gems/middleman-core-4.2.1/lib/middleman-core/renderers/kramdown.rb: in convert_a
48. scope.link_to(content, link, attr)
Why is Kramdown unable to convert a markdown link when "asked" to do so via custom helper which references data file, but does it properly when same syntax is used directly in HAML? What can be done, so that markdown link in = markdown(data.faq.dog.answerB)
renders properly?
Ps. My end goal will be to reference links which are already defined in separate data
file, but I think that solving this issue first is necessary in order to achieve that goal.
回答1:
App context needs to passed to Tilt.
def markdown(text)
Tilt['markdown'].new(context: @app) { text }.render
end
Source
来源:https://stackoverflow.com/questions/44240111/middleman-undefined-method-link-to-when-rendering-markdown-from-data-file