diff --git a/golink.go b/golink.go index 298eb86..bea74e3 100644 --- a/golink.go +++ b/golink.go @@ -629,6 +629,12 @@ var expandFuncMap = texttemplate.FuncMap{ "PathEscape": url.PathEscape, "QueryEscape": url.QueryEscape, "TrimSuffix": strings.TrimSuffix, + "Match": regexMatch, +} + +func regexMatch(pattern string, s string) bool { + b, _ := regexp.MatchString(pattern, s) + return b } // expandLink returns the expanded long URL to redirect to, executing any diff --git a/golink_test.go b/golink_test.go index 8ec39fb..03ca143 100644 --- a/golink_test.go +++ b/golink_test.go @@ -417,6 +417,18 @@ func TestExpandLink(t *testing.T) { remainder: "a/", want: "http://host.com/a", }, + { + name: "template-with-regex-func", + long: `http://host.com/{{if Match "\\d+" .Path}}id/{{.Path}}{{else}}search/{{.Path}}{{end}}`, + remainder: "123", + want: "http://host.com/id/123", + }, + { + name: "template-with-regex-func2", + long: `http://host.com/{{if Match "\\d+" .Path}}id/{{.Path}}{{else}}search/{{.Path}}{{end}}`, + remainder: "query", + want: "http://host.com/search/query", + }, { name: "relative-link", long: `rel`, diff --git a/tmpl/help.html b/tmpl/help.html index 3d10406..81a41ab 100644 --- a/tmpl/help.html +++ b/tmpl/help.html @@ -55,6 +55,8 @@

Advanced destination links