Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

If a post has invalid categories, it won't render #212

Closed
vikingcode opened this issue Feb 15, 2015 · 3 comments
Closed

If a post has invalid categories, it won't render #212

vikingcode opened this issue Feb 15, 2015 · 3 comments
Labels

Comments

@vikingcode
Copy link
Contributor

Forgive me, it's been awhile since I've used Github so I'm not sure how all the cool kids link to stuff properly anymore.

Basically if a post has no category, it'll throw a NullReferenceException when evaluating the permalink. The "offending" line (for lack of a better term) is this:
permalink = permalink.Replace(":categories", string.Join("-", page.Categories.ToArray()));

https://github.com/Code52/pretzel/blob/master/src/Pretzel.Logic/Templating/Context/SiteContextGenerator.cs#L402

Now, I know I could send in a PR but there would be a couple of things to think about:

  • A check before hand, if (page.Categories != null) solves the issue, easy done, right? Well, does that then necessitate a check for each of the date properties?
  • Investigate why page.Categories isn't initialised, because in theory it is in the constructor for Page.
@laedit
Copy link
Member

laedit commented Feb 15, 2015

Thanks for reporting this, but I cannot reproduce the bug: for a post with no categories, the Categories property is well initiated and empty.

Can you indicate some way to reproduce it?

@vikingcode
Copy link
Contributor Author

Okay, so, I'm an idiot. It's not that my posts have no categories, its that Pretzel didn't pick them up because of incorrect syntax and nulled its on values.

Using the post below as context, page.Categories = header["categories"] as IEnumerable<string>; (ln245 of the same file) comes out as null because in this context I haven't provided multiple categories. Now the key here is my syntax is off - it should be

categories:
-foo
-bah

So the solution is for me to fix my syntax, but the only way I discovered my syntax was off was by digging into the source code of Pretzel. There isn't an error (other than NullValueException) indicating what was wrong.

I think ultimately pretzel should do a "best effort" at delivering a page rather than stopping on an error (obviously depending on the scope of the error!). I think the null value check on L400 would still be a good idea for idiots like me :D What I mean is, sure, categories shouldn't work because my syntax is off, but the rest of the page should at least render by adding that check


Here is an example of the YAML front matter thats failing (the rest of the post isn't super relevant)

---
layout: post
title:  "Building a fancier work bench for the mini lathe"
date:  2015-02-11
categories:  video
---

@laedit
Copy link
Member

laedit commented Feb 15, 2015

Thanks for the info.
Yeah, I think we can add the check, and also support the "one categories" case.

For info, you can also use the syntax:
categories: [video]

@laedit laedit added the Bug label Feb 15, 2015
@laedit laedit changed the title If a post has no categories, it won't render If a post has invalid categories, it won't render Feb 15, 2015
@laedit laedit closed this as completed in fd234fc Feb 16, 2015
laedit pushed a commit that referenced this issue Feb 16, 2015
Fixes #212 relating to categories being null from incorrect syntax in posts
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants