Skip to content

Commit f69788c

Browse files
committed
feat: add split and toJSON template functions
1 parent a10e641 commit f69788c

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package main
33
import (
44
"bytes"
55
"encoding/base64"
6+
"encoding/json"
67
"errors"
78
"fmt"
89
"log"
910
"os"
1011
"reflect"
1112
"strconv"
13+
"strings"
1214
"text/template"
1315
"time"
1416
_ "time/tzdata"
@@ -143,6 +145,17 @@ var funcMap = template.FuncMap{
143145
"base64": func(in string) string {
144146
return base64.StdEncoding.EncodeToString([]byte(in))
145147
},
148+
"split": func(sep string, in string) []string {
149+
return strings.Split(in, sep)
150+
},
151+
"toJSON": func(in interface{}) string {
152+
b, err := json.Marshal(in)
153+
if err != nil {
154+
log.Printf("failed to marshal to JSON: %v", err)
155+
return ""
156+
}
157+
return string(b)
158+
},
146159
}
147160

148161
func renderTemplate(templateFilePath string, vars vars) (string, error) {

main_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,13 @@ func TestRenderTemplate(t *testing.T) {
153153
"time": time.Date(2023, time.August, 6, 15, 8, 28, 0, time.UTC),
154154
},
155155
nil,
156-
"06 Aug 2023 11:08:28\n06 Aug 2023 11:08:28\n[download](https://github.com)\n1,000\nQUJD\n",
156+
`06 Aug 2023 11:08:28
157+
06 Aug 2023 11:08:28
158+
[download](https://github.com)
159+
1,000
160+
QUJD
161+
["1","2","3"]
162+
`,
157163
},
158164
}
159165

testdata/funcs.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
{{ "https://github.com" | mdlink "download" }}
44
{{ "1000" | number }}
55
{{ "ABC" | base64 }}
6+
{{ "1,2,3" | split "," | toJSON }}

0 commit comments

Comments
 (0)