In Middleman I'd like to read some markdown files from a directory and render them. That way when a lay person adds a file they can just add the markdown file and not worry about anything else.
I have everything working except that the YAML is not being parsed. I think if I can access Middleman's FileRenderer or TemplateRenderer then the code would work.
However I can't figure out how to access them :(
Here is my code.
Main partial:
<% @photos = Dir["source/_partials/feature-pieces/*.md"] %>
<%= partial "/feature-pieces/homepageslider", :collection => @photos %>
Then in my
homepageslider
<div class="item featured<%=homepageslider_counter%>">
<div class="slider-content">
<% markdown = File.read(homepageslider) %>
<%= Kramdown::Document.new(markdown).to_html %>
</div>
</div>
<% content_for :cssScreen do %>
.owl-carousel .item.featured<%=homepageslider_counter%> {
background: url("/images/horizontal-medium1.jpg");
}
<% end %>
and last here is a markdown file:
---
background: /images/horizontal-medium1.jpg
---
## Featured Heading from md.
md
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<div class="center">
<input type="button" class="btn btn-med" value="Call To Action">
</div>
This was a lot easier than I thought to figure out.
I used the render_individual_file
method in my homepageslider
like this:
<div class="item featured<%=homepageslider_counter%>">
<div class="slider-content">
<%= render_individual_file root + "/" + homepageslider %>
</div>
</div>
It renders it perfectly.
I figured it out by using the Middleman Console and
require 'pp'
pp ::Middleman::Application.instance_methods
I also figured out how to get the frontmatter. In this case it helped me to trawl through Middleman's sourcecode instead of looking through the instance methods.
In my case I wanted the background
<div class="item featured<%=homepageslider_counter%>">
<div class="slider-content">
<%= render_individual_file root + "/" + homepageslider %>
</div>
</div>
<% content_for :cssScreen do %>
.owl-carousel .item.featured<%=homepageslider_counter%> {
background: url(
<%= self.extensions[:frontmatter].data(homepageslider.gsub(config.source + "/", ""))[0][:background] %>);
}
<% end %>
The important piece is <%= self.extensions[:frontmatter].data(homepageslider.gsub(config.source + "/", ""))[0][:background] %>
Then I made a helper method in config.rb
def getData(filePath, symbol)
fm = extensions[:frontmatter]
localFilePath = filePath.gsub(config.source + "/", "")
parsedData = fm.data(localFilePath)
fmData = parsedData[0]
fmData[symbol]
end
which I call like this:
<%= getData('source/_partials/feature-pieces/slider-1.md', :background) %>