2021-01-18 18:40:19 +01:00
|
|
|
const Helper = require("./Helper")
|
|
|
|
const { SafeString } = require("handlebars")
|
|
|
|
|
2021-01-19 19:44:29 +01:00
|
|
|
const HTML_SWAPS = {
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:12:16 +01:00
|
|
|
const HelperFunctionBuiltin = [
|
|
|
|
"#if",
|
|
|
|
"#unless",
|
|
|
|
"#each",
|
|
|
|
"#with",
|
|
|
|
"lookup",
|
|
|
|
"log"
|
|
|
|
]
|
|
|
|
|
|
|
|
const HelperFunctionNames = {
|
|
|
|
OBJECT: "object",
|
|
|
|
ALL: "all",
|
|
|
|
}
|
|
|
|
|
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 => {
|
|
|
|
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-18 18:40:19 +01:00
|
|
|
]
|
|
|
|
|
2021-01-20 19:12:16 +01:00
|
|
|
module.exports.HelperFunctions = Object.values(HelperFunctionNames).concat(HelperFunctionBuiltin)
|
|
|
|
|
2021-01-18 18:40:19 +01:00
|
|
|
module.exports.registerAll = handlebars => {
|
|
|
|
for (let helper of HELPERS) {
|
|
|
|
helper.register(handlebars)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.unregisterAll = handlebars => {
|
|
|
|
for (let helper of HELPERS) {
|
|
|
|
helper.unregister(handlebars)
|
|
|
|
}
|
|
|
|
}
|