CaiJimmy / hugo-theme-stack

Card-style Hugo theme designed for bloggers
https://stack.jimmycai.com
GNU General Public License v3.0
4.89k stars 1.6k forks source link

fix(widgets): ensure `params` is not nil #943

Closed WaterLemons2k closed 7 months ago

WaterLemons2k commented 7 months ago

If the params key is not defined in the config file, you will get an error when getting any value for .Params:

nil pointer evaluating interface {}

This is because when the params key is undefined, .Params is nil (note this is completely different from an empty map), and nil cannot be used as a map.

To fix this, just make sure params is always map and not nil.

CaiJimmy commented 7 months ago

Hi, thanks for the PR!

This issue is partially fixed in the canary branch: https://github.com/CaiJimmy/hugo-theme-stack/blob/canary/layouts/partials/widget/taxonomy.html, where the tag-cloud and categories widgets are merged into one taxonomy widget, which requires params.type. (The error you mention only occurs if params is empty.)

However, it's not fixed for the archives widget. So I think we can merge the changes you made to the archives widget into the canary branch. Does that sound good to you?

WaterLemons2k commented 7 months ago

Thanks for your reply!

It seems that the nil pointer evaluating interface {} error only occurs when params is nil, so ensuring that params is not nil sounds better.

This is good for all widgets that use params, as there is no need to worry about params being nil.