my Middleman structure looks like this:
source/
blog/
post-one.md
post-two.md
blog.erb
index.md
my config.rb
defines this helper:
helpers do
TOP_LEVEL_DIR = Dir.pwd
def posts
files = Dir["#{TOP_LEVEL_DIR}/source/blog/*"]
files.map do |file|
created_at = `git log --follow --date=short --pretty=format:%ad --diff-filter=A -- #{file}`
basename = File.basename(file).split('.')[0]
{
date: created_at,
link: '/blog/' + basename,
title: basename.gsub('-', ' ').capitalize
}
end
end
end
And my blog.erb
looks like this:
<ul>
<% posts.each do |post| %>
<li><%=post[:date]%>: <%= link_to post[:title], post[:link] %></li>
<% end %>
</ul>
This works really well for me, but I'm missing one thing. I want to display the created_at
metadata that I've defined in my custom helper in the layouts for post-one
and post-two
.
Usually this is done by defining Frontmatter, but I don't want to manually input the dates of each post when they are available in git.
So I need a way to define a custom helper that allows me to access the current_page
meta data. Or some other way to pass in the metadata I'm manually creating in the posts
helper into the layout.
This was much more straightforward than I thought it would be. current_page
is available in helpers, so I can use it directly in my helper like this in config.rb
:
helpers do
def created_at
# `git log --follow --date=short --pretty=format:%ad --diff-filter=A -- #{current_page.source_file}`
end
end