middleman / middleman-blog

Blog Engine Extension for Middleman
https://middlemanapp.com
MIT License
325 stars 179 forks source link

Iterating through blog.tags spits out extraneous frontmatter #372

Closed johanolofolsson closed 5 years ago

johanolofolsson commented 5 years ago

Hi gang,

The latest version of Middleman is great, the move to SassC has really sped things up. However for Middleman-blog, I'm getting some strange output when I try to loop through blog.tags.

Expected behavior

Following https://middlemanapp.com/basics/blogging/#listing-articles (with some minor adjustments), I'm hoping to alphabetically list all of the tags present in all posts, using blog.tags.

Actual behaviour

Although it correctly prints the current tag, it prints out other items of frontmatter as well:

["CSS", [# page="blog" tags="CSS, Theming" title="Test Title">>]]

Viewing the source, it's actually printing:

["CSS", [#<middleman::blog::blogarticle: #<middleman::util::enhancedhash="" date="2019-02-02" 00:00:00="" +0000="" hero="#<Middleman::Util::EnhancedHash" alt="Placeholder alt-text, this is not a real blog post." bg="u-color-blue-900-bg" image="test.svg" type="c-hero--textured c-hero--centered-vertically c-hero--narrow"> page="blog" tags="CSS, Theming" title="Test Title">>]]</middleman::blog::blogarticle:>

Steps to reproduce the problem

  1. Create at least one blog post, with several tags (e.g. "CSS, Theming")
  2. Create a new page (in my case /source/blog/topics/index.html.erb)
  3. Paste the following code:
<ul class="o-list-bare  c-tag-list">
<% topics = blog.tags.sort %>
<% topics.each do |topic| %>

    <li class="o-list-bare__item  c-tag-list__item">
      <a class="c-link-muted  c-tag-list__link"><%= topic %></a>
    </li>

<% end %>
<ul>

Troubleshooting

Additional information

Blog posts are actually suffixed .html.md.erb, as I intend to use erb-partials in these in the future. I've tried testing just .html.md, but the same issue occurs: 2019-02-02-Test-Title.txt

My config.rb looks like this: config.txt

Many thanks for looking into the issue. I'm sure I've done something dumb, but I am at a complete loss as to what it is.

Cheers, Johan

tdreyno commented 5 years ago

And this wasn't happening with Middleman 4.2.x?

It looks like it's returning the tag and a list of articles with that tag. Which seems reasonable. I don't remember the documented API. The blog extension is normally released by others. Was there a blog update recently?

johanolofolsson commented 5 years ago

Hi Thomas,

Thanks for the reply. This was happening in 4.2.1 as well, I updated to 4.3.2 yesterday to see if it resolved anything (I got SassC working in 4.2.x before your 4.3.x update, so I had put off the update until now).

Ah, I think you're right, I'm trying to do something here that differs too much from the boilerplate code in the documentation:

<% blog.tags.each do |tag, articles| %>. I essentially want to only list tags (and not posts). Re-reading the docs, I see I missed this bit:

you can get a list of all tags with their associated articles from blog.tags.

So naturally the method spits out a map in my badly-written loop.

So user-error on my part ;). I'll mark this as closed and approach this a little differently.

Cheers again, Johan

tdreyno commented 5 years ago
<% blog.tags.each do |tag, _| %>

If you want to omit the articles

johanolofolsson commented 5 years ago

Nice, I didn’t know about that _ syntax. Thanks!

johanolofolsson commented 5 years ago

In case anyone stumbles upon this closed issue, here is what I ended up with (markup needs work, but it's the ERB code that counts):

<ul class="o-list-bare  c-topic-list">

  <% blog.tags.sort.each do |topic, _| %><%# Discard articles with `_` (thanks tdreyno: https://github.com/middleman/middleman-blog/issues/372#issuecomment-462506361) %>

    <li class="o-list-bare__item  c-topic-list__item">
      <a href="<%= tag_path(topic) %>" class="c-link-muted  c-topic-list__link"><%= topic %></a>
    </li>

  <% end %>

</ul>

Even if you don't want to list each tag's article, you could always iterate through articles anyway to increment a counter. You can then display the number of articles per tag:

<ul class="o-list-bare  c-topic-list">

  <% blog.tags.sort.each do |topic, articles| %>

    <li class="o-list-bare__item  c-topic-list__item">
      <% postCount = 0 %>
      <% articles.each do |post| %>
        <% postCount = postCount + 1 %>
      <% end %>
      <a href="<%= tag_path(topic) %>" class="c-link-muted  c-topic-list__link  c-topic"><span class="c-topic__label"><%= topic %></span> <span class="c-topic__count"><%= postCount %></span></a>
    </li>

  <% end %>

</ul>

Cheers, Johan