Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

Implement "truncate" template function (#1027)

Nikolay0d0da1018355
diff --git a/app/pkg/tpl/funcs.go b/app/pkg/tpl/funcs.go
index 32a201e41..56f488e23 100644
--- a/app/pkg/tpl/funcs.go
+++ b/app/pkg/tpl/funcs.go
@@ -66,6 +66,12 @@ var templateFunctions = map[string]interface{}{
quoted := quote(text)
return quoted[1 : len(quoted)-1]
},
+ "truncate": func(input string, length int) string {
+ if len([]rune(input)) > length {
+ input = string([]rune(input)[:length-3]) + "..."
+ }
+ return input
+ },
}
func quote(text interface{}) string {
diff --git a/public/pages/Administration/components/webhook/WebhookTemplateInfoModal.tsx b/public/pages/Administration/components/webhook/WebhookTemplateInfoModal.tsx
index 448bd04b4..cee3ac265 100644
--- a/public/pages/Administration/components/webhook/WebhookTemplateInfoModal.tsx
+++ b/public/pages/Administration/components/webhook/WebhookTemplateInfoModal.tsx
@@ -78,6 +78,13 @@ const functions: StringObject<FunctionSpecification> = {
description: "Encode a string into a valid URL query element",
info: "You should use this function when using a value in URL",
},
+ truncate: {
+ params: [
+ { type: "string", desc: "The input string to truncate" },
+ { type: "number", desc: "Length of output string" },
+ ],
+ description: "Truncate the string",
+ },
}
const textExample = 'A new post entitled "{{ .post_title }}" has been created by {{ .author_name }}.'
const jsonExample = `{