Improve types and sanitation of macro values
This commit is contained in:
parent
ef0772bc9f
commit
719539c2ab
|
@ -13,17 +13,22 @@ Handlebars.registerHelper('helperMissing', function () {
|
||||||
return substituteParams(`{{${macroName}}}`);
|
return substituteParams(`{{${macroName}}}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef {Object<string, *>} EnvObject
|
||||||
|
* @typedef {(nonce: string) => string} MacroFunction
|
||||||
|
*/
|
||||||
|
|
||||||
export class MacrosParser {
|
export class MacrosParser {
|
||||||
/**
|
/**
|
||||||
* A map of registered macros.
|
* A map of registered macros.
|
||||||
* @type {Map<string, string|((nonce: string) => string)>}
|
* @type {Map<string, string|MacroFunction>}
|
||||||
*/
|
*/
|
||||||
static #macros = new Map();
|
static #macros = new Map();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers a global macro that can be used anywhere where substitution is allowed.
|
* Registers a global macro that can be used anywhere where substitution is allowed.
|
||||||
* @param {string} key Macro name (key)
|
* @param {string} key Macro name (key)
|
||||||
* @param {string|((nonce: string) => string)} value A string or a function that returns a string
|
* @param {string|MacroFunction} value A string or a function that returns a string
|
||||||
*/
|
*/
|
||||||
static registerMacro(key, value) {
|
static registerMacro(key, value) {
|
||||||
if (typeof key !== 'string') {
|
if (typeof key !== 'string') {
|
||||||
|
@ -55,7 +60,7 @@ export class MacrosParser {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Populate the env object with macro values from the current context.
|
* Populate the env object with macro values from the current context.
|
||||||
* @param {Object<string, *>} env Env object for the current evaluation context
|
* @param {EnvObject} env Env object for the current evaluation context
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
static populateEnv(env) {
|
static populateEnv(env) {
|
||||||
|
@ -88,6 +93,20 @@ export class MacrosParser {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (value instanceof Promise) {
|
||||||
|
console.warn('Promises are not supported as macro values');
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof value === 'function') {
|
||||||
|
console.warn('Functions are not supported as macro values');
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value instanceof Date) {
|
||||||
|
return value.toISOString();
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof value === 'object') {
|
if (typeof value === 'object') {
|
||||||
return JSON.stringify(value);
|
return JSON.stringify(value);
|
||||||
}
|
}
|
||||||
|
@ -367,7 +386,7 @@ function timeDiffReplace(input) {
|
||||||
/**
|
/**
|
||||||
* Substitutes {{macro}} parameters in a string.
|
* Substitutes {{macro}} parameters in a string.
|
||||||
* @param {string} content - The string to substitute parameters in.
|
* @param {string} content - The string to substitute parameters in.
|
||||||
* @param {Object<string, *>} env - Map of macro names to the values they'll be substituted with. If the param
|
* @param {EnvObject} env - Map of macro names to the values they'll be substituted with. If the param
|
||||||
* values are functions, those functions will be called and their return values are used.
|
* values are functions, those functions will be called and their return values are used.
|
||||||
* @returns {string} The string with substituted parameters.
|
* @returns {string} The string with substituted parameters.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue