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")
|
2021-10-11 15:53:55 +02:00
|
|
|
const { processJS } = require("./javascript")
|
2021-01-26 13:43:26 +01:00
|
|
|
const {
|
|
|
|
HelperFunctionNames,
|
|
|
|
HelperFunctionBuiltin,
|
|
|
|
LITERAL_MARKER,
|
|
|
|
} = 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-05-04 12:32:22 +02:00
|
|
|
new Helper(HelperFunctionNames.OBJECT, value => {
|
2021-01-18 18:40:19 +01:00
|
|
|
return new SafeString(JSON.stringify(value))
|
|
|
|
}),
|
2021-10-11 15:53:55 +02:00
|
|
|
// javascript helper
|
2021-10-12 16:40:01 +02:00
|
|
|
new Helper(HelperFunctionNames.JS, processJS, false),
|
2021-01-19 19:44:29 +01:00
|
|
|
// this help is applied to all statements
|
2021-05-04 12:32:22 +02:00
|
|
|
new Helper(HelperFunctionNames.ALL, value => {
|
2021-09-17 18:18:52 +02:00
|
|
|
if (
|
|
|
|
value != null &&
|
|
|
|
typeof value === "object" &&
|
|
|
|
value.toString() === "[object Object]"
|
|
|
|
) {
|
|
|
|
return new SafeString(JSON.stringify(value))
|
|
|
|
}
|
2021-01-21 14:48:23 +01:00
|
|
|
// null/undefined values produce bad results
|
2021-06-24 12:37:26 +02:00
|
|
|
if (value == null || typeof value !== "string") {
|
2021-11-02 17:30:43 +01:00
|
|
|
return value == null ? "" : value
|
2021-01-21 14:48:23 +01:00
|
|
|
}
|
2021-06-24 12:37:26 +02:00
|
|
|
if (value && value.string) {
|
|
|
|
value = value.string
|
|
|
|
}
|
|
|
|
let text = new SafeString(value.replace(/&/g, "&"))
|
2021-01-19 19:44:29 +01:00
|
|
|
if (text == null || typeof text !== "string") {
|
|
|
|
return text
|
|
|
|
}
|
2021-05-04 12:32:22 +02:00
|
|
|
return text.replace(/[<>]/g, tag => {
|
2021-01-19 19:44:29 +01:00
|
|
|
return HTML_SWAPS[tag] || tag
|
|
|
|
})
|
2021-01-21 12:32:26 +01:00
|
|
|
}),
|
2021-01-25 19:14:45 +01:00
|
|
|
// adds a note for post-processor
|
2021-05-04 12:32:22 +02:00
|
|
|
new Helper(HelperFunctionNames.LITERAL, value => {
|
2021-11-15 13:25:30 +01:00
|
|
|
if (value === undefined) {
|
|
|
|
return ""
|
|
|
|
}
|
2021-01-26 13:43:26 +01:00
|
|
|
const type = typeof value
|
2021-01-25 19:14:45 +01:00
|
|
|
const outputVal = type === "object" ? JSON.stringify(value) : value
|
2021-03-16 19:16:56 +01:00
|
|
|
return `{{${LITERAL_MARKER} ${type}-${outputVal}}}`
|
2021-01-26 13:43: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-05-04 12:32:22 +02:00
|
|
|
module.exports.registerAll = handlebars => {
|
2021-01-18 18:40:19 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
module.exports.unregisterAll = handlebars => {
|
2021-01-18 18:40:19 +01:00
|
|
|
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
|
|
|
}
|