mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-26 13:40:38 +03:00
tpl/transform: Optional options for highlight func
Closes #9249 Fixes gohugoio/hugoDocs#63
This commit is contained in:
parent
b4f27ef8e7
commit
5538507e90
3 changed files with 100 additions and 11 deletions
|
@ -59,13 +59,21 @@ func (ns *Namespace) Emojify(s interface{}) (template.HTML, error) {
|
|||
|
||||
// Highlight returns a copy of s as an HTML string with syntax
|
||||
// highlighting applied.
|
||||
func (ns *Namespace) Highlight(s interface{}, lang, opts string) (template.HTML, error) {
|
||||
func (ns *Namespace) Highlight(s interface{}, lang string, opts ...interface{}) (template.HTML, error) {
|
||||
ss, err := cast.ToStringE(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
highlighted, _ := ns.deps.ContentSpec.Converters.Highlight(ss, lang, opts)
|
||||
sopts := ""
|
||||
if len(opts) > 0 {
|
||||
sopts, err = cast.ToStringE(opts[0])
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
highlighted, _ := ns.deps.ContentSpec.Converters.Highlight(ss, lang, sopts)
|
||||
return template.HTML(highlighted), nil
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ import (
|
|||
"github.com/gohugoio/hugo/helpers"
|
||||
"github.com/gohugoio/hugo/hugofs"
|
||||
"github.com/gohugoio/hugo/langs"
|
||||
|
||||
)
|
||||
|
||||
type tstNoStringer struct{}
|
||||
|
@ -71,10 +70,11 @@ func TestHighlight(t *testing.T) {
|
|||
for _, test := range []struct {
|
||||
s interface{}
|
||||
lang string
|
||||
opts string
|
||||
opts interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
{"func boo() {}", "go", "", "boo"},
|
||||
{"func boo() {}", "go", nil, "boo"},
|
||||
// Issue #4179
|
||||
{`<Foo attr=" < "></Foo>`, "xml", "", `&lt;`},
|
||||
{tstNoStringer{}, "go", "", false},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue