ref: 4dba6ce15ae9b5208b1e2d68c96d7b1dce0a07ab
parent: 880ca19f209e68e6a8daa6686b361515ecacc91e
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Apr 15 19:17:50 EDT 2018
tpl/urls: Add anchorize template func
--- a/tpl/urls/init.go
+++ b/tpl/urls/init.go
@@ -59,6 +59,13 @@
[][2]string{},
)
+ ns.AddMethodMapping(ctx.Anchorize,
+ []string{"anchorize"},
+ [][2]string{
+ {`{{ "This is a title" | anchorize }}`, `this-is-a-title`},
+ },
+ )
+
return ns
}
--- a/tpl/urls/urls.go
+++ b/tpl/urls/urls.go
@@ -16,6 +16,9 @@
import (
"errors"
"fmt"
+
+ "github.com/russross/blackfriday"
+
"html/template"
"net/url"
@@ -76,6 +79,15 @@
return "", 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 {