Skip to content
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
26 changes: 14 additions & 12 deletions templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@ import (
// basicFunctions are the set of initial
// functions provided to every template.
var basicFunctions = template.FuncMap{
"json": func(v any) string {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(v)
if err != nil {
panic(err)
}

// Remove the trailing new line added by the encoder
return strings.TrimSpace(buf.String())
},
"json": formatJSON,
"split": strings.Split,
"join": strings.Join,
"title": strings.Title, //nolint:nolintlint,staticcheck // strings.Title is deprecated, but we only use it for ASCII, so replacing with golang.org/x/text is out of scope
Expand Down Expand Up @@ -103,3 +92,16 @@ func truncateWithLength(source string, length int) string {
}
return source[:length]
}

func formatJSON(v any) string {
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(false)
err := enc.Encode(v)
if err != nil {
panic(err)
}

// Remove the trailing new line added by the encoder
return strings.TrimSpace(buf.String())
}
Loading