I've been doing something very similar for templating static HTML sites amongst other things - you can interpret any string as a JS template string:
function jstsEngine(string = '', environment = {}) {
return new Function(
...Object.keys(environment),
'output={}',
'return [`' + string + '`, output]'
)(...Object.values(environment))
}
Link with examples: https://github.com/tomhodgins/jsts-engine