Display the rendered HTML output of a Markdown resource

When accessing a Resource object through sitemap, is it possible to retrieve the content of the resource (specifically the rendered HTML from the Markdown content)?

I have several files with layout: post specified in the frontmatter. They’re all located in /posts, and are composed of YAML frontmatter and Markdown content. I want to render their content in two templates: once in a post.erb template and again in a posts.erb template that (as you’ve probably guessed) renders the content of the entire post. One problem, however, is that I don’t want to just render the post.erb template several times, but instead access the content.

You can write a helper that gives you access to the content of a markdown file. Here’s a simple example you can put in your config.rb file (untested):

def get_content(page = current_page)
  if page.data.layout == 'post'
    content = page.render({layout: false})
    # alter the content as needed.
    return content
  end
end

I don’t know how you are building out your .erb templates, but if you are looping through sitemap you can pass each resource into your helper like, for example:

<% sitemap.resources.each do |current_resource| %>
  <ul>
    <li><% get_content(current_resource) %></li>
  </ul>
<% end %>

Not sure if that’s exactly what you’re looking for but hopefully it gives you some ideas.