Skip to content

Commit b3a03f4

Browse files
authored
Merge pull request #1072 from crazy-max/handlebars-compile
util: add handlebars render helper
2 parents e7e22d0 + 834b595 commit b3a03f4

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

__tests__/util.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,20 @@ describe('generateRandomString', () => {
392392
});
393393
});
394394

395+
describe('compileHandlebars', () => {
396+
it('renders the template with the meta context and compile options', () => {
397+
const rendered = Util.compileHandlebars(
398+
'{{name}} {{{raw}}}',
399+
{noEscape: true},
400+
{
401+
name: 'docker',
402+
raw: '<strong>actions-toolkit</strong>'
403+
}
404+
);
405+
expect(rendered).toBe('docker <strong>actions-toolkit</strong>');
406+
});
407+
});
408+
395409
describe('stringToUnicodeEntities', () => {
396410
it('should convert a string to Unicode entities', () => {
397411
const input = 'Hello, World!';

src/util.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import crypto from 'crypto';
1818
import fs from 'fs';
1919
import path from 'path';
20+
import * as handlebars from 'handlebars';
2021
import * as core from '@actions/core';
2122
import * as io from '@actions/io';
2223
import {parse} from 'csv-parse/sync';
@@ -202,6 +203,10 @@ export class Util {
202203
return bytes.toString('hex').slice(0, length);
203204
}
204205

206+
public static compileHandlebars(value: string, options: Parameters<typeof handlebars.compile>[1], data: unknown): string {
207+
return handlebars.compile(value, options)(data);
208+
}
209+
205210
public static stringToUnicodeEntities(str: string) {
206211
return Array.from(str)
207212
.map(char => `&#x${char.charCodeAt(0).toString(16)};`)

0 commit comments

Comments
 (0)