budibase/packages/string-templates/src/helpers/index.ts

100 lines
2.6 KiB
TypeScript
Raw Normal View History

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"
export { getJsHelperList } from "./list"
const HTML_SWAPS = {
"<": "&lt;",
">": "&gt;",
}
2022-05-04 18:36:30 +02:00
function isObject(value) {
if (value == null || typeof value !== "object") {
return false
}
return (
value.toString() === "[object Object]" ||
(value.length > 0 && typeof value[0] === "object")
)
}
const HELPERS = [
// external helpers
2021-05-04 12:32:22 +02:00
new Helper(HelperFunctionNames.OBJECT, value => {
return new SafeString(JSON.stringify(value))
}),
2021-10-11 15:53:55 +02:00
// javascript helper
new Helper(HelperFunctionNames.JS, processJS, false),
// this help is applied to all statements
new Helper(HelperFunctionNames.ALL, (value, inputs) => {
const { __opts } = inputs
2022-05-04 18:36:30 +02:00
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
}
2024-02-21 21:10:53 +01:00
// TODO: check, this should always be false
if (value && (value as any).string) {
value = (value as any).string
}
let text = value
if (__opts && __opts.escapeNewlines) {
text = value.replace(/\n/g, "\\n")
}
text = new SafeString(text.replace(/&amp;/g, "&"))
if (text == null || typeof text !== "string") {
return text
}
2021-05-04 12:32:22 +02:00
return text.replace(/[<>]/g, tag => {
return HTML_SWAPS[tag] || tag
})
2021-01-21 12:32:26 +01:00
}),
// adds a note for post-processor
2021-05-04 12:32:22 +02:00
new Helper(HelperFunctionNames.LITERAL, value => {
if (value === undefined) {
return ""
}
2021-01-26 13:43:26 +01:00
const type = typeof value
const outputVal = type === "object" ? JSON.stringify(value) : value
return `{{${LITERAL_MARKER} ${type}-${outputVal}}}`
2021-01-26 13:43:26 +01:00
}),
]
2024-02-21 19:40:50 +01:00
export function HelperNames() {
return Object.values(HelperFunctionNames).concat(
HelperFunctionBuiltin,
2021-01-21 18:30:51 +01:00
externalHandlebars.externalHelperNames
)
}
2024-02-21 19:40:50 +01:00
export function registerMinimum(handlebars) {
for (let helper of HELPERS) {
helper.register(handlebars)
}
}
2024-02-21 19:40:50 +01:00
export function registerAll(handlebars) {
registerMinimum(handlebars)
// register imported helpers
externalHandlebars.registerAll(handlebars)
}
2024-02-21 19:40:50 +01:00
export function unregisterAll(handlebars) {
for (let helper of HELPERS) {
helper.unregister(handlebars)
}
// unregister all imported helpers
externalHandlebars.unregisterAll(handlebars)
}