Skip to content

Commit b3ab9a6

Browse files
committed
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 <[email protected]>
1 parent 701c466 commit b3ab9a6

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

golink.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,10 @@ func (e expandEnv) User() (string, error) {
651651
var expandFuncMap = texttemplate.FuncMap{
652652
"PathEscape": url.PathEscape,
653653
"QueryEscape": url.QueryEscape,
654+
"TrimPrefix": strings.TrimPrefix,
654655
"TrimSuffix": strings.TrimSuffix,
656+
"ToLower": strings.ToLower,
657+
"ToUpper": strings.ToUpper,
655658
"Match": regexMatch,
656659
}
657660

golink_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,30 @@ func TestExpandLink(t *testing.T) {
411411
remainder: "a/b+c",
412412
want: "http://host.com/a%2Fb%2Bc",
413413
},
414+
{
415+
name: "template-with-trimprefix-func",
416+
long: `http://host.com/{{TrimPrefix .Path "BUG-"}}`,
417+
remainder: "BUG-123",
418+
want: "http://host.com/123",
419+
},
414420
{
415421
name: "template-with-trimsuffix-func",
416422
long: `http://host.com/{{TrimSuffix .Path "/"}}`,
417423
remainder: "a/",
418424
want: "http://host.com/a",
419425
},
426+
{
427+
name: "template-with-tolower-func",
428+
long: `http://host.com/{{ToLower .Path}}`,
429+
remainder: "BUG-123",
430+
want: "http://host.com/bug-123",
431+
},
432+
{
433+
name: "template-with-toupper-func",
434+
long: `http://host.com/{{ToUpper .Path}}`,
435+
remainder: "bug-123",
436+
want: "http://host.com/BUG-123",
437+
},
420438
{
421439
name: "template-with-match-func",
422440
long: `http://host.com/{{if Match "\\d+" .Path}}id/{{.Path}}{{else}}search/{{.Path}}{{end}}`,

tmpl/help.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ <h2 id="advanced">Advanced destination links</h2>
6363
<ul>
6464
<li><code>PathEscape</code> is the <a href="https://pkg.go.dev/net/url#PathEscape">url.PathEscape</a> function for escaping values inside a URL path.
6565
<li><code>QueryEscape</code> is the <a href="https://pkg.go.dev/net/url#QueryEscape">url.QueryEscape</a> function for escaping values inside a URL query.
66+
<li><code>TrimPrefix</code> is the <a href="https://pkg.go.dev/strings#TrimPrefix">strings.TrimPrefix</a> function for removing a leading prefix.
6667
<li><code>TrimSuffix</code> is the <a href="https://pkg.go.dev/strings#TrimSuffix">strings.TrimSuffix</a> function for removing a trailing suffix.
68+
<li><code>ToLower</code> is the <a href="https://pkg.go.dev/strings#ToLower">strings.ToLower</a> function for mapping all Unicode letters to their lower case.
69+
<li><code>ToUpper</code> is the <a href="https://pkg.go.dev/strings#ToUpper">strings.ToUpper</a> function for mapping all Unicode letters to their upper case.
6770
<li><code>Match</code> is the <a href="https://pkg.go.dev/regexp#MatchString">regexp.MatchString</a> function for matching a regular expression pattern.
6871
</ul>
6972

0 commit comments

Comments
 (0)