Skip to content

Commit

Permalink
adding changes to handle escapeExpression and remove fixed noEscape
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonrobledo committed Jul 4, 2022
1 parent fafb106 commit cc6ad44
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { registerCoreHelpers } = require('./helpers')

const isPromise = (obj) => !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'

function asyncHelpers(hbs) {

const handlebars = hbs.create(),
Expand Down Expand Up @@ -40,18 +42,27 @@ function asyncHelpers(hbs) {
handlebars.JavaScriptCompiler = asyncCompiler

const _compile = handlebars.compile,
_template = handlebars.VM.template
_template = handlebars.VM.template,
_escapeExpression = handlebars.escapeExpression,
escapeExpression = function(value) {
if(isPromise(value)) {
return value.then((v) => _escapeExpression(v))
}
return _escapeExpression(value)
}

handlebars.template = function(spec) {
spec.main_d = (prog, props, container, depth, data, blockParams, depths) => async(context) => {
// const main = await spec.main
container.escapeExpression = escapeExpression
const v = spec.main(container, context, container.helpers, container.partials, data, blockParams, depths)
return v
}
return _template(spec, handlebars)
}

handlebars.compile = function(template, options) {
const compiled = _compile.apply(handlebars, [template, { ...options, noEscape: true }])
const compiled = _compile.apply(handlebars, [template, { ...options }])

return function(context, execOptions) {
context = context || {}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "handlebars-async-helpers",
"version": "1.0.3",
"version": "1.0.4",
"description": "Adding async function support to handlebars helpers",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,16 @@ describe('Test async helpers', () => {
result = await compiled()
should.equal(result, expected)
})

it('Test custom helper without noEscape', async () => {
const hbs = asyncHelpers(Handlebars),
template = '<div>Value: {{toUpperAsync "my text"}}</div>',
expected = '<div>Value: MY TEXT</div>'
hbs.registerHelper('toUpperAsync', async (value) => {
return Promise.resolve(value.toUpperCase())
})
const compiled = hbs.compile(template),
result = await compiled()
should.equal(result, expected)
})
})

0 comments on commit cc6ad44

Please sign in to comment.