Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ templating.
| reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
| join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) |
| safeHtml | text string | [html/template.HTML](https://golang.org/pkg/html/template/#HTML), Marks string as HTML not requiring auto-escaping. |
| safeUrl | text string | [html/template.URL](https://golang.org/pkg/html/template/#URL), Marks string as URL not requiring auto-escaping. |
| urlUnescape | text string | [url.QueryUnescape](https://pkg.go.dev/net/url#QueryUnescape), unescapes a URL with % encoding |
| stringSlice | ...string | Returns the passed strings as a slice of strings. |
| date | string, time.Time | Returns the text representation of the time in the specified format. For documentation on formats refer to [pkg.go.dev/time](https://pkg.go.dev/time#pkg-constants). |
| tz | string, time.Time | Returns the time in the timezone. For example, Europe/Paris. |
Expand Down
4 changes: 4 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ var DefaultFuncs = FuncMap{
"safeHtml": func(text string) tmplhtml.HTML {
return tmplhtml.HTML(text)
},
"safeUrl": func(text string) tmplhtml.URL {
return tmplhtml.URL(text)
},
"urlUnescape": url.QueryUnescape,
"reReplaceAll": func(pattern, repl, text string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(text, repl)
Expand Down
17 changes: 17 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,28 @@ func TestTemplateExpansion(t *testing.T) {
html: true,
exp: "<b>",
},
{
title: "URL template with escaping",
in: `<a href="/search?{{ "q=test%20foo" }}"></a>`,
html: true,
exp: `<a href="/search?q%3dtest%2520foo"></a>`,
},
{
title: "URL template using safeUrl",
in: `<a href="/search?{{ "q=test%20foo" | safeUrl }}"></a>`,
html: true,
exp: `<a href="/search?q=test%20foo"></a>`,
},
{
title: "Template using reReplaceAll",
in: `{{ reReplaceAll "ab" "AB" "abcdabcda"}}`,
exp: "ABcdABcda",
},
{
title: "Template using urlUnescape",
in: `{{ "search?q=test%20foo" | urlUnescape }}`,
exp: "search?q=test foo",
},
{
title: "Template using stringSlice",
in: `{{ with .GroupLabels }}{{ with .Remove (stringSlice "key1" "key3") }}{{ .SortedPairs.Values }}{{ end }}{{ end }}`,
Expand Down