I think it's doable, but it will depend on how confident you are about catching the variable inside your HTML.
If it’s a known list of variables in advance, it’s really easy you can catch the pattern(s) with a regex, then evaluate the result.
If it’s not known in advance, you’ll need a more generic regex that can detect them dynamically, but that can lead to many edge cases depending on what’s inside the brackets (especially if the pattern itself includes brackets).
Example:
Code: Select all
async function evaluateVariable(job: Job, string: string): Promise<string> {
const result = await job.getVariableAsString(string);
return result;
}
async function replaceVariablesInHtml(job: Job, html: string): Promise<string> {
const regex = /\[(?:Job|Metadata|Doc)\.[^\]]+\]/g; //enhance the regex as needed or catch specific pattern
return html.replace(regex, (match) => {
const evaluatedValue = await evaluateVariable(job, match);
return evaluatedValue;
});
}
async function jobArrived(s: Switch, flowElement: FlowElement, job: Job) {
const html = `<!DOCTYPE html>
<html>
<body>
<h1>'[Metadata.Rational:Dataset="JSON",Model="JSON",Path="path/to/my/value"]</h1>
<p>My first paragraph.</p>
<p><strong>Name: </strong>[Job.NameProper]</p>
</body>
</html>
`;
const output = await replaceVariablesInHtml(job, html);
}
Bonus:
Here’s a
TypeScript Playground example.
It’s basically the same logic, but without the Switch context, instead, it replaces matches with a random number (since we can’t evaluate variables there).