Skip to content

add TrimPrefix, ToUpper, and ToLower template funcs #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2024
Merged
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
3 changes: 3 additions & 0 deletions golink.go
Original file line number Diff line number Diff line change
@@ -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,
}

18 changes: 18 additions & 0 deletions golink_test.go
Original file line number Diff line number Diff line change
@@ -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}}`,
3 changes: 3 additions & 0 deletions tmpl/help.html
Original file line number Diff line number Diff line change
@@ -63,7 +63,10 @@ <h2 id="advanced">Advanced destination links</h2>
<ul>
<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.
<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.
<li><code>TrimPrefix</code> is the <a href="https://pkg.go.dev/strings#TrimPrefix">strings.TrimPrefix</a> function for removing a leading prefix.
<li><code>TrimSuffix</code> is the <a href="https://pkg.go.dev/strings#TrimSuffix">strings.TrimSuffix</a> function for removing a trailing suffix.
<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.
<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.
<li><code>Match</code> is the <a href="https://pkg.go.dev/regexp#MatchString">regexp.MatchString</a> function for matching a regular expression pattern.
</ul>