mrdocs::js::registerHelper

Register a JavaScript helper function

Synopsis

Declared in <mrdocs/Support/JavaScript.hpp>

[[nodiscard]]
Expected<void, Error>
registerHelper(
    mrdocs::Handlebars& hbs,
    std::string_view name,
    Context& ctx,
    std::string_view script);

Description

This function registers a JavaScript function as a helper function that can be called from Handlebars templates.

The helper source is resolved in the following order:

1. Parenthesized eval ‐ wraps the script in parentheses and evaluates. Handles function declarations without side effects. Example: "function add(a, b) { return a + b; }"

2. Direct eval ‐ evaluates the script as‐is. Handles IIFEs and expressions that return functions. Example: "(function(){ return function(x){ return x*2; }; })()"

3. Global lookup ‐ looks up the helper name on the global object. Handles scripts that define globals before returning. Example: "var helper = function(x){ return x; }; helper;"

The resolved function is stored on the shared MrDocsHelpers global object and registered with Handlebars. When invoked, positional arguments are passed to the JavaScript function (the Handlebars options object is stripped to avoid expensive recursive conversion of symbol contexts).

Return Value

Success, or an error if the script could not be resolved to a function

Parameters

Name Description

hbs

The Handlebars instance to register the helper into

name

The name of the helper function

ctx

The JavaScript context to use

script

The JavaScript code that defines the helper function

Created with MrDocs