seethroughdev / obsidian-recipe-grabber

MIT License
55 stars 8 forks source link

Feature request: "any" conditional helper #39

Open de-vo-id opened 2 months ago

de-vo-id commented 2 months ago

Description

Hello! I found your plugin earlier, and have been enjoying it a lot. However, I've ran into a bit of a roadblock with my recipe template. I wanted to specify a sort of "or" or "any" conditional statement in order to only create a nutrition category if those values are available from the recipe website. Since a helper like that isn't built into Handlebars and a custom one isn't provided (and using a bunch of nested if statements isn't my jam) I very quickly came up with my own and shoved it into the main.js plugin file. It's been working very well for me, but it would be awesome to see it included in the plugin by default.

Plugin code

I'm quite a noob with JS and TS so these might be a bad way of doing it or prone to breaking everything. Likely can be refined.

The helper function (for the compiled main.js):

handlebars.registerHelper("any", function(...args) {
return args.some(arg => Boolean(arg));
});

The helper function (for main.ts):

handlebars.registerHelper("any", function (...args: any[]): boolean {
    return args.some(arg => Boolean(arg));
});

Handlebars template usage

How I've used it:

{{#if (any nutrition.calories nutrition.carbohydrateContent nutrition.proteinContent nutrition.fatContent nutrition.fiberContent nutrition.sugarContent nutrition.sodiumContent)}}
{{!-- Create category for nutrition details and specify further logic for the individual nutrition values--}}
{{/if}}

There's a chance that all of this is completely useless and I'm an idiot for missing something that's already built-in. If not, then I'd love for you to consider adding it!

seethroughdev commented 1 month ago

Hi @de-vo-id , I'm so sorry i missed this earlier! Thank you for taking the time to right it up.

I can't think of reason not to add this. it seems very straight forward.

@Lanjelin , you have more Handlebars experience, am I missing something here, or I go ahead and put it in the next release?