Skip to content

Commit cc6ad44

Browse files
committed
adding changes to handle escapeExpression and remove fixed noEscape
1 parent fafb106 commit cc6ad44

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const { registerCoreHelpers } = require('./helpers')
22

3+
const isPromise = (obj) => !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
4+
35
function asyncHelpers(hbs) {
46

57
const handlebars = hbs.create(),
@@ -40,18 +42,27 @@ function asyncHelpers(hbs) {
4042
handlebars.JavaScriptCompiler = asyncCompiler
4143

4244
const _compile = handlebars.compile,
43-
_template = handlebars.VM.template
45+
_template = handlebars.VM.template,
46+
_escapeExpression = handlebars.escapeExpression,
47+
escapeExpression = function(value) {
48+
if(isPromise(value)) {
49+
return value.then((v) => _escapeExpression(v))
50+
}
51+
return _escapeExpression(value)
52+
}
53+
4454
handlebars.template = function(spec) {
4555
spec.main_d = (prog, props, container, depth, data, blockParams, depths) => async(context) => {
4656
// const main = await spec.main
57+
container.escapeExpression = escapeExpression
4758
const v = spec.main(container, context, container.helpers, container.partials, data, blockParams, depths)
4859
return v
4960
}
5061
return _template(spec, handlebars)
5162
}
5263

5364
handlebars.compile = function(template, options) {
54-
const compiled = _compile.apply(handlebars, [template, { ...options, noEscape: true }])
65+
const compiled = _compile.apply(handlebars, [template, { ...options }])
5566

5667
return function(context, execOptions) {
5768
context = context || {}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "handlebars-async-helpers",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "Adding async function support to handlebars helpers",
55
"main": "index.js",
66
"scripts": {

test/test.js

+12
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,16 @@ describe('Test async helpers', () => {
305305
result = await compiled()
306306
should.equal(result, expected)
307307
})
308+
309+
it('Test custom helper without noEscape', async () => {
310+
const hbs = asyncHelpers(Handlebars),
311+
template = '<div>Value: {{toUpperAsync "my text"}}</div>',
312+
expected = '<div>Value: MY TEXT</div>'
313+
hbs.registerHelper('toUpperAsync', async (value) => {
314+
return Promise.resolve(value.toUpperCase())
315+
})
316+
const compiled = hbs.compile(template),
317+
result = await compiled()
318+
should.equal(result, expected)
319+
})
308320
})

0 commit comments

Comments
 (0)