Skip to content

Feature Request: New option for Handlebars helpers #54

@tristanlee85

Description

@tristanlee85

The templating functionality is great with this, and partials are a huge step in customizing the output. Taking this a step further, it would be nice to also allow for helper functions to be configured for the templates.

One example of this is to stringify data being written to the log. I am creating both a text and JSON changelog. The JSON is for other services to be able to easily parse. Using a helper, I can be sure that my output is JSON-safe. Here's an example of my current implementation:

changelog: {
    portal: {
        options: {
            after: 'v<%= package.version %>',
            partials: {
                features: '{{#each features}}{{> feature}}{{/each}}',
                feature: '[FEATURE] {{this}}\n',
                fixes: '{{#each fixes}}{{> fix}}{{/each}}',
                fix: '[BUGFIX] {{this}}\n'
            }
        }
    },

    // JSON version for easier parsing
    extension: {
        options: {
            after: 'v<%= package.version %>',
            dest: 'changelog.json',
            template: '{"fixes": [{{> fixes }}], "features": [{{> features }}]}',
            helpers: {
              toJSON: function(object){
                  return new Handlebars.SafeString(JSON.stringify(object));
              }
            },
            partials: {
                features: '{{#each features}}{{> feature}}{{#unless @last}},{{/unless}}{{/each}}',
                feature: '{{toJSON this}}',
                fixes: '{{#each fixes}}{{> fix}}{{#unless @last}},{{/unless}}{{/each}}',
                fix: '{{toJSON this}}'
            }
        }
    }
}

Output:

{
    "fixes": ["Fixed issue with \"stuff\" not working"],
    "features": ["Update notification feature", "Another feature"]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions