Permalink
Browse files

tpl/urls: Add anchorize template func

  • Loading branch information...
bep committed Apr 15, 2018
1 parent 880ca19 commit 4dba6ce15ae9b5208b1e2d68c96d7b1dce0a07ab
Showing with 19 additions and 0 deletions.
  1. +7 −0 tpl/urls/init.go
  2. +12 −0 tpl/urls/urls.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
}
@@ -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)

0 comments on commit 4dba6ce

Please sign in to comment.