Skip to content

Commit f969a4c

Browse files
Add templating functions for working with URLs (#4625)
* Add safeUrl to mark URL strings as safe Signed-off-by: Ruben Simons <[email protected]> * Add urlUnescape to allow for reversing of escaped URL strings Signed-off-by: Ruben Simons <[email protected]> --------- Signed-off-by: Ruben Simons <[email protected]> Co-authored-by: Ben Kochie <[email protected]>
1 parent cfc4731 commit f969a4c

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

docs/notifications.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ templating.
9393
| reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. |
9494
| 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.) |
9595
| safeHtml | text string | [html/template.HTML](https://golang.org/pkg/html/template/#HTML), Marks string as HTML not requiring auto-escaping. |
96+
| safeUrl | text string | [html/template.URL](https://golang.org/pkg/html/template/#URL), Marks string as URL not requiring auto-escaping. |
97+
| urlUnescape | text string | [url.QueryUnescape](https://pkg.go.dev/net/url#QueryUnescape), unescapes a URL with % encoding |
9698
| stringSlice | ...string | Returns the passed strings as a slice of strings. |
9799
| 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). |
98100
| tz | string, time.Time | Returns the time in the timezone. For example, Europe/Paris. |

template/template.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,10 @@ var DefaultFuncs = FuncMap{
188188
"safeHtml": func(text string) tmplhtml.HTML {
189189
return tmplhtml.HTML(text)
190190
},
191+
"safeUrl": func(text string) tmplhtml.URL {
192+
return tmplhtml.URL(text)
193+
},
194+
"urlUnescape": url.QueryUnescape,
191195
"reReplaceAll": func(pattern, repl, text string) string {
192196
re := regexp.MustCompile(pattern)
193197
return re.ReplaceAllString(text, repl)

template/template_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,28 @@ func TestTemplateExpansion(t *testing.T) {
367367
html: true,
368368
exp: "<b>",
369369
},
370+
{
371+
title: "URL template with escaping",
372+
in: `<a href="/search?{{ "q=test%20foo" }}"></a>`,
373+
html: true,
374+
exp: `<a href="/search?q%3dtest%2520foo"></a>`,
375+
},
376+
{
377+
title: "URL template using safeUrl",
378+
in: `<a href="/search?{{ "q=test%20foo" | safeUrl }}"></a>`,
379+
html: true,
380+
exp: `<a href="/search?q=test%20foo"></a>`,
381+
},
370382
{
371383
title: "Template using reReplaceAll",
372384
in: `{{ reReplaceAll "ab" "AB" "abcdabcda"}}`,
373385
exp: "ABcdABcda",
374386
},
387+
{
388+
title: "Template using urlUnescape",
389+
in: `{{ "search?q=test%20foo" | urlUnescape }}`,
390+
exp: "search?q=test foo",
391+
},
375392
{
376393
title: "Template using stringSlice",
377394
in: `{{ with .GroupLabels }}{{ with .Remove (stringSlice "key1" "key3") }}{{ .SortedPairs.Values }}{{ end }}{{ end }}`,

0 commit comments

Comments
 (0)