You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zk/internal/adapter/handlebars/helpers/substring.go

32 lines
625 B
Go

package helpers
import (
"github.com/aymerick/raymond"
)
// RegisterSubstring registers a {{substring}} template helper which extracts a
// substring given a starting index and a length.
//
// {{substring 'A full quote' 2 4}} -> "full"
// {{substring 'A full quote' -5 5}} -> "quote"
//
func RegisterSubstring() {
raymond.RegisterHelper("substring", func(str string, index int, length int) string {
if index < 0 {
index = len(str) + index
}
if index >= len(str) {
return ""
}
end := min(index+length, len(str))
return str[index:end]
})
}
func min(a, b int) int {
if a < b {
return a
}
return b
}