Skip to content
Snippets Groups Projects
Unverified Commit c80c7f3d authored by markussiebert's avatar markussiebert Committed by GitHub
Browse files

feat: add stringToPrettyJSON handlebars function (#12643)

parent ab5abd75
Branches
Tags 29.8.0
No related merge requests found
...@@ -20,3 +20,13 @@ Some are configuration options passed through, while others are generated as par ...@@ -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.
// 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\\"
}
}"
`;
...@@ -18,4 +18,10 @@ describe('util/template/index', () => { ...@@ -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();
});
}); });
...@@ -6,6 +6,10 @@ import { clone } from '../clone'; ...@@ -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',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment