2024-02-21 19:40:50 +01:00
|
|
|
import Helper from "./Helper"
|
|
|
|
import { SafeString } from "handlebars"
|
|
|
|
import * as externalHandlebars from "./external"
|
|
|
|
import { processJS } from "./javascript"
|
|
|
|
import {
|
2021-01-26 13:43:26 +01:00
|
|
|
HelperFunctionNames,
|
|
|
|
HelperFunctionBuiltin,
|
|
|
|
LITERAL_MARKER,
|
2024-02-21 19:40:50 +01:00
|
|
|
} from "./constants"
|
2024-02-22 00:39:27 +01:00
|
|
|
|
2024-02-21 19:40:50 +01:00
|
|
|
export { getJsHelperList } from "./list"
|
2021-01-18 18:40:19 +01:00
|
|
|
|
2021-01-19 19:44:29 +01:00
|
|
|
const HTML_SWAPS = {
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
}
|
|
|
|
|
2024-02-22 11:52:50 +01:00
|
|
|
function isObject(value: string | any[]) {
|
2022-05-04 18:36:30 +02:00
|
|
|
if (value == null || typeof value !== "object") {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
value.toString() === "[object Object]" ||
|
|
|
|
(value.length > 0 && typeof value[0] === "object")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-01-18 18:40:19 +01:00
|
|
|
const HELPERS = [
|
2021-01-19 19:44:29 +01:00
|
|
|
// external helpers
|
2024-02-22 11:52:50 +01:00
|
|
|
new Helper(HelperFunctionNames.OBJECT, (value: any) => {
|
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
|
2024-02-22 11:52:50 +01:00
|
|
|
new Helper(
|
|
|
|
HelperFunctionNames.ALL,
|
|
|
|
(value: string, inputs: { __opts: any }) => {
|
|
|
|
const { __opts } = inputs
|
|
|
|
if (isObject(value)) {
|
|
|
|
return new SafeString(JSON.stringify(value))
|
|
|
|
}
|
|
|
|
// null/undefined values produce bad results
|
|
|
|
if (__opts && __opts.onlyFound && value == null) {
|
|
|
|
return __opts.input
|
|
|
|
}
|
|
|
|
if (value == null || typeof value !== "string") {
|
|
|
|
return value == null ? "" : value
|
|
|
|
}
|
|
|
|
// TODO: check, this should always be false
|
|
|
|
if (value && (value as any).string) {
|
|
|
|
value = (value as any).string
|
|
|
|
}
|
2024-03-14 23:51:24 +01:00
|
|
|
let text: any = value
|
2024-02-22 11:52:50 +01:00
|
|
|
if (__opts && __opts.escapeNewlines) {
|
|
|
|
text = value.replace(/\n/g, "\\n")
|
|
|
|
}
|
2024-03-14 23:51:24 +01:00
|
|
|
text = new SafeString(text.replace(/&/g, "&"))
|
2024-02-22 11:52:50 +01:00
|
|
|
if (text == null || typeof text !== "string") {
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
return text.replace(/[<>]/g, (tag: string) => {
|
|
|
|
return HTML_SWAPS[tag as keyof typeof HTML_SWAPS] || tag
|
|
|
|
})
|
2021-09-17 18:18:52 +02:00
|
|
|
}
|
2024-02-22 11:52:50 +01:00
|
|
|
),
|
2021-01-25 19:14:45 +01:00
|
|
|
// adds a note for post-processor
|
2024-02-22 11:52:50 +01:00
|
|
|
new Helper(HelperFunctionNames.LITERAL, (value: any) => {
|
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
|
|
|
]
|
|
|
|
|
2024-02-21 19:40:50 +01:00
|
|
|
export function HelperNames() {
|
2021-01-21 16:50:46 +01:00
|
|
|
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
|
|
|
|
2024-02-22 11:52:50 +01:00
|
|
|
export function registerMinimum(handlebars: typeof Handlebars) {
|
2021-01-18 18:40:19 +01:00
|
|
|
for (let helper of HELPERS) {
|
|
|
|
helper.register(handlebars)
|
|
|
|
}
|
2022-02-07 18:56:01 +01:00
|
|
|
}
|
|
|
|
|
2024-02-22 11:52:50 +01:00
|
|
|
export function registerAll(handlebars: typeof Handlebars) {
|
2024-02-21 19:40:50 +01:00
|
|
|
registerMinimum(handlebars)
|
2021-01-21 16:50:46 +01:00
|
|
|
// register imported helpers
|
|
|
|
externalHandlebars.registerAll(handlebars)
|
2021-01-18 18:40:19 +01:00
|
|
|
}
|
|
|
|
|
2024-02-22 11:52:50 +01:00
|
|
|
export function unregisterAll(handlebars: any) {
|
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
|
|
|
}
|