Skip to content

Commit

Permalink
feat: add 'select' handlebars helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sdkrystian committed Jun 4, 2024
1 parent 0d821e2 commit 8c20d25
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
23 changes: 23 additions & 0 deletions include/mrdocs/Support/Handlebars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,17 @@ MRDOCS_DECL
void
registerAntoraHelpers(Handlebars& hbs);

/** Register logical helpers into a Handlebars instance
This function registers a number of common helpers that perform
logical operations.
@param hbs The Handlebars instance to register the helpers into
*/
MRDOCS_DECL
void
registerLogicalHelpers(Handlebars& hbs);

/** Register string helpers into a Handlebars instance
This function registers a number of common helpers that operate on
Expand Down Expand Up @@ -1130,6 +1141,18 @@ MRDOCS_DECL
bool
not_fn(dom::Array const& arg);

/** "select" helper function
*
* The "select" helper returns the second argument if the first argument is
* truthy, and the third argument otherwise.
*/
MRDOCS_DECL
dom::Value
select_fn(
dom::Value condition,
dom::Value result_true,
dom::Value result_false);

/** "increment" helper function
*
* The "increment" helper adds 1 to the value if it's an integer and converts
Expand Down
21 changes: 21 additions & 0 deletions src/lib/Support/Handlebars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3674,6 +3674,17 @@ registerAntoraHelpers(Handlebars& hbs)
hbs.registerHelper("year", dom::makeInvocable(year_fn));
}

void
registerLogicalHelpers(Handlebars& hbs)
{
hbs.registerHelper("and", dom::makeVariadicInvocable(and_fn));
hbs.registerHelper("eq", dom::makeVariadicInvocable(eq_fn));
hbs.registerHelper("ne", dom::makeVariadicInvocable(ne_fn));
hbs.registerHelper("not", dom::makeVariadicInvocable(not_fn));
hbs.registerHelper("or", dom::makeVariadicInvocable(or_fn));
hbs.registerHelper("select", dom::makeInvocable(select_fn));
}

bool
and_fn(dom::Array const& args)
{
Expand Down Expand Up @@ -3749,6 +3760,16 @@ increment_fn(dom::Value const& value)
return 1;
}

dom::Value
select_fn(
dom::Value condition,
dom::Value result_true,
dom::Value result_false)
{
return isEmpty(condition) ?
result_false : result_true;
}

dom::Value
detag_fn(dom::Value html)
{
Expand Down

0 comments on commit 8c20d25

Please sign in to comment.