zk/adapter/handlebars/helpers/slug.go

26 lines
727 B
Go
Raw Normal View History

2020-12-26 19:10:39 +00:00
package helpers
import (
"github.com/aymerick/raymond"
"github.com/gosimple/slug"
"github.com/mickael-menu/zk/util"
)
2020-12-27 17:58:22 +00:00
// RegisterSlug registers a {{slug}} template helper to slugify text.
//
// {{slug "This will be slugified!"}} -> this-will-be-slugified
// {{#slug}}This will be slugified!{{/slug}} -> this-will-be-slugified
2020-12-26 19:10:39 +00:00
func RegisterSlug(logger util.Logger, lang string) {
raymond.RegisterHelper("slug", func(opt interface{}) string {
switch arg := opt.(type) {
case *raymond.Options:
return slug.MakeLang(arg.Fn(), lang)
case string:
return slug.MakeLang(arg, lang)
default:
logger.Printf("the {{slug}} template helper is expecting a string as argument, received: %v", opt)
return ""
}
})
}