Merge pull request #22488 from RoadieHQ/add-nunjucks-options

Add nunjucks configuration possibility
This commit is contained in:
Ben Lambert
2024-01-26 17:31:52 +01:00
committed by GitHub
4 changed files with 21 additions and 1 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/plugin-scaffolder-backend': patch
'@backstage/plugin-scaffolder-node': patch
---
Add option to configure nunjucks with the `trimBlocks` and `lstripBlocks` options in the fetch:template action
+2
View File
@@ -177,6 +177,8 @@ export function createFetchTemplateAction(options: {
copyWithoutTemplating?: string[] | undefined;
cookiecutterCompat?: boolean | undefined;
replace?: boolean | undefined;
trimBlocks?: boolean | undefined;
lstripBlocks?: boolean | undefined;
},
JsonObject
>;
@@ -34,6 +34,7 @@ const { render, renderCompat } = (() => {
const env = module.exports.configure({
autoescape: false,
...JSON.parse(nunjucksConfigs),
tags: {
variableStart: '\${{',
variableEnd: '}}',
@@ -42,6 +43,7 @@ const { render, renderCompat } = (() => {
const compatEnv = module.exports.configure({
autoescape: false,
...JSON.parse(nunjucksConfigs),
tags: {
variableStart: '{{',
variableEnd: '}}',
@@ -102,13 +104,14 @@ export type TemplateFilter = _TemplateFilter;
*/
export type TemplateGlobal = _TemplateGlobal;
export interface SecureTemplaterOptions {
interface SecureTemplaterOptions {
/* Enables jinja compatibility and the "jsonify" filter */
cookiecutterCompat?: boolean;
/* Extra user-provided nunjucks filters */
templateFilters?: Record<string, TemplateFilter>;
/* Extra user-provided nunjucks globals */
templateGlobals?: Record<string, TemplateGlobal>;
nunjucksConfigs?: { trimBlocks?: boolean; lstripBlocks?: boolean };
}
export type SecureTemplateRenderer = (
@@ -122,6 +125,7 @@ export class SecureTemplater {
cookiecutterCompat,
templateFilters = {},
templateGlobals = {},
nunjucksConfigs = {},
} = options;
const isolate = new Isolate({ memoryLimit: 128 });
@@ -140,6 +144,8 @@ export class SecureTemplater {
mkScript(nunjucksSource),
);
await contextGlobal.set('nunjucksConfigs', JSON.stringify(nunjucksConfigs));
const availableFilters = Object.keys(templateFilters);
await contextGlobal.set(
@@ -69,6 +69,8 @@ export function createFetchTemplateAction(options: {
copyWithoutTemplating?: string[];
cookiecutterCompat?: boolean;
replace?: boolean;
trimBlocks?: boolean;
lstripBlocks?: boolean;
}>({
id: 'fetch:template',
description:
@@ -237,6 +239,10 @@ export function createFetchTemplateAction(options: {
...additionalTemplateFilters,
},
templateGlobals: additionalTemplateGlobals,
nunjucksConfigs: {
trimBlocks: ctx.input.trimBlocks,
lstripBlocks: ctx.input.lstripBlocks,
},
});
for (const location of allEntriesInTemplate) {