summaryrefslogtreecommitdiffstats
path: root/markup
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-02-22 11:27:14 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-02-23 18:08:39 +0100
commitcd0c5d7ef32cbd570af00c50ce760452381df64e (patch)
tree13dcde7bb470d2ea96e7b0ab11377d1b2baa9cf1 /markup
parente7e194435b4c566f47d644f2e60c096ea1866254 (diff)
Allow markdown attribute lists to be used in title render hooks
Fixes #8270
Diffstat (limited to 'markup')
-rw-r--r--markup/converter/hooks/hooks.go7
-rw-r--r--markup/goldmark/render_hooks.go33
2 files changed, 35 insertions, 5 deletions
diff --git a/markup/converter/hooks/hooks.go b/markup/converter/hooks/hooks.go
index 622d503ef..6f08a2161 100644
--- a/markup/converter/hooks/hooks.go
+++ b/markup/converter/hooks/hooks.go
@@ -19,6 +19,10 @@ import (
"github.com/gohugoio/hugo/identity"
)
+type AttributesProvider interface {
+ Attributes() map[string]string
+}
+
type LinkContext interface {
Page() interface{}
Destination() string
@@ -45,6 +49,9 @@ type HeadingContext interface {
Text() string
// PlainText is the unrendered version of Text.
PlainText() string
+
+ // Attributes (e.g. CSS classes)
+ AttributesProvider
}
// HeadingRenderer describes a uniquely identifiable rendering hook.
diff --git a/markup/goldmark/render_hooks.go b/markup/goldmark/render_hooks.go
index f17e4e139..6bedc897e 100644
--- a/markup/goldmark/render_hooks.go
+++ b/markup/goldmark/render_hooks.go
@@ -14,6 +14,8 @@
package goldmark
import (
+ "sync"
+
"github.com/gohugoio/hugo/markup/converter/hooks"
"github.com/yuin/goldmark"
@@ -38,6 +40,25 @@ func newLinks() goldmark.Extender {
return &links{}
}
+type attributesHolder struct {
+ // What we get from Goldmark.
+ astAttributes []ast.Attribute
+
+ // What we send to the the render hooks.
+ attributesInit sync.Once
+ attributes map[string]string
+}
+
+func (a *attributesHolder) Attributes() map[string]string {
+ a.attributesInit.Do(func() {
+ a.attributes = make(map[string]string)
+ for _, attr := range a.astAttributes {
+ a.attributes[string(attr.Name)] = string(util.EscapeHTML(attr.Value.([]byte)))
+ }
+ })
+ return a.attributes
+}
+
type linkContext struct {
page interface{}
destination string
@@ -76,6 +97,7 @@ type headingContext struct {
anchor string
text string
plainText string
+ *attributesHolder
}
func (ctx headingContext) Page() interface{} {
@@ -301,11 +323,12 @@ func (r *hookedRenderer) renderHeading(w util.BufWriter, source []byte, node ast
err := h.HeadingRenderer.RenderHeading(
w,
headingContext{
- page: ctx.DocumentContext().Document,
- level: n.Level,
- anchor: string(anchor),
- text: string(text),
- plainText: string(n.Text(source)),
+ page: ctx.DocumentContext().Document,
+ level: n.Level,
+ anchor: string(anchor),
+ text: string(text),
+ plainText: string(n.Text(source)),
+ attributesHolder: &attributesHolder{astAttributes: n.Attributes()},
},
)