util: add handlebars render helper

Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2026-04-15 10:15:43 +02:00
parent e7e22d0351
commit 834b59514f
2 changed files with 19 additions and 0 deletions

View File

@@ -392,6 +392,20 @@ describe('generateRandomString', () => {
});
});
describe('compileHandlebars', () => {
it('renders the template with the meta context and compile options', () => {
const rendered = Util.compileHandlebars(
'{{name}} {{{raw}}}',
{noEscape: true},
{
name: 'docker',
raw: '<strong>actions-toolkit</strong>'
}
);
expect(rendered).toBe('docker <strong>actions-toolkit</strong>');
});
});
describe('stringToUnicodeEntities', () => {
it('should convert a string to Unicode entities', () => {
const input = 'Hello, World!';

View File

@@ -17,6 +17,7 @@
import crypto from 'crypto';
import fs from 'fs';
import path from 'path';
import * as handlebars from 'handlebars';
import * as core from '@actions/core';
import * as io from '@actions/io';
import {parse} from 'csv-parse/sync';
@@ -202,6 +203,10 @@ export class Util {
return bytes.toString('hex').slice(0, length);
}
public static compileHandlebars(value: string, options: Parameters<typeof handlebars.compile>[1], data: unknown): string {
return handlebars.compile(value, options)(data);
}
public static stringToUnicodeEntities(str: string) {
return Array.from(str)
.map(char => `&#x${char.charCodeAt(0).toString(16)};`)