1
0
Fork 0
mirror of synced 2024-10-01 09:38:55 +13:00

Adding some more changes to make it more obvious when a binding hasn't worked.

This commit is contained in:
mike12345567 2021-02-03 13:04:19 +00:00
parent 9aaf6b4883
commit febad5ad9d
2 changed files with 26 additions and 7 deletions

View file

@ -2,7 +2,7 @@ const handlebars = require("handlebars")
const { registerAll } = require("./helpers/index") const { registerAll } = require("./helpers/index")
const processors = require("./processors") const processors = require("./processors")
const { cloneDeep } = require("lodash/fp") const { cloneDeep } = require("lodash/fp")
const { removeNull, addConstants } = require("./utilities") const { removeNull, addConstants, removeHandlebarsStatements } = require("./utilities")
const manifest = require("../manifest.json") const manifest = require("../manifest.json")
const hbsInstance = handlebars.create() const hbsInstance = handlebars.create()
@ -86,18 +86,24 @@ module.exports.processStringSync = (string, context) => {
if (!exports.isValid(string)) { if (!exports.isValid(string)) {
return string return string
} }
// take a copy of input incase error
const input = string
let clonedContext = removeNull(cloneDeep(context)) let clonedContext = removeNull(cloneDeep(context))
clonedContext = addConstants(clonedContext) clonedContext = addConstants(clonedContext)
// remove any null/undefined properties // remove any null/undefined properties
if (typeof string !== "string") { if (typeof string !== "string") {
throw "Cannot process non-string types." throw "Cannot process non-string types."
} }
string = processors.preprocess(string) try {
// this does not throw an error when template can't be fulfilled, have to try correct beforehand string = processors.preprocess(string)
const template = hbsInstance.compile(string, { // this does not throw an error when template can't be fulfilled, have to try correct beforehand
strict: false, const template = hbsInstance.compile(string, {
}) strict: false,
return processors.postprocess(template(clonedContext)) })
return processors.postprocess(template(clonedContext))
} catch (err) {
return removeHandlebarsStatements(input)
}
} }
/** /**

View file

@ -32,3 +32,16 @@ module.exports.addConstants = obj => {
} }
return obj return obj
} }
module.exports.removeHandlebarsStatements = string => {
let regexp = new RegExp(exports.FIND_HBS_REGEX)
let matches = string.match(regexp)
if (matches == null) {
return string
}
for (let match of matches) {
const idx = string.indexOf(match)
string = exports.swapStrings(string, idx, match.length, "Invalid Binding")
}
return string
}