2021-01-18 18:40:19 +01:00
|
|
|
const Helper = require("./Helper")
|
|
|
|
const { SafeString } = require("handlebars")
|
2021-01-21 16:50:46 +01:00
|
|
|
const externalHandlebars = require("./external")
|
|
|
|
const { HelperFunctionNames, HelperFunctionBuiltin } = require("./constants")
|
2021-01-18 18:40:19 +01:00
|
|
|
|
2021-01-19 19:44:29 +01:00
|
|
|
const HTML_SWAPS = {
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
}
|
|
|
|
|
2021-01-18 18:40:19 +01:00
|
|
|
const HELPERS = [
|
2021-01-19 19:44:29 +01:00
|
|
|
// external helpers
|
2021-01-20 19:12:16 +01:00
|
|
|
new Helper(HelperFunctionNames.OBJECT, value => {
|
2021-01-18 18:40:19 +01:00
|
|
|
return new SafeString(JSON.stringify(value))
|
|
|
|
}),
|
2021-01-19 19:44:29 +01:00
|
|
|
// this help is applied to all statements
|
2021-01-20 19:12:16 +01:00
|
|
|
new Helper(HelperFunctionNames.ALL, value => {
|
2021-01-21 14:48:23 +01:00
|
|
|
// null/undefined values produce bad results
|
|
|
|
if (value == null) {
|
|
|
|
return ""
|
|
|
|
}
|
2021-01-21 12:32:26 +01:00
|
|
|
let text = new SafeString(unescape(value).replace(/&/g, "&"))
|
2021-01-19 19:44:29 +01:00
|
|
|
if (text == null || typeof text !== "string") {
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
return text.replace(/[<>]/g, tag => {
|
|
|
|
return HTML_SWAPS[tag] || tag
|
|
|
|
})
|
2021-01-21 12:32:26 +01:00
|
|
|
}),
|
2021-01-18 18:40:19 +01:00
|
|
|
]
|
|
|
|
|
2021-01-21 16:50:46 +01:00
|
|
|
module.exports.HelperNames = () => {
|
|
|
|
return Object.values(HelperFunctionNames).concat(
|
|
|
|
HelperFunctionBuiltin,
|
2021-01-21 18:30:51 +01:00
|
|
|
externalHandlebars.externalHelperNames
|
2021-01-21 16:50:46 +01:00
|
|
|
)
|
|
|
|
}
|
2021-01-20 19:12:16 +01:00
|
|
|
|
2021-01-18 18:40:19 +01:00
|
|
|
module.exports.registerAll = handlebars => {
|
|
|
|
for (let helper of HELPERS) {
|
|
|
|
helper.register(handlebars)
|
|
|
|
}
|
2021-01-21 16:50:46 +01:00
|
|
|
// register imported helpers
|
|
|
|
externalHandlebars.registerAll(handlebars)
|
2021-01-18 18:40:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.unregisterAll = handlebars => {
|
|
|
|
for (let helper of HELPERS) {
|
|
|
|
helper.unregister(handlebars)
|
|
|
|
}
|
2021-01-21 16:50:46 +01:00
|
|
|
// unregister all imported helpers
|
|
|
|
externalHandlebars.unregisterAll(handlebars)
|
2021-01-18 18:40:19 +01:00
|
|
|
}
|