blakeembrey / metalsmith-pagination

A Metalsmith plugin for paginating arrays and collections
MIT License
34 stars 10 forks source link

Plugin throws TypeError if collection doesn't exist #25

Open colophonemes opened 8 years ago

colophonemes commented 8 years ago

Hey

First, thanks for the great plugin!

Second, just wondering why the plugin is so strict about requiring the collection to be present in the build. If I have a null collection (i.e. nothing in my build matches the pattern set on metalsmith-collections) then the plugin throws a TypeError (L37).

I back most of my Metalsmith sites with Contentful, so I'm not in complete control of which items will be in the build. If an editor removes all items from a particular Content Type then it's possible that the collection will be empty.

Could default behaviour just be skipping over missing/empty collections?

Cheers Sam

blakeembrey commented 8 years ago

Interesting. I guess that's possible. Do you use predefined collections? I would have guessed the collection would be [] instead of null if there's no items.

colophonemes commented 8 years ago

My metalsmith-collection and metalsmith-pagination settings are generated automatically for each content type. The difficulty is checking whether the collection actually contains any items at build time (because the Contentful data is pulled in after the build starts, but the pagination settings are defined beforehand). I agree that it's strange that it doesn't just create an empty collection — I'll do a bit of digging and see if there's an issue with my implementation.

colophonemes commented 8 years ago

So, metalsmith-collections doesn't add any keys to the metadata if the pattern glob doesn't match anything in the build. I could create a hacky fix by running a plugin in between metalsmith-collections and metalsmith-pagination that adds an empty array to all the expected metadata key if it doesn't exist, but it seems like it wouldn't be a big deal to just let unmatched collections fall through rather than throwing the error. Would you be open to me submitting a pull request to that effect?

blakeembrey commented 8 years ago

Sure, sounds good to me.