From 834b59514fe8dcc288dfe069bed8b61d7bfaa0e9 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Wed, 15 Apr 2026 10:15:43 +0200 Subject: [PATCH] util: add handlebars render helper Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __tests__/util.test.ts | 14 ++++++++++++++ src/util.ts | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index b1e8477..c38646e 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -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: 'actions-toolkit' + } + ); + expect(rendered).toBe('docker actions-toolkit'); + }); +}); + describe('stringToUnicodeEntities', () => { it('should convert a string to Unicode entities', () => { const input = 'Hello, World!'; diff --git a/src/util.ts b/src/util.ts index 25bb098..11ece94 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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[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)};`)