mirror of https://github.com/mickael-menu/zk
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.
26 lines
718 B
Go
26 lines
718 B
Go
4 years ago
|
package helpers
|
||
|
|
||
|
import (
|
||
|
"github.com/aymerick/raymond"
|
||
|
"github.com/gosimple/slug"
|
||
4 years ago
|
"github.com/mickael-menu/zk/internal/util"
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
// NewSlugHelper creates a new template helper to slugify text.
|
||
4 years ago
|
//
|
||
|
// {{slug "This will be slugified!"}} -> this-will-be-slugified
|
||
|
// {{#slug}}This will be slugified!{{/slug}} -> this-will-be-slugified
|
||
4 years ago
|
func NewSlugHelper(lang string, logger util.Logger) interface{} {
|
||
|
return func(opt interface{}) string {
|
||
4 years ago
|
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 ""
|
||
|
}
|
||
4 years ago
|
}
|
||
4 years ago
|
}
|