feat: add stringToPrettyJSON handlebars function (#12643)

This commit is contained in:
markussiebert 2021-11-14 20:49:05 +01:00 committed by GitHub
parent ab5abd7553
commit c80c7f3d0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 0 deletions

View file

@ -20,3 +20,13 @@ Some are configuration options passed through, while others are generated as par
## Other available fields ## Other available fields
<!-- Insert runtime fields here --> <!-- Insert runtime fields here -->
## Additional Handlebars helpers
### stringToPrettyJSON
If you want to print pretty JSON with Handlebars you can use the built-in function `stringToPrettyJSON` like this:
`{{{stringToPrettyJSON myvar}}}`
In the example above `myvar` is a variable/field, that contains valid JSON.

View file

@ -1,3 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`util/template/index filters out disallowed fields 1`] = `"github token = \\"\\""`; exports[`util/template/index filters out disallowed fields 1`] = `"github token = \\"\\""`;
exports[`util/template/index string to pretty JSON 1`] = `
"{
\\"some\\": {
\\"fancy\\": \\"json\\"
}
}"
`;

View file

@ -18,4 +18,10 @@ describe('util/template/index', () => {
expect(output).toContain('github'); expect(output).toContain('github');
expect(output).not.toContain('123test'); expect(output).not.toContain('123test');
}); });
it('string to pretty JSON ', () => {
const userTemplate =
'{{{ stringToPrettyJSON \'{"some":{"fancy":"json"}}\'}}}';
const output = template.compile(userTemplate, undefined);
expect(output).toMatchSnapshot();
});
}); });

View file

@ -6,6 +6,10 @@ import { clone } from '../clone';
handlebars.registerHelper('encodeURIComponent', encodeURIComponent); handlebars.registerHelper('encodeURIComponent', encodeURIComponent);
handlebars.registerHelper('stringToPrettyJSON', (input: string): string =>
JSON.stringify(JSON.parse(input), null, 2)
);
// istanbul ignore next // istanbul ignore next
handlebars.registerHelper( handlebars.registerHelper(
'replace', 'replace',