From 8de182a42ae62132e05805c89ed6c9f176c6eff1 Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Mon, 22 Apr 2024 17:20:47 +0200 Subject: [PATCH] WIP. Add custom filters doc section Signed-off-by: cmoulliard --- .../software-templates/writing-templates.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/features/software-templates/writing-templates.md b/docs/features/software-templates/writing-templates.md index ad542ba4a3..0f5c10b96d 100644 --- a/docs/features/software-templates/writing-templates.md +++ b/docs/features/software-templates/writing-templates.md @@ -654,6 +654,8 @@ how to use them in the Scaffolder templates. It's important to mention that Back native filters from the Nunjucks library. For a complete list of these native filters and their usage, refer to the [Nunjucks documentation](https://mozilla.github.io/nunjucks/templating.html#builtin-filters). +To create your own custom filters, look to the section [Custom Filters](#custom-filters) hereafter. + ### parseRepoUrl The `parseRepoUrl` filter parse a repository URL into @@ -738,3 +740,26 @@ The `projectSlug` filter generates a project slug from a repository URL - **Input**: `github.com?repo=backstage&org=backstage` - **Output**: `backstage/backstage` + +## Custom Filters + +Whenever it is needed to extend the built-in filters with yours `${{ parameters.name | my-filter1 | my-filter2 | etc }}`, then you have the option to add them using the property `addTemplateFilters` of the type `RouterOptions` that you typically define + +```ts title="plugins/scaffolder-backend/src/service/router.ts" +export interface RouterOptions { + ... + additionalTemplateFilters?: Record; +} +``` + +The `addTemplateFilters` function accepts the filters as a JSON array of records where each record starts +with the name of the filter, get as input a list of `JSON value` arguments and will return a JsonValue. + +```ts title="plugins/scaffolder-node/stc/types.ts" +export type TemplateFilter = (...args: JsonValue[]) => JsonValue | undefined; +``` + +OR + +function of the `ScaffolderTemplatingExtensionPoint` interface +addTemplateFilters(filters: Record): void;