diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-16 00:17:50 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-16 00:17:50 +0300 |
commit | 4dba6ce15ae9b5208b1e2d68c96d7b1dce0a07ab (patch) | |
tree | 8375ba7778fdff45a367d4af197dc9f12b38ab2b /tpl/urls | |
parent | 880ca19f209e68e6a8daa6686b361515ecacc91e (diff) |
tpl/urls: Add anchorize template func
Diffstat (limited to 'tpl/urls')
-rw-r--r-- | tpl/urls/init.go | 7 | ||||
-rw-r--r-- | tpl/urls/urls.go | 12 |
2 files changed, 19 insertions, 0 deletions
diff --git a/tpl/urls/init.go b/tpl/urls/init.go index c48fe8ca1..debaaabf9 100644 --- a/tpl/urls/init.go +++ b/tpl/urls/init.go @@ -59,6 +59,13 @@ func init() { [][2]string{}, ) + ns.AddMethodMapping(ctx.Anchorize, + []string{"anchorize"}, + [][2]string{ + {`{{ "This is a title" | anchorize }}`, `this-is-a-title`}, + }, + ) + return ns } diff --git a/tpl/urls/urls.go b/tpl/urls/urls.go index a9f8f4f76..b93c7cada 100644 --- a/tpl/urls/urls.go +++ b/tpl/urls/urls.go @@ -16,6 +16,9 @@ package urls import ( "errors" "fmt" + + "github.com/russross/blackfriday" + "html/template" "net/url" @@ -78,6 +81,15 @@ func (ns *Namespace) URLize(a interface{}) (string, error) { 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 { Ref(refs ...string) (string, error) RelRef(refs ...string) (string, error) |