Organize Blog Posts Into Sub-Directories

Is it possible to organize blog posts into a “blog” subdirectory? I’m positive that it is, but I just can’t seem to get this setup to work:

I have source/_posts/2015-07-12-a-good-bug-report.md. If I move it from _posts to just residing in source, it works perfectly. BUT I don’t want to have a thousand blog posts cluttering everything up.

In my config.rb file I have the following:

activate :blog do |blog|
  blog.sources = "_posts/{year}-{month}-{day}-{title}.md"
end
activate :directory_indexes

I haven’t done anything else weird that may be affecting it that I know of.

Folder and file names starting with underscore _ have special meaning in Middleman, don’t use it for normal organization of data. Use posts instead. An excerpt from my config.rb:

activate :blog do |blog|
  blog.permalink = "{year}/{month}-{day}-{title}.html"
  blog.sources = "{year}/{year}-{month}-{day}-{title}.html"
  blog.taglink = "tagi/{tag}.html"
  blog.layout = "article_layout"

  blog.tag_template = "tag.html"
  blog.year_template = "calendar.html"

  # Custom collection
  blog.custom_collections = {
    category: {
      link: '/kategorie/{category}.html',
      template: '/category.html'
    }
  }

My blog posts reside in subfolders: 2014, 2015 and next year I will have 2016, accordingly. Your config should be:

blog.sources = "posts/{year}-{month}-{day}-{title}.md"
1 Like

What does you category template look like?

There are some unrelated details and custom sorting (due to problems with unicode-sorting of Polish diacritic ł), but it looks like this:

<ul>
  <% blog.articles.group_by {|a| a.data.category}.sort_alphabetical_by {|s| s[0].gsub('ł', 'lx')}.each do |category, articles| %>
    <li>
      <%= link_to "#{category}", category_path(category), class: 'category_link' %> <span class="count_badge"><%= articles.size %></span><br />
      <ul>
        <% articles.each do |article| %>
          <li><%= link_to UnicodeUtils.downcase(article.title), article, class: 'article_link' %> <span class="article_date"><%= strftime_pl(article.date, true) %></span></li>
        <% end %>
      </ul>
    </li>
  <% end %>
</ul>

The Unicode gems in use are:

gem 'unicode_utils'       # https://github.com/lang/unicode_utils
gem 'sort_alphabetical'   # https://github.com/grosser/sort_alphabetical
1 Like