From b3ab9a617620fb9be7b589ae3fee5f12e8b205a7 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Thu, 19 Sep 2024 09:12:37 -0700 Subject: [PATCH] add TrimPrefix, ToUpper, and ToLower template funcs We need TrimPrefix and ToUpper for some links at Tailscale, and might as well ToLower as well at that point. Updates #10 Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris --- golink.go | 3 +++ golink_test.go | 18 ++++++++++++++++++ tmpl/help.html | 3 +++ 3 files changed, 24 insertions(+) diff --git a/golink.go b/golink.go index 7bbffcd..b58b60c 100644 --- a/golink.go +++ b/golink.go @@ -651,7 +651,10 @@ func (e expandEnv) User() (string, error) { var expandFuncMap = texttemplate.FuncMap{ "PathEscape": url.PathEscape, "QueryEscape": url.QueryEscape, + "TrimPrefix": strings.TrimPrefix, "TrimSuffix": strings.TrimSuffix, + "ToLower": strings.ToLower, + "ToUpper": strings.ToUpper, "Match": regexMatch, } diff --git a/golink_test.go b/golink_test.go index f65fda4..564a56b 100644 --- a/golink_test.go +++ b/golink_test.go @@ -411,12 +411,30 @@ func TestExpandLink(t *testing.T) { remainder: "a/b+c", want: "http://host.com/a%2Fb%2Bc", }, + { + name: "template-with-trimprefix-func", + long: `http://host.com/{{TrimPrefix .Path "BUG-"}}`, + remainder: "BUG-123", + want: "http://host.com/123", + }, { name: "template-with-trimsuffix-func", long: `http://host.com/{{TrimSuffix .Path "/"}}`, remainder: "a/", want: "http://host.com/a", }, + { + name: "template-with-tolower-func", + long: `http://host.com/{{ToLower .Path}}`, + remainder: "BUG-123", + want: "http://host.com/bug-123", + }, + { + name: "template-with-toupper-func", + long: `http://host.com/{{ToUpper .Path}}`, + remainder: "bug-123", + want: "http://host.com/BUG-123", + }, { name: "template-with-match-func", long: `http://host.com/{{if Match "\\d+" .Path}}id/{{.Path}}{{else}}search/{{.Path}}{{end}}`, diff --git a/tmpl/help.html b/tmpl/help.html index 63e5489..7b66b61 100644 --- a/tmpl/help.html +++ b/tmpl/help.html @@ -63,7 +63,10 @@

Advanced destination links