Add Template operation for basic JSON rendering

This commit is contained in:
Kendall Goto 2025-04-09 15:16:11 -07:00
parent 7c8be12d52
commit 361a3b2929
No known key found for this signature in database
GPG key ID: C78AF3694E455D69
6 changed files with 138 additions and 3 deletions

View file

@ -0,0 +1,53 @@
/**
* @author kendallgoto [k@kgo.to]
* @copyright Crown Copyright 2025
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import Handlebars from "handlebars";
/**
* Template operation
*/
class Template extends Operation {
/**
* Template constructor
*/
constructor() {
super();
this.name = "Template";
this.module = "Handlebars";
this.description = "Render a template with Handlebars/Mustache substituting variables using JSON input. Templates will be rendered to plain-text only, to prevent XSS.";
this.infoURL = "https://handlebarsjs.com/";
this.inputType = "JSON";
this.outputType = "string";
this.args = [
{
name: "Template definition (.handlebars)",
type: "text",
value: ""
}
];
}
/**
* @param {JSON} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [templateStr] = args;
try {
const template = Handlebars.compile(templateStr);
return template(input);
} catch (e) {
throw new OperationError(e);
}
}
}
export default Template;