bridgetownrb / bridgetown

A next-generation progressive site generator & fullstack framework, powered by Ruby
https://www.bridgetownrb.com
MIT License
1.16k stars 112 forks source link

feat: Remove collection requirement from prototype pages #754

Closed goulvench closed 1 year ago

goulvench commented 1 year ago

The more I read the docs, the less I'm sure if this is a bug or feature. Feel free to turn into an issue if that's the case. Using Bridgetown v1.2.0 (codename "Bonny Slope")

Summary

Prototype pages are great, but should not be constrained by collections. The docs explain that prototype can be restricted to collections, which would imply that it's not mandatory, but the code enforces it.

Motivation

There are 3 collections on my blog, all using tags, and sometimes the tags overlap. I want to list all tags on a single page, and list all associated resources on each (prototype'd) tag page.

Guide-level explanation

It would simply be a matter of removing collection: posts in the "Simple usage" paragraph. Easy peasy!

Reference-level explanation

Since the collection key is currently mandatory, all existing code is backwards-compatible.

I was able to create a page that lists all tags, using the following:

---
title: All tags
permalink: /tags/
---
<% site.taxonomy_types.tag.terms.sort_by(&:first).each do |tag, tagged| %>
  <h2><%= link_to tag, "/tags/#{tag}" %> (<%= tagged.size %>)</h2>
  <ul>
    <% tagged.collect(&:resource).each do |resource| %>
      <li>
        <%= link_to resource.data.title, resource.absolute_url %>
      </li>
    <% end %>
  </ul>
<% end %>

And a prototype page for each collection. Here's the one for posts, all other have exactly the same contents except the collection: name in frontmatter.

---
title: ":prototype-term"
permalink: /tags/:term
prototype:
  term: tag
  collection: posts
---
<ul>
  <% site.taxonomy_types.tag.terms.find { |tag, _| tag.downcase == page.data.tag }.last.collect(&:resource).each do |resource| %>
    <li>
      <%= link_to resource.data.title, resource.absolute_url %>
    </li>
  <% end %>
</ul>
goulvench commented 1 year ago

Extra questions:

Not trying to be picky here, just sharing things that I couldn't find reading the docs or code.

jaredcwhite commented 1 year ago

@goulvench This would appear to be a duplicate of https://github.com/bridgetownrb/bridgetown/issues/730. Can you copy any additional context here you'd like to share to comments on that issue? Thanks!