mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-29 00:22:09 -05:00
tpl/urls: Add anchorize template func
This commit is contained in:
parent
880ca19f20
commit
4dba6ce15a
2 changed files with 19 additions and 0 deletions
|
@ -59,6 +59,13 @@ func init() {
|
||||||
[][2]string{},
|
[][2]string{},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ns.AddMethodMapping(ctx.Anchorize,
|
||||||
|
[]string{"anchorize"},
|
||||||
|
[][2]string{
|
||||||
|
{`{{ "This is a title" | anchorize }}`, `this-is-a-title`},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
return ns
|
return ns
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,9 @@ package urls
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/russross/blackfriday"
|
||||||
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
|
@ -78,6 +81,15 @@ func (ns *Namespace) URLize(a interface{}) (string, error) {
|
||||||
return ns.deps.PathSpec.URLize(s), nil
|
return ns.deps.PathSpec.URLize(s), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Anchorize creates sanitized anchor names that are compatible with Blackfriday.
|
||||||
|
func (ns *Namespace) Anchorize(a interface{}) (string, error) {
|
||||||
|
s, err := cast.ToStringE(a)
|
||||||
|
if err != nil {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return blackfriday.SanitizedAnchorName(s), nil
|
||||||
|
}
|
||||||
|
|
||||||
type reflinker interface {
|
type reflinker interface {
|
||||||
Ref(refs ...string) (string, error)
|
Ref(refs ...string) (string, error)
|
||||||
RelRef(refs ...string) (string, error)
|
RelRef(refs ...string) (string, error)
|
||||||
|
|
Loading…
Reference in a new issue