Get Frontmatter Content (all the parts minus the data)

If I have a Markdown file like this:

---
title: The Title
---

# Some other title

Some content.

it is easy to get the Frontmatter data with current_page.data.title but how would I get just the:

# Some other title

Some content

Part of the file. I tried current_page.content but that did not work. In the end I ended up using current_page.source_file and then manually stripping out the Frontmatter part but that is just ugly. It seems that Frontmatter should return both the data and the other content as well. I checked the Frontmatter code but most of the methods needed for this were private. I was just wondering if there is an obvious solution I am missing due to being newer to Middleman. If not do other people think this would be useful I could create a pull request to make current_page.content return just the Markdown content of the page minus the Frontmatter data.

The reason I need this is I want to parse out a table of contents for my page. The TOC generator though chokes on the Frontmatter data so I have to pass it clean Markdown.

Thanks
David

Perhaps not exactly what you’re looking for, but the body (the part that’s not the frontmatter) of the source file is available within the before_render hook.

If your goal is to add a table of contents by modifying the Markdown, you could do something like:

before_render do |body, path, locs, template_class|
  next body unless template_class == Middleman::Renderers::KramdownTemplate

  markdown_toc_for(body) + body
end

You can also just use yield. Example:

<%= your_helper yield %>

This is an old thread, but in case anyone stumbles upon it, here is the snippet I use:

content = Middleman::Util::Data.parse(resource.file_descriptor, config[:frontmatter_delims])[1]
1 Like